help with threads communication

This is a discussion on help with threads communication within the C++ Programming forums, part of the General Programming Boards category; Hello, I implemented a simlpe class CTimer, which launch a thread when Start() method is called which should exit when ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    help with threads communication

    Hello, I implemented a simlpe class CTimer, which launch a thread when Start() method is called which should exit when the Stop() method is called. The thread is a simple function with a while loop whose exit condition is m_bExitFlag == true:
    Code:
    class CTimer
    {
    public:
      CTimer();
      virtual ~CTimer();
      void Stop();
      void Start();
      ....
    
    private:
      static UINT	privStartThreadWait( LPVOID pparam);
      BOOL		 m_bEndThread;		//true if stop is requested
      ....
    }
    The thread routine is this:
    Code:
    UINT CTimer::privStartThreadWait( LPVOID pparam)
    {
    	CTime		currTime;
    	CTimeSpan	diffTime;
    	CTimer		*pt;
    	
    	pt = (CTimer*)pparam;
    
    	pt->m_bEndThread = false;
    	pt->m_bThreadStopped = false;
    
    	do
    	{
    		currTime = CTime::GetCurrentTime();
    		diffTime = currTime - pt->m_startTime;
    	}while( diffTime < pt->m_ctimTimeout && !pt->m_bEndThread );
    
    	pt->m_bThreadStopped = true;
    	AfxEndThread( 0 , true );
    
    	return 0;
    }
    the Stop() method just sets m_bEndThread to true, and so the thread function should exit from the while() loop.

    THE PROBLEM: the while() loop doesn't exit after I set m_bEndThread from the main thread, calling the Stop() method. I'd like to know if there si something I should know about the interprocess communication, as for example the kind of the members used to do the communication (perhaps should be public, or protected with caller class as friend, or static or something else).

    Thank you for any hint.
    BrownB
    Last edited by BrownB; 05-22-2006 at 03:59 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    5,856
    Your information isn't really sufficient to be sure, but my guess is that making m_bEndThread volatile will fix the issue.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    thank you, I made the m_bEndThread boolean flag volatile, but this didn't help: in main thread, when I call CTimer::Stop() I set the m_bEndThread to true immediately, but the "child-thread" doesn't care and continues the loop. It only exits when the timeout is reached...

    I found on CodeGuru an implementation for a simple timer class, and it looks exactly as mine...what's wrong? Compiler options? I'm using VC++ and MFC.

    BrownB
    Last edited by BrownB; 05-22-2006 at 06:40 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    5,856
    A few obvious possibilities I can think of.

    1) Ensure the object you call Stop() for is the same as the one executing the thread.

    2) Turn off compiler optimisation: it is possible that the compiler is optimising out the check of your m_bEndThread (although it's not the intent of volatile, I would have expected setting it volatile would address that).

    3) Do things in a thread safe manner: introduce a critical section or mutex and protect accesses to the flag using that. Both the Stop() method and the thread method will need to use that protection.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21