| View previous topic :: View next topic |
| Author |
Message |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Tue Feb 12, 2008 1:35 pm Post subject: [C++Classes] CreateThread with member. |
|
|
Having trouble making a class execute CreateThread with the routine being a member of itself.
Something like...
| Code: | void class::ExecuteThread( void ){
classHandle = CreateThread( NULL, NULL, cThread, NULL, NULL, &cTID );
}
void WINAPI class::cThread( LPVOID lpParameter ){
//blah.
} |
It says to use &class::cThread, but that just results in a "There is no context in which this conversion is possible" error.
The lpStartAddress parameter can't accept the member since a members' this pointer is not available.
Any ideas on a solution?
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Feb 12, 2008 2:10 pm Post subject: |
|
|
Iono if I got your right, but.. Umm.. | Code: | #include <iostream>
#include <windows.h>
class test {
public:
void ExecuteThread();
static void Thread( LPVOID lpParam ) { std::cout << "Thread says hello world!"; }
};
int main( int argc, char **argv )
{
test thingy;
thingy.ExecuteThread();
::Sleep(1000); // I wanna see the output :P
return EXIT_SUCCESS;
}
void test::ExecuteThread()
{
::CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)Thread, NULL, NULL, NULL);
} |
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Tue Feb 12, 2008 2:17 pm Post subject: |
|
|
| Close, but with static, it has problems accessing the variable members that aren't static. Once changed to static, linker errors.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Feb 12, 2008 2:41 pm Post subject: |
|
|
k, so no more clean solutions :P My suggestion is that you use a static launcher? | Code: | #include <iostream>
#include <windows.h>
class test {
public:
void ExecuteThread();
static void ThreadLauncher( LPVOID lpParam ) { ((test*)lpParam)->Thread(); }
void Thread() { std::cout << "Thread says hello world!"; }
};
int main( int argc, char **argv )
{
test thingy;
thingy.ExecuteThread();
::Sleep(1000); // I wanna see the output :P
return EXIT_SUCCESS;
}
void test::ExecuteThread()
{
::CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadLauncher, this, NULL, NULL);
} |
|
|
| Back to top |
|
 |
Renkokuken GO Moderator
Reputation: 4
Joined: 22 Oct 2006 Posts: 3249
|
Posted: Tue Feb 12, 2008 3:14 pm Post subject: |
|
|
Heh, I just did so and checked back to see the same solution.
Thanks for your help, Jani. <3~
|
|
| Back to top |
|
 |
|