![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
|
Class threads
I need to create a thread inside a class that allows the programmer to access the member functions. The way I declare my thread, though, I don't think it's possible to create a "member thread", or whatever, how I want to. Here's how I initialize the thread
DWORD WINAPI RecvThread(LPVOID lpParameter); Whenever I try to use an access member scopre like so: DWORD WINAPI Client::RecvThread(LPVOID lpParameter)
{
//do stuff
}I'm told I can't do that. Does anyone have a clue how I would create a class thread? The only other solution I've had was to create the class object globally so that the thread could receive while another function was sending messages, which everyone knows, declaring globally is wrong.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Use a "normal" function as the thread function, but have it immediately call a member function in your class to do the work.
Pass the class object in the lpParameter value and cast it to the class E.g.
class MyThreadClass
{
public:
DWORD RecvThread();
private:
// blah blah
}
DWORD Client::RecvThread()
{
//do stuff
}
DWORD WINAPI RecvThread(LPVOID lpParameter)
{
MyThreadClass *ptr = static_cast<MyThreadClass *>(lpParameter);
return ptr->RecvThread;
}If you don't care about the return value from the member function RecvThread, you can make it void and make the wrapper function return 0 (or whatever). Edit: You could also make the wrapper function a static member of the class, whatever floats your boat. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Minor correction to Dark's example. The thread function should be something like;
DWORD WINAPI RecvThread(LPVOID lpParameter)
{
MyThreadClass *ptr = static_cast<MyThreadClass *>(lpParameter);
return ptr->RecvThread(); // calling a member function, not returning a pointer to member
} |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
Oops :o
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|