Threads are not difficult, just remember that global vars that are NOT threaded, and all global Queues need to be wrapped with a synch object of your choice, to ensure data corruption doesnt occur, and the above point about Resuming Started threads if the procedure doesnt have an accept loop, which all windows procedures have by default but source procedures like your example doesnt have an Accept loop.
Now I havent tried, but adding an Accept loop to a source procedure should remove the requirement to use Resume, but dont quote me on that.
The choice of synch objects can be found in the help docs.
Synchronization Objects
Whilst Critical Sections are the suggested form of locking, because it doesnt give feedback namely it doesnt state if a lock has been achieved you’ll have to have a global var which is used to control the lock.
More info here on the Critical Section and Wait Objects.
The other interesting thing I have noticed when using Critical Sections is how the OutputDebugStringA appears in debug view.
When running multiple threads with a Critical Section and using OutputDebugStringA, you’ll see one or two lines from each thread appear in debugview and then a thread switch occurs.
When running multiple threads without a CriticalSection and using OutputDebugStringA, you’ll see a much larger batch of OutputDebugStringA strings in debugview before a thread switch occurs.
Now I wonder what the overheard is switching from one thread to another when using CriticalSections, but I dont know if the thread is on one core and the other thread might be on another core, which could reduce the overhead if data is stored in the core cache. They can do some fancy premptive things using core cache.
When not using CriticalSections and seeing the batch of OutputDebugStringA output it would suggest more code from a thread can run before a thread switch occurs. Likewise CPU scheduling can also be used to alter how many OutputDebugStringA appear in debugview before a thread switch occurs. Having CPU Scheduling set for Programs (workstations) or Background Services (Servers) is what can influence the thread switching.
From a debug perspective using debugview, having CriticalSections switched off can be easier to follow and thus debug before switching critical sections on, but debugview does colorise threads if the thread number is included in the OutputDebugStringA string, which can make it easier to follow in debugview.
Local Queues are thread safe if defined in the procedure. Not sure on the module or the routine because I remember Alexey saying there was a compiler switch/pragma which could alter the scope of routine declared variables. ***
Module declared queue’s I’ve never used, but should be thread safe as these are contained within a thread.
Its OS thread switching which potentially** causes the problem with unthreaded variables and queues.
**I say potentially because I would imagine Microsoft tries to avoid problems with apps using true threads and no global var locking synch objects.
*** Found the post I was thinking of.
Search for: Implicit Variables - Big Mistake or Bad Idea?