Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- static int nthreads = 10;
- static int init_count = 0;
- pthread_mutex_t init_lock;
- pthread_cond_t init_cond;
- pthread_t threads[10];
- void* func(void* p)
- {
- pthread_mutex_lock(&init_lock);
- init_count++;
- pthread_cond_signal(&init_cond);
- pthread_mutex_unlock(&init_lock);
- return NULL;
- }
- int main(int argc, char* argv[])
- {
- switch(fork()) {
- case 0:
- break;
- case -1:
- perror("perror");
- exit(0);
- default:
- exit(0);
- }
- pthread_mutex_init(&init_lock, NULL);
- pthread_cond_init(&init_cond, NULL);
- int i;
- for(i = 0; i < nthreads; ++i)
- {
- pthread_create(&threads[i], NULL, func, NULL);
- }
- pthread_mutex_lock(&init_lock);
- while (init_count < nthreads)
- {
- pthread_cond_wait(&init_cond, &init_lock);
- }
- pthread_mutex_unlock(&init_lock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement