ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.................
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.