Use of class function in thread
-
:confused:How can I implement a class function, which can be run in separate thread Example class AudioIn { public: AudioIn(); virtual ~AudioIn(); void StartProc(); HANDLE Thread; DWORD m_ThreadID; HANDLE hEvent; }; AudioIn::AudioIn() { hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AudioIn::StartProc,NULL,0,&m_ThreadID); } Is it possible? I'm creating console application in VC++. Sorry for stupid question. Tomaz
-
:confused:How can I implement a class function, which can be run in separate thread Example class AudioIn { public: AudioIn(); virtual ~AudioIn(); void StartProc(); HANDLE Thread; DWORD m_ThreadID; HANDLE hEvent; }; AudioIn::AudioIn() { hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AudioIn::StartProc,NULL,0,&m_ThreadID); } Is it possible? I'm creating console application in VC++. Sorry for stupid question. Tomaz
-
How can I get few parameters into static class function? Should I declare a structure Thread = CreateThread(NULL,0,AudioIn::StartProc,declared-structure,0,&m_ThreadID); or is there a simple way? Thank you Tomaz
-
How can I get few parameters into static class function? Should I declare a structure Thread = CreateThread(NULL,0,AudioIn::StartProc,declared-structure,0,&m_ThreadID); or is there a simple way? Thank you Tomaz
You have to declare a structure and pass a pointer to it. There are some common problems regarding who owns this structure. Consider the following:
void foo()
{
data d;
// fill d;
CreateThread(...,&d,...)
}This seems OK and will probably work some times until it suddenly stops to do it. The reason is that, when the thread starts executing and access
d
, it is perfectly normal thatfoo
has already exited, andd
is no longer valid. So, you have to dynamically allocate the data:void foo()
{
data* d=new data;
// fill d;
CreateThread(...,d,...)
}Now the problem is who deletes
d
? The most reasonable choice is to let the thread delete the data:MyThread(LPVOID arg)
{
data * d=(data *)arg;
...
delete d; // d no longer needed.
...
}This is almost perfect: you should take into account the rare case when the thread does not launch (due to insufficient resources, for instance):
void foo()
{
data* d=new data;
// fill d;
if(!CreateThread(...,d,...)){
delete d; // no thread launched, clean up the mess ourselves
}
}That's it, hope it helps. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
:confused:How can I implement a class function, which can be run in separate thread Example class AudioIn { public: AudioIn(); virtual ~AudioIn(); void StartProc(); HANDLE Thread; DWORD m_ThreadID; HANDLE hEvent; }; AudioIn::AudioIn() { hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); Thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)AudioIn::StartProc,NULL,0,&m_ThreadID); } Is it possible? I'm creating console application in VC++. Sorry for stupid question. Tomaz
Hi Tomaz, the problem is that CreateThread() is a c function...and c doesn't know anything about classes or virtual tables... try this: //--------------------------------------------------- void YourClass::AnyFunction() { // declarations DWORD dwId; // create the thread CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartAddress,(LPVOID)this,0,&dwId); } DWORD YourClass::StartAddress(LPVOID lpParam) { // call the thread function ((YourClass*)lpParam)->ThreadFunction(); } void YourClass::ThreadFunction() { ... } //--------------------------------------------------- in the header you should declare the thread start procedure as follows: static DWORD StartAddress(LPVOID lpParam); ..this should do it
-
Hi Tomaz, the problem is that CreateThread() is a c function...and c doesn't know anything about classes or virtual tables... try this: //--------------------------------------------------- void YourClass::AnyFunction() { // declarations DWORD dwId; // create the thread CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartAddress,(LPVOID)this,0,&dwId); } DWORD YourClass::StartAddress(LPVOID lpParam) { // call the thread function ((YourClass*)lpParam)->ThreadFunction(); } void YourClass::ThreadFunction() { ... } //--------------------------------------------------- in the header you should declare the thread start procedure as follows: static DWORD StartAddress(LPVOID lpParam); ..this should do it
:laugh:It is working. Thank you. Tomaz