Simplest C++ Multithreading with Thread classes, mutex, set, wait, lock, unlock defined
I have searched high and low for the best and simplest multi threading explained within C++. As you know, I have already posted some articles on Linux processes and how deadlocks work. Theoretically, we are trying to prevent the Dead Diamond of Death syndrome. Both Java and C++ have potential solutions but Java has all these features built in natively. C++ needs a new set of classes defined which includes a specialized Thread class.
Using a set of mutex method, we can lock certain processes from subsequent messing up the state of a current thread. An example of mutex locking a storage block of code, look at:
int g_iStorage = 0;
void StoreValue( int *pInt )
MyMutex.Lock(); //the gate keeper. only one thread
//allowed in at a time
g_iStorage = *pInt; //protected data, critical code section
MyMutex.Unlock(); //unblocks, allowing another thread to
There are some other methods which work the same way Java’s Threading class and methods. These methods are explained below:
void Set() Sets an event state to signaled, notifying the blocked thread.
BOOL Wait() Places the calling thread in a blocked state until the event state is set to signaled. Returns TRUE on success, FALSE on failure.
void Reset() Resets a signaled event to unsignaled.
event object being used by a receiving thread::
event.Wait(); // wait for an event to occur
// perform some task
event.Reset(); // reset the event to un-signaled
Example of an event object used by one thread signaling another:
// change some data
event.Set(); // notify thread that an event has occured,
// set event to signaled
There is more detailed info you could go into. I do believe if you know enough of above, you should be able to show some rudimentary knowledge in multithreading within C++. I would recommend following through with the link below as it is fairly close representation of how Java works.
To download and better understand how all this Thread classes work, go to http://www.codeproject.com/KB/threads/threadobject.aspx
It also contains a very good end to end set of source files to generate your own Thread classes which include the mutexes explained.