Click here to Skip to main content
15,916,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I am new to multithreading. I was trying controlling threads using EVENT Objects. FYI i have single process and multiple threads.

I could achieve the same results without using events. Instead i used global flags to achieve the same.I can totally understand that using global variable is not a good practice.

Can anybody tell, what else is a major difference and if anybody can give some specific examples where only events can be used.

Thanks in advance
Philippe Mori 18-Jun-15 12:36pm    
Read a book or a tutorial on threading first to learn basic concept.

1 solution

The differences are in the way that the access to resources is guarantee, and CPU usage.
If you use a global flag you have to guarantee that the access is made by a single thread at time. Imagine that your first thread read the flag, then while it is evaluating the status the executing thread is switched in favor of another thread that read also the same flag and set it. The last thread now thinks that it owns the resource and act dependently. The former thread, when get executing again, will do the same. The resullt is that the resource is trashed by contemporary access by both threads.
This is normally solved using bus locking instructions that reads and sets the flags in a single bus cycle and guarantee the access to a single thread.
Now the difficult part: how your software checks that the resource is available?
The easier solution you can find is to poll the flag status until it is ready. This is also the worst one because you waste CPU in a loop checking until the flag is released. In fact the thread is always running and consuming all its execution slice polling the flags (what a waste... :( ).
Write your program in this way and then check CPU usage, and you'll get a very bad surprise: you will see that your sample program that makes almost nothing use almost all available CPU...
Event objects are much more complex, flexible and efficient. An event object is an OS object, and interacts directly with task scheduling (threads and fibres scheduling).
When you setup an event object the OS takes care to guarantee private access to the flag, then stops thread execution sparing CPU. This is very efficient because the OS don't even restart thread execution until the resource is not available.
When you will approach advanced programming you'll find also some interesting points related to task priority. Usage of event objects makes fully effective the priority mechanism transferring execution to high priority task as soon as resource is available.

All software running on a machine use event objects, and because each task needs access to shared resources (I/O on disks and devices), most of the time they are in idle waiting for event. For this reason looking at the task manager you'll see many programs running and the idle process at 70-99% ;)
Share this answer
_Asif_ 18-Jun-15 8:15am    
Frankie-C 18-Jun-15 10:52am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900