With a preemptive multitasking OS such as windows, how do I make sure a thread such as the following has finished executing to the marked point when I suspend the thread from my main message loop?



ThreadProc
.while 1
invoke Sleep, 10
call a
call b
call c
; X <------------ end of calls
.endw
ThreadProc


If I just call SuspendThread, I can't guarantee that the thread will be at a safe position to be suspended. What's the usual framework for thread syncronization like this?
Posted on 2002-06-22 15:32:25 by grv575
I think your solution might be to share variables between threads. You can test the variable in the main thread and if set or whatever you can suspend the other thread.

check here maybe in the win32 help:

Synchronizing Execution of Multiple Threads
Posted on 2002-06-22 18:24:56 by IwasTitan
In your thread procedure,


ThreadProc
.while 1
invoke Sleep, 10
invoke EnterCriticalSection, offset cs
call a
call b
call c
invoke LeaveCriticalSection, offset cs
.endw
ThreadProc

In the main thread,


invoke InitializeCriticalSection, offset cs
; create the thread
; message loop
invoke DeleteCriticalSection, offset cs

In the msg loop,


invoke EnterCriticalSection, offset cs
; suspend the thread, it's safe now

The above code guarantees the executions of call a, call b, and call c are not separated. cs is a global variable of type CRITICAL_SECTION.

You may also use events or mutexes to achieve this. However I don't think repeatedly checking variables is a good idea as it takes just too much cpu time.:rolleyes:
Posted on 2002-06-22 21:04:07 by C.Z.