Skip to main content

Multi-Threading in Windows

  1. there are two ways to program with multiple threads: use the Microsoft Foundation Class (MFC) library or the C run-time library and the Win32 API.

What is threading ?

A thread is basically a path of execution through a program. It is also the smallest unit of execution. A thread consists of a stack, the state of the CPU registers, and an entry in the execution list of the system scheduler. Each thread can shares all the process's resources.

A process consists of

1. one or more threads

2. the code,

3. data,

4. other resources of a program in memory.

Ex. open files, semaphores, dynamically allocated memory.

A program executes when the system scheduler gives one of its threads execution control. The scheduler determines which threads should run and when they should run. Threads of lower priority might have to wait while higher priority threads complete their tasks. On multiprocessor machines, the scheduler can move individual threads to different processors to balance the CPU load.

Each thread in a process operates independently. Unless you make them visible to each other, the threads execute individually and are unaware of the other threads in a process. Threads sharing common resources, however, must coordinate their work by using semaphores or another method of inter-process communication.

Library Support – all versions of CRT –c run time libraries supports with the exception of the non-locking versions of some functions

Include Files for Multithreading

Standard include files declare C run-time library functions as they are implemented in the libraries. If you use the Full Optimization (/Ox) or fastcall Calling Convention (/Gr) option, the compiler assumes that all functions should be called using the register calling convention. The run-time library functions were compiled using the C calling convention, and the declarations in the standard include files tell the compiler to generate correct external references to these functions.

C Run-Time Library Functions for Thread Control

All Win32 programs have at least one thread. Any thread can create additional threads. A thread can complete its work quickly and then terminate, or it can stay active for the life of the program.

The LIBCMT and MSVCRT C run-time libraries provide the following functions for thread creation and termination: _beginthread, _beginthreadex and _endthread, _endthreadex.

The _beginthread and _beginthreadex functions create a new thread and return a thread identifier if the operation is successful. The thread terminates automatically if it completes execution, or it can terminate itself with a call to _endthread or _endthreadex.

NoteNote

If you are going to call C run-time routines from a program built with Libcmt.lib, you must start your threads with the _beginthread or _beginthreadex function. Do not use the Win32 functions ExitThread and CreateThread. Using SuspendThread can lead to a deadlock when more than one thread is blocked waiting for the suspended thread to complete its access to a C run-time data structure.

The _beginthread and _beginthreadex Functions

The _beginthread and _beginthreadex functions create a new thread. A thread shares the code and data segments of a process with other threads in the process but has its own unique register values, stack space, and current instruction address. The system gives CPU time to each thread, so that all threads in a process can execute concurrently.

_beginthread and _beginthreadex are similar to the CreateThread function in the Win32 API but has these differences:

· _beginthread and _beginthreadex let you pass multiple arguments to the thread.

· They initialize certain C run-time library variables. This is important only if you use the C run-time library in your threads.

· CreateThread helps provide control over security attributes. You can use this function to start a thread in a suspended state.

_beginthread and _beginthreadex return a handle to the new thread if successful or an error code if there was an error.

The _endthread and _endthreadex Functions

The _endthread function terminates a thread created by _beginthread (and similarly, _endthreadex terminates a thread created by _beginthreadex). Threads terminate automatically when they finish. _endthread and _endthreadex are useful for conditional termination from within a thread. A thread dedicated to communications processing, for example, can quit if it is unable to get control of the communications port.

Comments

Popular posts from this blog

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.