Click to See Complete Forum and Search --> : semaphores and pthread...


plinius
August 4th, 2005, 09:09 PM
Hello, I made some code. I wanted it to do the following:
print 1->20 thread 1 and thread 2 simultaneously
print 20->100 thread 1 alone
print 1-20 thread 2 alone
#include <pthread.h>
#include <windows.h>
#include <iostream>
#include <semaphore.h>

#pragma comment (lib,"pthreadVC1.lib")
using namespace std;

sem_t lampion;

void * scheduler(void *null){
for(int i=0;i<100;i++){
Sleep(200);
printf("\nsh%d",i);fflush(stdout);
}

lampion=(sem_t)1;
sem_post(&lampion);

return((void *)0);
}

void * worker(void *worker){
for(int i=0;i<20;i++){
Sleep(200);
printf("\nwo%d",i);fflush(stdout);
}

sem_wait(&lampion);

for(i=0;i<20;i++){
Sleep(200);
printf("\nwo%d",i);fflush(stdout);
}


return((void *)0);
}

int main(){
pthread_t thread[2];
int status;
pthread_attr_t attr;

sem_init(&lampion,NULL,0);

pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_JOINABLE);
pthread_create(&thread[0],&attr,scheduler,NULL);/*sheduler*/
pthread_create(&thread[1],&attr,worker,NULL);/*worker*/
pthread_join(thread[0],(void**)&status);
pthread_join(thread[1],(void**)&status);
pthread_exit(NULL);
}

But it allways saying there is an unhandled exeption

What is wrong in the code? How should I do what I want?

Thanks for any input...

Plinius