These threads never execute in artbitrary order?
Code:
void Arrival_MainThread::Body ()
{
t1 = new boost::thread((Arrive_Fctor()), 2);
t2 = new boost::thread((Arrive_Fctor()), 10000);
t3 = new boost::thread((Arrive_Fctor()), 3000);
Code:
class Arrive_Fctor {
public:
void operator()(int mean) {
m_Arrivals = new Arrivals(mean);
m_Arrivals->Activate();
#ifndef NO_RESOURCE
Resources::ref(m_Arrivals);
#endif
}
Arrivals *m_Arrivals;
};
Code:
void Arrivals::Body ()
{
cprintf ("Arrival Body Entered %d", id);
double arrivalTime = (*InterArrivalTime)();
Sleep(arrivalTime);
cprintf ("Lorry Arrive time %f\n", arrivalTime);
Job* work = new Job(id);
terminate();
}
The order of arrivals always appear as 3,2,1 it can't be 1,2,3 or 3,1,2 or something like that?
The body method is called when m_Arrival->Activate() is executed, and it is running as an independent thread.
I am not so sure It should have slept on that thread?
Any ideas?
Thanks
Jack
Re: These threads never execute in artbitrary order?
Why does it matter for you in which order these threads were started/executed? :confused:
Re: These threads never execute in artbitrary order?
I'd want to simulate a warehouse environment where cargo trucks would come in randomly but within a predictable time range.
I have asked Mark Little, the author of the library, called C++ Sim
The program is originally designed for many processes sharing a common mutex, so that would one process swing into work, the rest will be kept on hold, But I'd like to get some illusion of concurrency.
Re: These threads never execute in artbitrary order?
Learn about synchronization objects such as mutex, semaphore, and events.
Re: These threads never execute in artbitrary order?
Your code isn't currently well suited to getting 'random' arrival times, mainly because the code doesn't actually do anything, just goes to sleep and is then delegated to the OS scheduler to wake them back up.
For this particular case, you may want to make sure the threads have 'slightly random' sleep times to get threads to arrive in random orders. (don't use rand() it's terrible, and make sure you randomly seed the PRNG to get different times for every run).