Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error in posix threads.

Status
Not open for further replies.

rajeshwari

Programmer
Jun 17, 2001
5
0
0
US
hello everyone.

i am trying to write a multithreaded prg with posix threads and semaphores.

the code is as follows

#define parg 2
#define NTHREADS 5

void *do_job(void *);

sem_t sid[NTHREADS];
pthread_t tid[NTHREADS];

int main()
{
int i,ret;

printf("about to create semaphores\n");
for(i=1;i<=4;i++)
{
ret=sem_init(&sid,0,0);
if(ret==-1)
printf(&quot;error in creating semaphore\n&quot;);
else
printf(&quot;semaphore %d created\n&quot;,i);
}

ret=sem_init(&sid[5],0,5);
if(ret==-1)
printf(&quot;error in creating semaphore\n&quot;);
else
printf(&quot;semaphore 5 created\n&quot;);




for(i=1;i<=NTHREADS;i++)
{
ret=pthread_create(&tid,NULL,do_job,NULL);
if(ret==-1)
printf(&quot;error in creating thread\n&quot;);
else
printf(&quot;thread %d created\n&quot;, i);

}

for(i=1;i<=NTHREADS;i++)
pthread_join(tid,NULL);


printf(&quot;Threads have terminated&quot;);
for(i=1;i<=NTHREADS;i++)
sem_destroy(&sid);
}

void *do_job(void *i)
{
int x,j,m;
pthread_t t;
x=(int)i;
t=pthread_self();
printf(&quot;entered do job\n&quot;);
int sleep_time=2;
for(x=1;x<=NTHREADS;x++)
{
if(tid[x]==t)
{
if(x==1)
j=6;
else
j=x+1;
sem_wait(&sid[j]);
printf(&quot;thread %d is sleeping now\n&quot;,thr_self());
sleep(sleep_time);
printf(&quot;Thread %d is now waking up\n&quot;,thr_self());
sem_post(&sid[x]);

}
}
return(NULL);
}

can someone please tell me what is it that i am doing worng. I want each thread to run individually one after another.
 
hey!
u r passing NULL to the argument list of thread when u r creating thread & second u've got the wrong concept of threads...sorry to say but its true...
Yogi
 
any of you have a good multithreading library/header that will compile in borlandc3.1 and work in dos. <-i know dos doesn't support multithreading, but it'll weave given some yarn and a needle.
i did find a library, but it wouldn't link, and im not good with the mechanics of stacks, etc yet.


im assuming that dojob is the thread(s) you want to run along side main. it seems that you create nthreads, and attempt to start the threads, yet in the thread(s)((dojob)) you try passing it a argument and ignore that argument.
once your tread is running you can't recall the function. so another method of passing arguments is neccessary.
y: x=(int)i;
&: for(x=?
it looks like i was to let the tread know which thread it is yet you reassign its successor before going to the part where the tread needs to know which tread it is. ?
google search: threads c++ programming (yourcompiler) tutorial :(w/)pthread_t <-if thats the thread class/struct
 
hi guys,

i was able to solve the prob in my code. the main prob was that posix threads do not accept an initialization from 1 to 5 in an array of five they only accept an initiallization from 0 to 4. ofcourse i did another mistake in my code which i did not notice in the multitude off changes i kept doing i passed null but was using i in the thread function and hence another error.


my modified code is as follows:


#define NTHREADS 5

void *do_job(void *);

sem_t sid[NTHREADS];
pthread_t tid[NTHREADS];

int main()
{
int i,ret;

printf(&quot;about to create semaphores\n&quot;);
for(i=0;i<5;i++)
{
ret=sem_init(&sid,0,0);
if(ret==-1)
printf(&quot;error in creating semaphore\n&quot;);
else
printf(&quot;semaphore %d created\n&quot;,i);
}


for(i=0;i<NTHREADS;i++)
{
ret=pthread_create(&tid,NULL,do_job,(void *)i);
if(ret==-1)
printf(&quot;error in creating thread\n&quot;);
else
printf(&quot;thread %d created\n&quot;, i);

}

for(i=0;i<NTHREADS;i++)
pthread_join(tid,NULL);


printf(&quot;Threads have terminated&quot;);
for(i=1;i<=NTHREADS;i++)
sem_destroy(&sid);
}

void *do_job(void *i)
{
int x,j,m;
pthread_t t;
x=(int)i;
t=pthread_self();
printf(&quot;entered do job\n&quot;);
int sleep_time=2;
if(tid[x]==t)
{
if(x==0)
{
j=4;
sem_post(&sid[j];
}
else
j=x+1;
sem_wait(&sid[j]);
printf(&quot;thread %d is sleeping now\n&quot;,thr_self());
sleep(sleep_time);
printf(&quot;Thread %d is now waking up\n&quot;,thr_self());
sem_post(&sid[x]);

}
return(NULL);
}

anyway thanx a lot for ur help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top