random numbers with threads

H

hande

Hi, I wrote a program with two threads playing a number guessing game.
one thread pick a number the other try to guess it. Also first thread
say try a lower or higher number. but my program crashes in defining
the bounds for generating random numbers.
For instance the first thread pick 35
the other say 19. he should pick a number bigger than 19. for now
that's ok. now second thread say 60. then first thread say lower
number. but my second thread is not like that. how i can define that
borders?

thanks a lot
 
H

hande

Martin said:
What program?
What about posting some code?


else if(num<gnum)
{
gnum=rand()%gnum;
temp2=gnum;
cout<<"Thread2: "<<gnum<<endl;
// pthread_mutex_unlock(&myMutex);
}
else if(num>gnum)
{
gnum=rand()%(100-gnum);
cout<<"Thread2: "<<gnum<<endl;
//pthread_mutex_unlock(&myMutex);
}
 
J

Jim Langston

hande said:
Hi, I wrote a program with two threads playing a number guessing game.
one thread pick a number the other try to guess it. Also first thread
say try a lower or higher number. but my program crashes in defining
the bounds for generating random numbers.
For instance the first thread pick 35
the other say 19. he should pick a number bigger than 19. for now
that's ok. now second thread say 60. then first thread say lower
number. but my second thread is not like that. how i can define that
borders?

thanks a lot

First, try your program without the threads. See if it works that way. If
it does, then it's a threading issue and post some code at:
comp.programming.threads that is small enough to show the problem.

If it does not without the threads, it's a C++ error, post the smallest code
here that compiles that wll show the error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top