- The Glibc I using is glibc-2.5, computer architecture is i386
- type definition of pthread_spinlock_t
/* POSIX spinlock data type. */
typedef volatile int pthread_spinlock_t;
- function pthread_spin_lock
- my test of spin lock
#include
#define NUM_THREADS 8
long sum;
pthread_spinlock_t sumlock;
int my_sleep(int i)
{
int j,k,n=0;
for(;i>0;i--)
for(j=0;j<100;j++)
for(k=0;k<100;k++)
n++;
return n;
}
void *BusyWork(void *threadid)
{
int i,k;
long tid = (long)threadid;
for(i=0;i<10;i++){
k = my_sleep(3);
pthread_spin_lock (&sumlock);
printf("%d ", tid);
sum += k;
pthread_spin_unlock (&sumlock);
}
pthread_exit((void *) 0);
}
int main (int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
int rc, t;
void *status;
pthread_spin_init(&sumlock, NULL);
for(t=0; t
rc = pthread_create(&thread[t], NULL, BusyWork, (void*)t);
if (rc){
return 0;
}
}
for(t=0; t
rc = pthread_join(thread[t], &status);
if (rc){
printf("ERROR; return code from pthread_join() is %d\n", rc);
return 0;
}
}
printf("sum = %ld\n", sum);
pthread_spin_destroy(&sumlock);
pthread_exit(NULL);
}
No comments:
Post a Comment