You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
876 B
38 lines
876 B
6 years ago
|
#include <stdio.h>
|
||
|
#include <pthread.h>
|
||
|
|
||
|
#define NTHREADS 10
|
||
|
void *thread_function(void *);
|
||
|
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
|
||
|
int counter = 0;
|
||
|
|
||
|
main()
|
||
|
{
|
||
|
pthread_t thread_id[NTHREADS];
|
||
|
int i, j;
|
||
|
|
||
|
for(i=0; i < NTHREADS; i++)
|
||
|
{
|
||
|
pthread_create( &thread_id[i], NULL, thread_function, NULL );
|
||
|
}
|
||
|
|
||
|
for(j=0; j < NTHREADS; j++)
|
||
|
{
|
||
|
pthread_join( thread_id[j], NULL);
|
||
|
}
|
||
|
|
||
|
/* Now that all threads are complete I can print the final result. */
|
||
|
/* Without the join I could be printing a value before all the threads */
|
||
|
/* have been completed. */
|
||
|
|
||
|
printf("Final counter value: %d\n", counter);
|
||
|
}
|
||
|
|
||
|
void *thread_function(void *dummyPtr)
|
||
|
{
|
||
|
printf("Thread number %ld\n", pthread_self());
|
||
|
pthread_mutex_lock( &mutex1 );
|
||
|
counter++;
|
||
|
pthread_mutex_unlock( &mutex1 );
|
||
|
}
|