I am trying to put together a C code, wherein a thread performs a specific operation when an event occurs. However, as soon as I create the task using pthread_create(), my code jumps to thread's function. Is it possible to somehow just initiate the thread but keeping it idle till an event occurs and meanwhile my other threads keep executing independently ?
Asked
Active
Viewed 242 times
2
-
You need a mutex, a condition variable, and some predicate data (protected by the mutex). Examples of that trio abound on this site, Google, and a plethora of books on pthreads. It just takes some searching. – WhozCraig Sep 11 '18 at 03:19
-
What would *this* event be?! – Antti Haapala -- Слава Україні Sep 11 '18 at 07:23
-
A mouse click. But this muse click shouldnt cause my other threads to wait. Any task that is independent and not waiting on mouse click should execute itself even if the mouse isnt pressed. – Moose Sep 11 '18 at 16:46
-
1In your thread, you can do a blocking read on the `/dev/input/event#`, where # is the mouse event and check the event code to detect a click. – dhanushka Sep 15 '18 at 05:56
1 Answers
0
You can use semaphore or condition variable.
Semaphore The event thread will have sem_post() operation, operation thread will have sem_wait() operation.
Condition variable Similar to semaphore. Condition variable works with a mutex. When an event occurs, signal the operation thread(pthread_cond_signal()). Signal will be received by operation thread, which is waiting at pthread_cond_wait().

Preeti
- 171
- 4