LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to implement WaitForMultipleObjects in linux (https://www.linuxquestions.org/questions/programming-9/how-to-implement-waitformultipleobjects-in-linux-908553/)

shamjs 10-17-2011 05:31 AM

how to implement WaitForMultipleObjects in linux
 
Hi,All
for synchronization of threads im using WaitForMultipleObjects in windows,but my questions is how to do the same in linux since there is no equivalent api in linux.....
Here is my code
Code:

long PredictThread(const Array2D<REAL> &X)  //For generating members matrix only
        {
                if (X.dim2()!=m_NumVars) return -1;
                long i,j;
               
                Array3D<REAL>  Ypred(X.dim1(),m_Categories.size(), 6, 0.0);
                       
                HANDLE* TID = new HANDLE[m_Categories.size()];
                DWORD ThreadID;

                TParam* pOTParam = new TParam[m_Categories.size()];
                TParam* pTParam = pOTParam;
                for(j=0;j<m_Categories.size();++j)
                {
                        TParam* pParam = pTParam;
                        pParam->category = j;
                        pParam->model = m_Categories[j];
                        pParam->X = X.copy();
                        pParam->Ypred = Ypred;

                        TID[j] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,(LPVOID)pParam,0,&ThreadID);
                        pTParam++;
                }
                WaitForMultipleObjects(m_Categories.size(), TID, TRUE, INFINITE);
                for(j=0;j<m_Categories.size();++j)
                        CloseHandle(TID[j]);

                delete [] TID;
                delete [] pOTParam;
                m_members = Ypred;               
                return 0;
        }

please suggest me to do this
thannx.................

lior.okman 10-18-2011 01:45 AM

Quote:

Originally Posted by shamjs (Post 4500398)
Hi,All
for synchronization of threads im using WaitForMultipleObjects in windows,but my questions is how to do the same in linux since there is no equivalent api in linux.....

You can use a semaphore to do this. For example, consider the following program:

Code:

#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define NUM_THREADS (2)

sem_t semaphore;

void *worker_thread(void *arg)
{
        int i;
        for (i=0; i<10; ++i) {
                fprintf(stderr,".");
                sleep(1);
        }
        sem_post(&semaphore);
        return NULL;
}


int main(int argc, char* argv[])
{
        pthread_t thread[NUM_THREADS];
        int i;
        sem_init(&semaphore, 0, -1 * NUM_THREADS);

        for (i =0; i< NUM_THREADS; ++i) {
                pthread_create(&thread[i],NULL, &worker_thread, NULL);
        }

        if (sem_wait(&semaphore) < 0) {
                printf("sem_wait failed: %s\n", strerror(errno));
                return EXIT_FAILURE;
        }
        for (i =0; i< NUM_THREADS; ++i) {
              pthread_join(thread[i], NULL);
        }
        printf("\nAll threads are done now\n");
        return EXIT_SUCCESS;
}



All times are GMT -5. The time now is 10:20 PM.