Find the answer to your Linux question:
Results 1 to 4 of 4
Hi,All im using WaitForMultipleObjects in windows,but my questions is how to do the same in linux since there is no equivalent api in linux..... (since i need my threads to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    4

    WaitForMultipleObjects equivalent in linux


    Hi,All
    im using WaitForMultipleObjects in windows,but my questions is how to do the same in linux since there is no equivalent api in linux.....
    (since i need my threads to wait for the event to trigger)
    Here is my code
    Code:
    long PredictThread(const Array2D<REAL> &X)  
    	{
    		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 help in fixing this issue
    thanking you ....................

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,633
    Assuming you are using posix threads (pthreads), you may be able to use pthread_cond_wait() to simulate this Windows function. You should carefully read and understand the pthreads documentation before you continue. There is quite a bit of information in the man pages (see "man pthreads" and the man pages for other pthread functions and structures) which will help you use these api's.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Fairfax, Virginia, USA
    Posts
    94
    Hi Shams
    Just to augment what Rubberman passed on, if you want your code to be cross platform so one set of code compiles on both Linux and Windows, you can use this package on Windows:
    POSIX Threads (pthreads) for Win32
    I use it and it works pretty well. You can compile the library statically and link against the static image if you want. Some features which aren't used often (like pthread_yield() for instance()) aren't implemented. If you read the forms, people are skeptical of this library because it doesn't get much development attention but it works well (I think ffmpeg uses this library BTW).

  4. #4
    Just Joined!
    Join Date
    Jun 2012
    Posts
    1
    Hi Shamjs, I got similar issue, in my case, I'm waiting for multiple events on windows, psudo code:
    Code:
    enum {cmd_op,cmd_quit,cmd_total};
    HANDLE hEvents[cmd_total];
    
    bool bQuit = false;
    while(!bQuit)
    {
    switch(WaitForMultipleObjects(hEvents,cmd_total,...);
    {
    case cmd_op:
        //doing something correspondingly here
       break;
    case cmd_quit:
       bQuit = true;
       break;
    }
    }
    I didn't found corresponding equivalence on linux, and I did it this way:

    Code:
    sem_t sem_CmdActive;
    volatile int cmd_op = 0;
    volatile int cmd_quit = 0;
    
    bool bQuit = 0;
    while(!bQuit)
    {
    sem_wait(sem_CmdActive);
    if(cmd_op>0)
    {
    //do something here
    cmd_op--;
    }
    else if(cmd_quit>0)
    {
    bQuit = 0;
    cmd_quit--;
    }
    }
    for thread which need active the command, do this:

    Code:
    cmd_quit++;
    sem_post(sem_CmdActive);
    or

    Code:
    cmd_op++;
    sem_post(sem_CmdActive);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •