From 102341d7ae8793c29d44fa416d3b5b797d1eca3e Mon Sep 17 00:00:00 2001 From: Clay Smith Date: Tue, 1 Aug 2023 01:09:09 -0500 Subject: First commit --- threads.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 threads.c (limited to 'threads.c') diff --git a/threads.c b/threads.c new file mode 100644 index 0000000..d9230a3 --- /dev/null +++ b/threads.c @@ -0,0 +1,63 @@ +// C program to implement cond(), signal() +// and wait() functions +#include +#include +#include + +// Declaration of thread condition variable +pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER; + +// declaring mutex +pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; + +int done = 1; + +// Thread function +void* foo() +{ + + sleep(2); + // acquire a lock + pthread_mutex_lock(&lock); + if (done == 1) { + + // let's wait on condition variable cond1 + done = 2; + printf("Waiting on condition variable cond1\n"); + pthread_cond_wait(&cond1, &lock); + } + else { + + // Let's signal condition variable cond1 + printf("Signaling condition variable cond1\n"); + pthread_cond_signal(&cond1); + } + + // release lock + // pthread_mutex_unlock(&lock); + + printf("Returning thread\n"); + + return NULL; +} + +// Driver code +int main() +{ + pthread_t tid1, tid2; + + // Create thread 1 + pthread_create(&tid1, NULL, foo, NULL); + + // sleep for 1 sec so that thread 1 + // would get a chance to run first + sleep(1); + + // Create thread 2 + pthread_create(&tid2, NULL, foo, NULL); + + // wait for the completion of thread 2 + pthread_join(tid2, NULL); + + return 0; +} -- cgit v1.2.1