79471301

Date: 2025-02-27 00:00:19
Score: 1.5
Natty:
Report link

Thanks to @john Bollinger! His suggestion got me on track.

I have created global variable: pthread_cond_t condition;

Initialized this one with defaults: r = pthread_cond_init(&condition, NULL);

And went to sleep by this: rt = pthread_cond_timedwait(&condition, &lock, &ts);

Where lock is a global lockfile (which I was already using anyways): pthread_mutex_t lock;

And ts is of struct timespec ts;

Because pthread_cond_timedwait does not wait for seconds to pass, instead until a specified time has come I just added the sleeping time: ts.tv_sec += waitSecs;

In the other thread it was very easy. Just inform the waiting thread: pthread_cond_signal(&condition); while the lock is blocked by the sending thread.

Works like a charm! Thanks again!

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @john
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Christian