Question

Noob question:

This link shows an example of CreateEvent and CreateThread

http://msdn.microsoft.com/en-us/library/ms686915(v=vs.85).aspx

My question is if the ThreadProc is truly thread safe?

Specifically, the dwWaitResult variable. Since all threads are waiting on the same event, it turns out this code works but had different events been created, for example, this would not work correct?

Was it helpful?

Solution

The dwWaitResult variable is a local variable in that function. Thus each individual thread has its own copy, which assures that the variable is thread safe. Each thread has its own stack, therefore all local variables are specific to the individual thread.

OTHER TIPS

The Event is created by name, so if the event is already created is reused in any other thread "creating" an event with the same name. As a result, the example code IS thread safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top