simple join example
This commit is contained in:
27
threads-sema/join.c
Normal file
27
threads-sema/join.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "common.h"
|
||||
#include "common_threads.h"
|
||||
|
||||
sem_t s;
|
||||
|
||||
void *child(void *arg) {
|
||||
sleep(4);
|
||||
printf("child\n");
|
||||
Sem_post(&s); // signal here: child is done
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
Sem_init(&s, 0);
|
||||
printf("parent: begin\n");
|
||||
pthread_t c;
|
||||
Pthread_create(&c, NULL, child, NULL);
|
||||
Sem_wait(&s); // wait here for child
|
||||
printf("parent: end\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user