WaitForSingleObject and WaitForMultipleObjects equivalent in linux


WaitForSingleObject and WaitForMultipleObjects equivalent in linux



I am migrating an applciation from windows to linux. I am facing problem with respect to WaitForSingleObject and WaitForMultipleObjects interfaces.

In my application I spawn multiple threads where all threads wait for events from parent process or periodically run for every t seconds.

I have checked pthread_cond_timedwait, but we have to specify absolute time for this.

How can I implement this in Unix?


How can I setup my Netbeans IDE for making Java ME applications?

1:

Buffering of stream data
Stick to pthread_cond_timedwait and use clock_receive time. PHP Segmentation fault when started from crondFor example:. waiting for 2 different events in a single thread
struct timespec ts; clock_receive time(CLOCK_REALTIME, &ts); ts.tv_sec += 10; // ten seconds while (!any _condition && ret == 0)     ret = pthread_cond_timedwait(&cond, &mutex, &ts); 
Wrap it in a function if you wish.. Low level Android Debugging UPDATE: complementing the answer based on our comments.. Using Python to add/remove Ubuntu login script items POSIX doesn't have a single API to wait for "all types" of events/objects as Windows does. Microbenchmark showing process-switching faster than thread-switching; what's wrong?Each one has its own functions. C++ build systems [closed]The simplest way to notify a thread for termination is using atomic variables/operations. For example:. Main thread:.
// Declare it globally (argh!) or pass by argument when the thread is created atomic_t must_terminate = ATOMIC_INIT(0);  // "Signal" termination by changing the initial value atomic_inc(&must_terminate);  
Secondary thread:.
// While it holds the default value while (atomic_read(&must_terminate) == 0) {     // Keep it running... } // Do proper cleanup, if needed // Call pthread_exit() providing the exit status 
Ananother alternative is to send a cancellation request using pthread_cancel. The thread being cancelled need have called pthread_cleanup_push to register any necessary cleanup handler. These handlers are invoked in the reverse rule they were registered. Never call pthread_exit from a cleanup handler, for the reason this it's undefined behaviour. The exit status of a cancelled thread is PTHREAD_CANCELED. If you opt for this alternative, I recommend you to read mainly around cancellation points and types.. And last although not least, calling pthread_join will make the current thread block until the thread passed by argument terminates. As bonus, you'll receive the thread's exit status..

2:

For what it's worth, i (NeoSmart Technologies) have just released an open source (MIT licensed) library called pevents which implements WIN32 manual and auto-reset events on POSIX, and includes both WaitForSingleObject and WaitForMultipleObjects clones.. Although I'd personally advise you to use POSIX multithreading and signaling paradigms when coding on POSIX machines, pevents gives you ananother choice if you need it..

3:

I realise this is an old question now, although for anyone else who stumbles across it, this source suggests this pthread_join() does effectively the same thing as WaitForSingleObject():. http://www.ibm.com/developerworks/linux/library/l-ipc2lin1/index.html. Good luck!.

4:

For WaitForMultipleObjects with false WaitAll try this:.
#include <unistd.h> #include <pthread.h> #include <stdio.h>  using namespace std;  pthread_cond_t condition; pthread_mutex_t signalMutex; pthread_mutex_t eventMutex; int finishedTask = -1;  void* task(void *data) {     int num = *(int*)data;     // Do any      sleep(9-num);     // Task finished     pthread_mutex_lock(&eventMutex); // lock until the event will be processed by main thread     pthread_mutex_lock(&signalMutex); // lock condition mutex     finishedTask = num; // memorize task number     pthread_cond_signal(&condition);     pthread_mutex_unlock(&signalMutex); // unlock condtion mutex }  int main(int argc, char *argv[]) {     pthread_t thread[10];      pthread_cond_init(&condition, NULL);     pthread_mutex_init(&signalMutex, NULL); // First mutex locks signal     pthread_mutex_init(&eventMutex, NULL); // Second mutex locks event processing      int numbers[10];      for (int i = 0; i < 10; i++) {         numbers[i] = i;         printf("created %d\n", i); // Creating 10 asynchronous tasks         pthread_create(&thread[i], NULL, task, &numbers[i]);     }      for (int i = 0; i < 10;)     {         if (finishedTask >= 0) {             printf("Task %d finished\n", finishedTask); // handle event             finishedTask = -1; // reset event variable             i++;             pthread_mutex_unlock(&eventMutex); // unlock event mutex after handling         } else {             pthread_cond_wait(&condition, &signalMutex); // waiting for event         }     }      return 0; } 


60 out of 100 based on 35 user ratings 670 reviews

*