Archived

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

original vesoljc

classes with threads

Recommended Posts

when used globally i had: DWORD API mythread() { while(true) { do stuff} return 1; } now when using mythread as a member function, i get an error with CreateThread, saying i cannot use "&" on a member func. how should this be done? Abnormal behavior of abnormal brain makes me normal...

Share this post


Link to post
Share on other sites
for threads in C++, check out the boost::threads library. Efficient and platform independent, and much better support for OO.

Share this post


Link to post
Share on other sites
Make it a non-static member function, then pass the object via the context pointer. You can have a pointer to a static member function. You just have to cast the thread argument to a pointer to your class and then explicitly call "pThis->" for member calls. Example:

  
MyClass
{
int m_thingy;
static DWORD API threadProc (DWORD arg)
{
MyClass *pThis = reinterpret_cast<MyClass *> (arg);
pThis->m_thingy = 4;
}
};

Share this post


Link to post
Share on other sites
not sure i follow you...
"Make it a non-static member function" like:
decl:
class WinApp
{
// stuff
DWORD WINAPI MyThread();
}
implem:
DWORD WINAPI WinApp::MyThread()
{
// do stuff
}

i know that this doesnt work:
CreateThread(...,&(this->MyThread),...)


confuesed...

Abnormal behavior of abnormal brain makes me normal...

Share this post


Link to post
Share on other sites
Should have said "Make a static member", not "make a non-static." Your problem is that it''s currently non-static; it needs to be static. Just follow my example.

(Sorry, I stayed up last night beating the single-player NeverWinter Nights campaign with my monk).

Share this post


Link to post
Share on other sites
quote:
Original post by Stoffel
(Sorry, I stayed up last night beating the single-player NeverWinter Nights campaign with my monk).

I skipped my one-day sleep with my dwarf.


Anyway, they need to have a function that is NOT reference by this pointer. (or they don''t like member-function for that. )

Share this post


Link to post
Share on other sites