Remember Me
forgot your password?

Synchronizing Threads in Windows

Synchronizing threads in Windows
======================

BY DR.WAEL ALBAYAYDH
======================

Abstract
----------
A thread describes a path of execution within a process. Every time a process in initialized, the system creates a primary thread. This thread starts at the C runtime's start-up code, which in turn calls the WinMain function and continues executing until the WinMain function returns and the C runtime's start up code calls ExitProcess. For many applications, this primary thread is the only one the application requires. However, processes can create additional threads to help them do their work. The whole idea behind creating additional threads is to utilize as much of the CPU's power as possible.

Thread synchronization:
---------------------------

As we know, to run threads, we need to schedule them. In order to run them effectively, they need to be synchronized. Suppose one thread creates a brush and then creates several threads that share the brush and draw with it. The first thread must not destroy the brush until the other threads finish drawing. This requires a means of coordinating the sequence of actions in several threads.
One way is to create a global Boolean variable that one thread uses to signal another. The writing thread will set this parameter to TRUE and the reading thread might loop until it sees the flag change. This will definitely work, but the looping thread wastes a lot of processor time.
Instead, Win32 supports a set of synchronization objects such as mutexes, semaphores, events and critical sections. These are the system objects created by the object manager. All of them will work in a similar way. A thread that wants to perform some coordinated action waits for a response from one of these objects and proceeds only after receiving it. The scheduler removes the waiting objects from the dispatch queue so that they will not consume processor time
It is important to keep in mind that:
• A mutex object works like a narrow gate for one thread to pass at a time.
• A semaphore object work like a multi-lane gate that a limited number of threads can pass through together.
• An event object broadcasts a public signal for any listening thread to hear.
• A critical section object works like a mutex but only within a single process.
Mutexes, semaphores and events can coordinate threads in different processes, but critical sections are only visible to threads in a single process.
Mutexex: These are very much like critical sections except that they can be used to synchronize data across multiple processes. To do this, a thread in each process must have its own process-relative handle to a single mutex object.
Semaphores: These objects are used for resource counting. They offer a thread the ability to query the number of resources available; if one or more resources are available, the count of available resources is decremented. Thus semaphores perform the test and set operations automatically, that is, when you request a resource from a semaphore, the operating system checks whether the resource is available and decrements the count of the available resources without letting another thread interfere. Only after the resource count has been decremented does the system allow another thread to request a resource.
For example, let us say that a computer has three serial ports. No more than three threads can use the serial ports at any given time; each port can be assigned to one thread. This situation provides a perfect opportunity to use a semaphore. To monitor serial port usage, you can create a semaphore with a count of three - one for each port. A semaphore is signaled when its resource count is greater than zero and is non-signaled when the count is zero.
Because several threads can affect a semaphore's resource count, a semaphore, unlike a critical section or mutex, is not considered to be owned by a thread. This means that it is possible to have one thread wait for the semaphore object and another thread release the object.
Events: Even objects are the most primitive form of synchronization objects and they are quite different from mutexes and semaphores. Mutexes and semaphores are usually used to control access to data, but events are used to signal that some operation has been completed.
There are two different types of event objects - manual reset events and auto reset events. A manual reset event is used to signal several threads simultaneously to say that an operation has finished, and an auto reset event is used to signal a single thread to say that an operation has been completed.
Events are most commonly used when one thread performs initialization work and, when it finishes, signals another thread to perform the remaining work. The initialization thread sets the event to the non-signaled state and begins to perform the initialization. Then, after the initialization has been completed, the thread sets the event to the signaled state. Waiting for the event, the worker thread wakes up and performs the rest of the work.
For example, a process might be running two threads. The first thread reads data from a file into a memory buffer. After the data has been read, the first thread signals the second thread that it can process the data. When the second thread finishes processing the data, it might need to signal the first thread again, so that the first thread can read the next block of data from the file.
Critical sections: A critical section is a small section of the code that required exclusive access to some shared data before the code can execute. Of all synchronization objects, critical sections are the simplest to use, but they can be used to synchronize threads only within a single process. Critical sections allow only one thread at a time to gain access to a region of data.

Dr.Wael Al Bayaydh

Dr.Wael AlBayaydh has a PhD degree in computer engineering. He has been working in information technology for several years, concentrating on areas such as operating system, networking, network security, electronic commerce, Internet services, LDAP and Web servers. AlBayaydh has authored a number of articles for trade publications, and he presents his own papers at industry conferences. He can be reached at wr_y@hotmail.com

Rate this Article: 0 / 5 stars - 0 vote(s)
Print Email Re-Publish

Add new Comment



Captcha

  • Latest Information Technology Articles
  • More from Dr.Wael Al Bayaydh

How to Fix a Computer or PC that is Not Booting/Starting Up? How Can I Fix my PC Without Having a Boot CD? Where Can I Get a Boot CD for Windows?

By: Tom Parks | 30/11/2009
How to Fix a Computer or PC that is Not Booting Up? How Can I Fix my PC Without Having a Boot CD? What Should I Do if I Don't Have a Boot Disc? Where Can I Get a Boot CD for Windows?

Companies Go Green By Getting in The Cloud

By: Richard Alessandro | 30/11/2009
Cloud Computing helps Companies go green.

Choosing an Industrial Display

By: Richard n Williams | 30/11/2009
Using computer monitors and LCD displays in industrial environments can encompass some challenges that are not necessary to consider when choosing a monitor for the office or home.

How to Make Your Own Biodiesel - Homemade Biodiesel Guide

By: Colby Marks | 30/11/2009
How to Make Your Own Biodiesel? We'll first of all why do people end up going with biodiesel. Reasoning being is because it is has more advantages according to "Joseph Then" the create of the amazing e book "how to make biodiesel." Learning how to Make your own biodiesel is simple and easy. Only if you follow the step by step instructions then this will help benefit you. If done in the right manner. However if you are looking for an alternative fuel then making biodiesel is the p...

Questions You Need To Answer Before Buying A POS System

By: Coy Mcdaniel | 30/11/2009
Making it an option to buy a POS system for your business may seem to be so simple especially if you have all the money to spare. If you can afford one or more because you have more than enough for your financial needs then you might consider purchasing without spending a lot of time thinking and weighing your options. You will not be worrying about your expenses. After all, you have all the money you need. H...

Can Ubuntu Reach Over 16,000 Anime Lovers In April

By: Jose Matheny | 30/11/2009
Ubuntu really is a perfect candidate for the new operating system of choice for all anime lovers, and that is exactly what we aim to make it. Is there a more passionate and tight-knit, yet diverse and open minded cultural phenomenon that we could target than the widespread anime fandom.

How To Play BlackJack Tips and Strategies Online

By: Bob Mason | 30/11/2009
If you want to win at blackjack, you've come to the right place! Blackjack is one of the most popular casino games for a good reason. It is one of the only games you can play and actually have an advantage. Here's how to win.

Touch Screen LCD Phones Are In

By: Coy Mcdaniel | 29/11/2009
A touch screen LCD is one of the many features that most shoppers are now looking for at a mobile phone when they go shopping. Well, how about you? What are your ideals when it comes to choosing a particular phone model over another? What are some of the phone specifications that could fascinate you and make you decide to buy? What particular brand of mobile phones that you consider hot and therefore more popular? The simplistic ...

Self Tuned Networks

By: Dr.Wael Al Bayaydh | 14/09/2009 | Communication
This is about a real revolution in the field of RF signal optimization and RF planning. when it comes to the best spectral efficiency, then STN will say loudly, I DO IT.

Time Traveling Is Possible!!

By: Dr.Wael Al Bayaydh | 21/02/2009 | Science
Time is relative, time is not fixed, it is moving, running, walking, or stopped!!. The River of time is an interesting issue for any scientist to study and travel in its world.

Wireless Road Traffic Management System

By: Dr.Wael Al Bayaydh | 24/02/2008 | Technology
The 5-year research and development programme of WRTMS is aimed to promote open systems that support future road traffic management policies, procedures, and measures, both in Jordan, Gulf Area, and overseas. The programme comprises a number of projects investigating various aspects of road traffic control systems infrastructure and applications.

Why Data Loss ?!===> Hw or System Malfunction

By: Dr.Wael Al Bayaydh | 29/01/2008 | Information Technology
When you lose the information, which was once accessible is referred as data loss. The threats to data loss may come in many different forms, from a simple mistake to a massive natural disaster Since, we now know what data loss is; let us read on to find out as to what causes data loss to occur and what measures do we need to follow in order to prevent that from happening.

Multiprocessor Semaphore

By: Dr.Wael Al Bayaydh | 29/01/2008 | Information Technology
Shared memory semaphores are basic tools for interprocessor synchronization. Although self-imposed design constraints can often reduce synchronization requirements, semaphores offer significant flexibility to multiprocessor system designers. The implementation presented here illustrates some fundamental issues of multiprocessor concurrency and demonstrates the tremendous value of a multitasking OS like DSP/BIOS.

Synchronizing Threads in Windows

By: Dr.Wael Al Bayaydh | 29/01/2008 | Information Technology
A thread describes a path of execution within a process. Every time a process in initialized, the system creates a primary thread. This thread starts at the C runtime's start-up code, which in turn calls the WinMain function and continues executing until the WinMain function returns and the C runtime's start up code calls ExitProcess

Submit Your Articles Free: Signup
Article Categories




Use of this web site constitutes acceptance of the Terms Of Use and Privacy Policy | User published content is licensed under a Creative Commons License.
Copyright © 2005-2008 Free Articles by ArticlesBase.com, All rights reserved. (0.06, 1, w1)