Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mdias

Thread inside object

This topic is 5689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m trying to create a thread inside a object like this:
void CObject:_CreateThread()
{
    HANDLE Thread;
    DWORD  ThreadID;
    Thread = CreateThread( NULL, 0, p_ThreadMain, NULL, 0, &ThreadID );
}

DWORD WINAPI CObject:_ThreadMain(void* Data)
{
   return 0;
}
 
but I get this error when compiling:
quote:
error C2664: ''CreateThread'' : cannot convert parameter 3 from ''unsigned long (void *)'' to ''unsigned long (__stdcall *)(void *)'' None of the functions with this name in scope match the target type
Why do I get this error ? BTW: How do you close threads form other threads ?
KaMiKaZe

Share this post


Link to post
Share on other sites
Advertisement
CreateThread expects a pointer to a "regular" function, while you are passing a member-function, which is a different cattle of fish altogether.
You''ll either need to use a non-member function, or a static member-function.
Seeing as you don''t actually pass a parameter to the thread-routine consider changing it like this:

  
void CObject::CreateThread() {
HANDLE thread;
DWORD threadId;
thread = CreateThread ( NULL, 0, ThreadMain, reinterpret_cast<void*>(this), &threadId );
}

DWORD CObject::RunThread ( void ) {
/* Put the actual code you want to run threaded here */
return 0;
}

static DWORD WINAPI CObject::ThreadMain ( void* data ) {
CObject* obj = reinterpret_cast<CObject*>(data);
return obj->RunThread();
}



As for your second question I believe your only course of action (short of calling TerminateThread (which you should avoid at all costs)) is to monitot a flag, and have the
thread terminate if the flag is set.


  
void CObject::CreateThread() {
HANDLE thread;
DWORD threadId;
thread = CreateThread ( NULL, 0, ThreadMain, reinterpret_cast<void*>(this), &threadId );
}

DWORD CObject::RunThread() {
this->shouldTerminate = false;
while ( !shouldTerminate ) {
/* Put the actual code you want to run threaded here */
}
return 0;
}

void CObject::Terminate() {
this->shouldTerminate = true;
}

static DWORD WINAPI CObject::ThreadMain ( void* data ) {
CObject* obj = reinterpret_cast<CObject*>(data);
return obj->RunThread();
}



-Neophyte

Share this post


Link to post
Share on other sites
One last question about threads, do I need to call CloseHandle
to avoid memory leaks or ExitThread is enough ??


KaMiKaZe

Share this post


Link to post
Share on other sites
You should call CloseHandle as well. After a thread is exited the OS still retain some information on the thread (such as it''s exit value) until CloseHandle is called.

-Neophyte

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry to hijack the thread, but I have a quick question. WHEN do you call ExitThread? Right before the program exits?

Share this post


Link to post
Share on other sites
CloseHandle could be called right after CreateThread if you don't need the handle for anything. The thread will run anyway. The following example will create a thread and close its handle, but it will not exit the thread.

CloseHandle(CreateThread( NULL, 0, ThreadMain, reinterpret_cast<void*>(this), &dwThreadId));

ExitThread is called from within the thread that should exit. It is the same as returning from the thread procedure.



Update GameDev.net system time campaign: 'date ddmmHHMMYYYY'

[edited by - dalleboy on January 23, 2003 7:45:44 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Sorry to hijack the thread, but I have a quick question. WHEN do you call ExitThread? Right before the program exits?


I''m using a thread to make some animation, so after I complete rendering
the whole animation I call ExitThread()


Oh, and thanks for the answers, I need the handle, I use it to see when
the thread is still running... So when I close the thread I do this:

CloseHandle( ThreadHandle );
ThreadHandle = NULL



KaMiKaZe

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!