Threads synchronization problem
-
There are to threads: //thread one: CSingleLock m_lock(&syn_object); m_lock.Lock(); /* .....Doing something */ m_lock.UnLock(); /================================= thread two: CSingleLock m_lock(&syn_object); m_lock.Lock(); while (!m_lock.IsLocked()); //continue to do something if thread one finished with thre resource I want the thread two to wait for the thread one finished 'doing something' before continue but the whole program seems to crash here.
-
There are to threads: //thread one: CSingleLock m_lock(&syn_object); m_lock.Lock(); /* .....Doing something */ m_lock.UnLock(); /================================= thread two: CSingleLock m_lock(&syn_object); m_lock.Lock(); while (!m_lock.IsLocked()); //continue to do something if thread one finished with thre resource I want the thread two to wait for the thread one finished 'doing something' before continue but the whole program seems to crash here.
lisoft wrote: m_lock.Lock(); while (!m_lock.IsLocked()); Your will always hang on the
while
line as you just lockedm_lock
on the previous line. There is no need to check the lock state as the code will wait on them_lock.Lock()
line until thread one has unlocked the lock. Remove thewhile
and your code should work better.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
-
There are to threads: //thread one: CSingleLock m_lock(&syn_object); m_lock.Lock(); /* .....Doing something */ m_lock.UnLock(); /================================= thread two: CSingleLock m_lock(&syn_object); m_lock.Lock(); while (!m_lock.IsLocked()); //continue to do something if thread one finished with thre resource I want the thread two to wait for the thread one finished 'doing something' before continue but the whole program seems to crash here.
Your
while (!m_lock.IsLocked());
will never execute because m_lock will always be locked at that step. Your request cries out for a manual reset event but it looks like you are using something other than an event. John
-
Your
while (!m_lock.IsLocked());
will never execute because m_lock will always be locked at that step. Your request cries out for a manual reset event but it looks like you are using something other than an event. John
-
What should I do to let thread two waiting until the thread one finish? :confused::confused::confused:
If I remember right the lock function does not return until it has managed to lock. So checking it is lock after call lock is redundant. The purpose of locking is to allow a thread to manipulate the data with messing up another threads actions. In other words only one thread can read or modify the data at a given time. You do not want to try to copy a string while anouther thread is modifying that same string. So the answer is YES! Before thread two can work with the data thread one has to stop working with it. Before thead one can work with the data thread two has to stop working with it. The individual threads do not have to stop they just have to stop accessing the shared data long enough for anouther thread to access it. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
What should I do to let thread two waiting until the thread one finish? :confused::confused::confused:
You do: Somewhere define an event that both threads know about: HANDLE hEvent = ::CreateEvent(NULL, TRUE, FALSE, NULL); which is a manual reset event with unsignalled state to begin with. Then in your worker thread: // Do something, stuff, thing or whatever... // Ok Ready, signal the event ::SetEvent(hEvent); Then in thread 2 you just wait for the event // Wait for event to be signalled ::WaitForSingleObject(hEvent, INFINITE); Execution will continue after the ::WaitForSinglObject as soon as the event gets signalled.