I would like to wake up a pthread from another pthread – but after some time. I know signal or pthread_signal with pthread_cond_wait can be used to wake another thread, but I can’t see a way to schedule this. The situation would be something like:
THREAD 1:
========
while(1)
recv(low priority msg);
dump msg to buffer
THREAD 2:
========
while(1)
recv(high priority msg);
..do a little bit of processing with msg ..
dump msg to buffer
wake(THREAD3, 5-seconds-later); <-- **HOW TO DO THIS? **
//let some msgs collect for at least a 5 sec window.
//i.e.,Don't wake thread3 immediately for every msg rcvd.
THREAD 3:
=========
while(1)
do some stuff ..
Process all msgs in buffer
sleep(60 seconds).
Any simple way to schedule a wakeup (short of creating a 4th thread that wakes up every second and decides if there is a scheduled entry for thread-3 to wakeup). I really don’t want to wakeup thread-3 frequently if there are only low priority msgs in queue. Also, since the messages come in bursts (say 1000 high priority messages in a single burst), I don’t want to wake up thread-3 for every single message. It really slows things down (as there is a bunch of other processing stuff it does every time it wakes up).
I am using an ubuntu pc.
How about the use of the
pthread_cond_tobject available through the pthread API ?You could share such an object within your threads and let them act on it appropriately.
The resulting code should look like this :
If you want your thread 3 to exit the blocking call to
pthread_cond_wait()after a timeout, consider usingpthread_cond_timedwait()instead (read the man carefully, the timeout value you supply is the ABSOLUTE time, not the amount of time you don’t want to exceed).If the timeout expires,
pthread_cond_timedwait()will return anETIMEDOUTerror.EDIT : I skipped error checking in the lock / unlock calls, don’t forget to handle this potential issue !
EDIT² : I reviewed the code a little bit