I have a basic program that is using multi-threading. Each thread needs to use different random numbers when the thread procedure is called. I have tried seeding the random number generator within the thread procedure, but I get the same random numbers for each thread. Here is a simple version of what I am doing:
public ref class ThreadX
{
public:
void ThreadProc()
{
srand(time(NULL));
Console::WriteLine(rand()); //Will output same random numbers
}
}
int main(){
ThreadX^ process1 = gcnew ThreadX(gasStationATM);
Thread^ Thread1 = gcnew Thread(gcnew ThreadStart(process1, &ThreadX::ThreadProc));
Thread^ Thread2 = gcnew Thread(gcnew ThreadStart(process1, &ThreadX::ThreadProc));
Thread1->Start();
Thread2->Start();
}
What I originally thought was when the second thread was started, the second thread started the time for the seed would be different and give a different series of random numbers for the second number. How can I seed the srand in C++ CLI so that each thread generates random numbers.
Aucun commentaire:
Enregistrer un commentaire