CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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
    Last edited by lucky6969b; July 25th, 2014 at 02:30 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: These threads never execute in artbitrary order?

    Why does it matter for you in which order these threads were started/executed?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    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.

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: These threads never execute in artbitrary order?

    Learn about synchronization objects such as mutex, semaphore, and events.
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured