I wrote a wrote a program and it doesn't work as I expect it to.
I have two threads: thread
triggers func
and anotherThread
triggers anotherFunc
. What I wanted to do is when cont
reaches value 10
in func
, anotherThread
to be triggered using pthread_cond_wait
and pthread_cond_signal
. The strange thing is everything works fine if i uncomment the sleep(1)
line. I'm new to threads and I was following the tutorial here and if I comment the sleep
line in their example it breaks as well.
My question is how can I make this work without any sleep()
calls? And what happens if in my code both func
reaches pthread_mutex_lock
after anotherFunc
? How can I control these things? This is my code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t myMutex;
pthread_cond_t cond;
pthread_attr_t attr;
int cont;
void *func(void*)
{
printf("func\n");
for(int i = 0; i < 20; i++)
{
pthread_mutex_lock(&myMutex);
cont++;
printf("%d\n", cont);
if(cont == 10)
{
printf("signal:\n");
pthread_cond_signal(&cond);
// sleep(1);
}
pthread_mutex_unlock(&myMutex);
}
printf("Done func\n");
pthread_exit(NULL);
}
void *anotherFunc(void*)
{
printf("anotherFunc\n");
pthread_mutex_lock(&myMutex);
printf("waiting...\n");
pthread_cond_wait(&cond, &myMutex);
cont += 10;
printf("slot\n");
pthread_mutex_unlock(&myMutex);
printf("mutex unlocked anotherFunc\n");
printf("Done anotherFunc\n");
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t thread;
pthread_t anotherThread;
pthread_attr_init(&attr);
pthread_mutex_init(&myMutex, NULL);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_cond_init(&cond, NULL);
pthread_create(&anotherThread, &attr, anotherFunc, NULL);
pthread_create(&thread, &attr, func, NULL);
pthread_join(thread, NULL);
pthread_join(anotherThread, NULL);
printf("Done MAIN()");
pthread_mutex_destroy(&myMutex);
pthread_cond_destroy(&cond);
pthread_attr_destroy(&attr);
pthread_exit(NULL);
return 0;
}
Sorry for the long post but I'm new to threads and I'm willing to learn. Also do you know some good references or courses/tutorials on threads and networking on Linux? I want to learn create an chat client and I heard that I have to know threads and networking for that. Problem is I don't know pretty good if what I learn is ok since I don't know what I have to know.
Thank you so much :)
pthread_attr_t
, you can use static initializers for the condition and mutex to simplify the code. Furthermore, your thread functions can justreturn NULL;
orreturn 0;
instead of callingpthread_exit
. And your main thread can justreturn 0;
from main without callingpthread_exit
. The other threads are not running at that point since they were joined. return from main forces a process exit, butpthread_exit
in the primary thread does not force a process exit, which is useful for keeping other threads running. – Shemikashemitepthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
. The complex initializations are needed for situations where you want to set unusual attributes. E.g. make a process-shared robust mutex. – Shemikashemite