LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2011, 05:31 AM   #1
shamjs
Member
 
Registered: Sep 2011
Posts: 93

Rep: Reputation: Disabled
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.................
 
Old 10-18-2011, 01:45 AM   #2
lior.okman
LQ Newbie
 
Registered: Sep 2006
Distribution: Debian GNU/Linux
Posts: 9

Rep: Reputation: 0
Quote:
Originally Posted by shamjs View Post
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
WaitFOrMultipleObjects on Linux? asif.ishaq Programming 2 06-26-2012 08:53 PM
How to implement two periodical processes in C++ under Linux? MarijaS Programming 16 07-30-2010 04:46 AM
How can I implement a caret on Linux? antoniog General 2 06-23-2007 08:54 AM
How to implement clustering in linux servers armbarathi Linux - Server 7 03-27-2007 08:23 AM
How to implement network protocol in Linux? jamespayne Programming 2 12-03-2004 01:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration