Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C++
Article

Template Thread Library

Rate me:
Please Sign up or sign in to vote.
3.67/5 (15 votes)
26 Feb 20032 min read 75.5K   1K   20   12
Allows any class method to be executed in a separate thread without global functions.

Image 1

Introduction

This project demonstrate two non MFC thread classes: one of them is a template library that can make any member function of any class executed in different thread, without requiring that you define any static or global functions; the other class requires inheritance in the same way that CWinThread under MFC work but without any MFC dependency. Both classes use _beginthreadex.

Details

First the template thread library takes in the constructor a pointer to a class and a point to one of its member functions. The member function must take no parameters and return void as the following:

class ThreadTest1<BR>{<BR>public:<BR>    void InsideThread()
    {<BR>        for(int i=0;i<5;i++)
        {<BR>            cout << "ThreadTest1: " << i 
                << endl;<BR>        }<BR>    }<BR>};

This is a very simple class that have a single member function. To run this member in a different thread use the TThread class as follow:

// First create an instance of the class that has the required member<BR>// function
ThreadTest1 test1;
TThread<ThreadTest1> thTest1(test1,&ThreadTest1::InsideThread);

// The constructor takes 2 parameters and a third optional parameter,
// the first is an object of the class and the second is pointer to one
// of its member functions. The third optional parameter is the priority of
// the thread, the default is Normal
thTest1.StartAndWait();

// This member actually starts the thread and waits until the thread begins
// execution. Wait until the thread terminates; optionally you can
specify the number of milliseconds to wait.
thTest1.WaitUntilTerminate();

The TThread class has a lot of member functions to increase and decrease the priority, terminate the thread immediatly and check the status of the thread:

TThread
The Constructor take 3 parameters, the first is an instance of a class that contain the member function that we want to run it in different thread, the second parameter is a pointer to the member function, the third one which is optional is the priority of the thread the default is Normal.
WaitUntilTerminate
Wait until the thread is terminate also you can define how many milli second to wait the default is infinite, this member doesn't terminate the thread, if the thread didn't terminate at the specified number of msec it will return false, otherwise it will return true.
Start
Start the thread and return immediatly.
StartAndWait
Start the thread and wait until it begin executing.
Pause
Suspend the thread, you need to call Start again to resume the thread.
IsRunning
Return true if the thread running.
IsTerminated
Return true if the thread is terminated.
IsSuspend
Return true if the thread is suspended.
SetPriority and GetPriority
Set the priority to a specific value, or get the current priority of the thread, the thread must be in running mode before calling any of these members.
SpeedUp
Increase the thread priority to one step, the thread must be in running mode before calling any of these members.
SlowDown
Decrease the thread priority to one step, the thread must be in running mode before calling any of these members.
Terminate
Terminate the thread immediatly, Not Safe!!!.

Second, in the CThread class, which is like MFC's CWinThread, you must overload the OnRunning method:

class ThreadTest2:public CThread<BR>{<BR>public:<BR>    void OnRunning()<BR>    {<BR>        for(int i=0;i<5;i++)<BR>        {<BR>            cout << "ThreadTest2: " << i << endl;<BR>        }<BR>    }<BR>};<BR>

Just create an instance of this class and call Start() or the StartAndWait() method:

ThreadTest2 test2;<BR><BR><BR><BR>// This member actually starts the thread and waits until the thread begins<BR>// execution<BR><BR><BR><BR><BR><BR><BR>test2.StartAndWait();<BR><BR><BR>// Wait until the thread terminates; optionally you can specify the number<BR>// of milliseconds to wait<BR><BR><BR>test2.WaitUntilTerminate();
The other members of CThread are exactly like TThread.

Note: To pass any parameters to the thread, you just need to define a member variable in the thread class before calling the Start() or StartAndWait() member functions, and you can use this variable inside the thread; just make sure that no two threads access the same variable at the same time by using any locking techniques.

History

  • Date Posted: February 19, 2003

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
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralLink is not working :) Pin
Zeruel27-Feb-03 3:47
Zeruel27-Feb-03 3:47 
GeneralRe: Link is not working :) Pin
Emad Barsoum27-Feb-03 6:48
Emad Barsoum27-Feb-03 6:48 

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.