473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

running class method as a thread proc

Hi,
Lets say I have a method called "listen_proc" inside "class1". There is
another method called "start" in the same class that has to start the
"listen_proc" inside a new thread. I am using CreateThread but I cannot
manage to pass the right function pointer, the compiler keeps on giving
error that it cant convert my given function to LPTHREAD_START_ROUTINE while
I try to cast it. In a simple c++ non-class file I can easily do it but this
class method prototype mismatch is causing this problem. Can someone plz
write a little example that has a method inside a class that is started in a
seperate thread using CreateThread.

Thanks,

-Ab.
Dec 21 '05 #1
2 3867
Abubakar wrote:
Hi,
Lets say I have a method called "listen_proc" inside "class1". There
is another method called "start" in the same class that has to start
the "listen_proc" inside a new thread. I am using CreateThread but I
cannot manage to pass the right function pointer, the compiler keeps
on giving error that it cant convert my given function to
LPTHREAD_START_ROUTINE while I try to cast it. In a simple c++
non-class file I can easily do it but this class method prototype
mismatch is causing this problem. Can someone plz write a little
example that has a method inside a class that is started in a
seperate thread using CreateThread.


The problem is that you cannot convert a pointer to non-static member
function into an ordinary pointer to function, which is what CreateThread
wants.

The canonical solution is to write a static "trampoline" function that in
turn calls your non-static function:

class Active
{
private:
HANDLE m_hTthread;
static DWORD WINAPI ThreadStart(LPVOID info)
{
return static_cast<Active*>(info)->Run();
}

DWORD Run()
{
// your thread proc here
}

public:
Active()
m_hThread(INVALID_HANDLE_VALUE)
{
}

~Active()
{
// take appropriate steps to destroy/wait for your thread to
terminate
}

void Start()
{
m_hThread = CreateThread(
NULL,0,&Active::ThreadStart,this,0,NULL
);
}
};

Note that 'this' is passed to the 'lpParameter" parameter of CreateThread,
allowing the trampoline function to recover the this pointer and call your
non-static thread proc.

Note also that if you're linking to the static CRT, you should use
_beginthread or _beginthreadex instead of CreateThread. If you're linking
to the DLL verison of the CRT then it doens't matter which you use.

-cd

Dec 21 '05 #2
Thanks, that worked!

Ab.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:O5*************@TK2MSFTNGP11.phx.gbl...
Abubakar wrote:
Hi,
Lets say I have a method called "listen_proc" inside "class1". There
is another method called "start" in the same class that has to start
the "listen_proc" inside a new thread. I am using CreateThread but I
cannot manage to pass the right function pointer, the compiler keeps
on giving error that it cant convert my given function to
LPTHREAD_START_ROUTINE while I try to cast it. In a simple c++
non-class file I can easily do it but this class method prototype
mismatch is causing this problem. Can someone plz write a little
example that has a method inside a class that is started in a
seperate thread using CreateThread.


The problem is that you cannot convert a pointer to non-static member
function into an ordinary pointer to function, which is what CreateThread
wants.

The canonical solution is to write a static "trampoline" function that in
turn calls your non-static function:

class Active
{
private:
HANDLE m_hTthread;
static DWORD WINAPI ThreadStart(LPVOID info)
{
return static_cast<Active*>(info)->Run();
}

DWORD Run()
{
// your thread proc here
}

public:
Active()
m_hThread(INVALID_HANDLE_VALUE)
{
}

~Active()
{
// take appropriate steps to destroy/wait for your thread to
terminate
}

void Start()
{
m_hThread = CreateThread(
NULL,0,&Active::ThreadStart,this,0,NULL
);
}
};

Note that 'this' is passed to the 'lpParameter" parameter of CreateThread,
allowing the trampoline function to recover the this pointer and call your
non-static thread proc.

Note also that if you're linking to the static CRT, you should use
_beginthread or _beginthreadex instead of CreateThread. If you're linking
to the DLL verison of the CRT then it doens't matter which you use.

-cd

Dec 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: atlantix | last post by:
How do you execute a .BAT file from a C# WinForm? An example would be greatly appreciated. Thanks, atlantix
1
by: Dan | last post by:
Good Day, I have a Windows Service that is responsible for processing somes files. One method of the service invokes another method which may take a while to complete its work. Something like...
2
by: Jeffrey Palermo, MCAD.Net | last post by:
I have some classes that control file processing in c#. The files come from a mainframe and may take some time for each to process. I use the Threadpool to process multiple files at once. Each...
3
by: Bob Day | last post by:
VS 2003, vb.net, sql msde... The help is pretty empatic that you cannot pass parameters to a thread. The sample below is from help, showing you to set up variables in the TasksClass, and assign...
17
by: rory | last post by:
Hi everyone. I'm trying to declare a function as a friend in one of my classes. Correct me if I'm wrong but if I declare a friendly function that means that I can access member of my class in that...
2
by: Mike P | last post by:
I am programmatically starting and ending a local instance of Outlook : //programmatically start local Outlook System.Diagnostics.Process proc = new System.Diagnostics.Process();...
5
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc =...
6
by: tvaughan77 | last post by:
Hi, I have some code that I want to use to run a command line utility and I want to be able to run it from an aspx page running under IIS. The command line utility is a local utility running on...
1
by: amgamg | last post by:
Dear experts, you help is necessary! When trying to compile this code an error encounted. I think that thread in class is good idea for c++, so if you know what`s wrong, please post your opinion!...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.