Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sem_t sem;
- void* thread(void* arg) {
- sem_wait(&mutex);
- printf("Entering, thread id %ld\n", pthread_self());
- sleep(4);
- printf("Exiting, thread id %ld\n", pthread_self());
- sem_post(&mutex);
- }
- int main() {
- sem_init(&mutex, 0, 2);
- pthread_t t1, t2, t3, t4;
- pthread_create(&t1, NULL, thread, NULL);
- sleep(1);
- pthread_create(&t2, NULL, thread, NULL);
- sleep(1);
- pthread_create(&t3, NULL, thread, NULL);
- sleep(1);
- pthread_create(&t4, NULL, thread, NULL);
- pthread_join(t1, NULL);
- pthread_join(t2, NULL);
- pthread_join(t3, NULL);
- pthread_join(t4, NULL);
- sem_destroy(&mutex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement