no lock while signalling
This commit is contained in:
@@ -3,6 +3,7 @@ CFLAGS := -Wall -Werror -I../include
|
||||
|
||||
SRCS := join.c \
|
||||
join_spin.c \
|
||||
join_no_lock.c \
|
||||
join_modular.c
|
||||
|
||||
OBJS := ${SRCS:c=o}
|
||||
|
||||
34
threads-cv/join_no_lock.c
Normal file
34
threads-cv/join_no_lock.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include "common.h"
|
||||
#include "common_threads.h"
|
||||
|
||||
pthread_cond_t c = PTHREAD_COND_INITIALIZER;
|
||||
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
|
||||
int done = 0;
|
||||
|
||||
void *child(void *arg) {
|
||||
printf("child: begin\n");
|
||||
sleep(1);
|
||||
done = 1;
|
||||
printf("child: signal\n");
|
||||
Cond_signal(&c);
|
||||
return NULL;
|
||||
}
|
||||
int main(int argc, char *argv[]) {
|
||||
pthread_t p;
|
||||
printf("parent: begin\n");
|
||||
Pthread_create(&p, NULL, child, NULL);
|
||||
Mutex_lock(&m);
|
||||
printf("parent: check condition\n");
|
||||
while (done == 0) {
|
||||
sleep(2);
|
||||
printf("parent: wait to be signalled...\n");
|
||||
Cond_wait(&c, &m);
|
||||
}
|
||||
Mutex_unlock(&m);
|
||||
printf("parent: end\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user