Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows Event implementation in Linux using conditional variables?
    primarykey
    data
    text
    <p>I am trying to implement very simple Windows events in Linux. Only for my scenario - 3 threads, 1 main and 2 secondary. Each of secondary threads raise 1 event by SetEvent and main thread wait it. Example:</p> <pre><code>int main() { void* Events[2]; Events[0] = CreateEvent(); Events[1] = CreateEvent(); pthread_start(Thread, Events[0]); pthread_start(Thread, Events[1]); WaitForMultipleObjects(2, Events, 30000) // 30 seconds timeout return 0; } int* thread(void* Event) { // Do something SetEvent(Event); // Do something } </code></pre> <p>So, to implement it, i use conditional variables. But my question is - is this a right way? Or i doing something wrong? My implementation:</p> <pre><code>// Actually, this function return pointer to struct with mutex and cond // here i just simplified example void* CreateEvent(mutex, condition) { pthread_mutex_init(mutex, NULL); pthread_cond_init(condition, NULL); } bool SetEvent (mutex, condition) { pthread_mutex_lock(mutex); pthread_cond_signal(condition); pthread_mutex_unlock(mutex); } int WaitForSingleObject(mutex, condition, timeout) { pthread_mutex_lock(mutex); pthread_cond_timedwait(condition, mutex, timeout); pthread_mutex_unlock(mutex); } // Call WaitForSingleObject for each event. // Yes, i know, that its a wrong way, but it should work in my example. int WaitForMultipleObjects(count, mutex[], condition[], timeout); </code></pre> <p>And all seems good, but i think, that problem will appear when i call WaitFor.. function in Main thread before SetEvent in secondary thread will be called. In Windows, it worked well, but in Linux - only idea is described above.</p> <p>Maybe you tell me the better way to solve it? Thank you.</p> <p><strong>UPD:</strong> Timeout is very important, because one of the secondary threads may not pass SetEvent().</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload