Click here to Skip to main content
15,868,349 members
Articles / Desktop Programming / MFC
Article

Threads and shared resources

Rate me:
Please Sign up or sign in to vote.
2.56/5 (6 votes)
31 Aug 20032 min read 52.5K   1.1K   17   2
The objective of this article is to demonstrate the result of common access to a resource by different threads

Introduction

This article illustrates that when a common resource is accessed by more than one thread its value can be affected by all the threads during the activation time of the threads and so the resource should be thread safe (i.e. it should be used by only one thread at a time).

Using shared resource

Whenever a resource is accessed by different threads it should be thread safe in order to get correct results. In programming a shared resource can be a memory location (allocated for a variable), which can be accessed by one or more threads and it's possible to read a wrong value from the memory location when the threads are running. Although threads do not run at the same time when a code operation is made op of three or more instructions, due to the scheduled time for threads, the processor may stop at the middle of an operation (for example during an increment operation) in order to continue code execution belonging to other threads. If then another thread gets the processor and begins to access or check the shared resource, it will read a wrong value.

Critical Section

A solutions is to block access to the shared resource if it is already in use by another thread and unblock the access when the thread is finished to operate on the shared resource. A critical section is a mechanism that limits access to a certain resource to a single thread in an application. A thread enters the critical section before it needs to work the specific shared resource and then exits the critical section after it is finished accessing the resource. When a code section is marked by critical section only one thread at a time is allowed to execute it.

About the Demo example

In the demo example the device context (DC) object has been used as a shared resource among 5 different worker threads, which try to paint the client area of the application's main window with different colors, and it shows that the result is a multi-colored window when the threads run without being blocked. In order to make the DC thread safe an object of CCriticalSection class is used to block other threads if it is already in use by another thread. The threads can be locked and unlocked by the Lock() and Unlock() member functions of the critical section object.

The demo example shows that if the DC is thread safe, then the background will be painted by only one thread at a time and the final background color is made by the last thread and is made up of just one color.

The following image shows this situation:

If the DC is not thread safe every thread will have access to it during the activation time of threads and each thread tries to paint the client area of the window with its color and so the final background color will be made up of different colors.

The following image shows this situation:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
fjj1989103031-Jan-13 21:29
fjj1989103031-Jan-13 21:29 
Generalthanx Pin
Maxim Yudin14-Dec-06 5:04
Maxim Yudin14-Dec-06 5:04 
it's excellent article for beginner Smile | :) thank you Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.