|
|
[php]
#include<pthread.h>
int total;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *
count ()
{
int i;
for (i = 0; i < 100; i++)
{
pthread_mutex_lock (&lock);
total++;
pthread_mutex_unlock (&lock);
}
}
main ()
{
pthread_t t1, t2;
total = 0;
pthread_create (&t1, NULL, count, NULL);
pthread_create (&t2, NULL, count, NULL);
pthread_join (t1, NULL);
pthread_join (t1, NULL);
printf ("total=%d\n", total);
}
[/php] |
|