Click here to Skip to main content
Click here to Skip to main content

Template Thread Library

, 26 Feb 2003
Rate this:
Please Sign up or sign in to vote.
Allows any class method to be executed in a separate thread without global functions.

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

About the Author

Emad Barsoum
Architect
United States United States
No Biography provided

Comments and Discussions

 
GeneralCWinThread Pinmemberviveking14-Jun-05 3:32 
QuestionWhy implement with templates? PinmemberMarc Clifton27-Feb-03 4:08 
AnswerRe: Why implement with templates? PinmemberEmad Barsoum27-Feb-03 6:46 
GeneralRe: Why implement with templates? PinmemberAnatoly Ivasyuk27-Feb-03 8:18 
GeneralRe: Why implement with templates? Pinmemberguyotk4-Mar-03 9:41 
GeneralRe: Why implement with templates? PinsussAnonymous31-Aug-05 8:19 
GeneralRe: Why implement with templates? PinsussHeng Wah Chiew5-Mar-03 22:18 
GeneralRe: Why implement with templates? PinmemberAnatoly Ivasyuk6-Mar-03 2:30 
GeneralRe: Why implement with templates? PinmemberEmad Barsoum6-Mar-03 6:46 
GeneralRe: Why implement with templates? PinmemberEmad Barsoum6-Mar-03 6:36 
It is better as you said to make a template parameter to specify the wait function, but it was in my project also that I want to wait certain period of time, and than time out or return that it close properly.
 
Regards,
Emad
GeneralLink is not working :) PinmemberZeruel27-Feb-03 3:47 
GeneralRe: Link is not working :) PinmemberEmad Barsoum27-Feb-03 6:48 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 27 Feb 2003
Article Copyright 2003 by Emad Barsoum
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid