Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2006, 10:42 PM   #1
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Apr 11th, 2006, 12:32 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old Apr 11th, 2006, 4:23 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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
}
grumpy is offline   Reply With Quote
Old Apr 11th, 2006, 5:54 AM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Oops :o
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC