Boost Library and CreateThread API
I have a class like:
class MyStreamReader
{
public:
MyStreamReader(MyPramameter myPram) {.....}
~MyStreamReader() {}
DWORD WINAPI ReaderThread(LPVOID *lpdwThreadParam )
{
//....
}
};
and I want to call ReaderThread with WinAPI CreateThread . But CreateThread wants the ReaderThread function to request a static function.
Some forms say that this is possible with the boost library such as :
CreateThread(NULL, 0, boost::bind(&MyStreamReader::ReaderThread,this),
(void*)&myParameterObject), 0, NULL);
But I got a compile error:
'CreateThread' : cannot convert parameter x from 'boost::_bi::bind_t<R,F,L>'
to 'LPTHREAD_START_ROUTINE'
As a result, my questions are:
- Is it possible to call non-stationary class function from CreateThread using boost lib (or any other method)
- If not any C ++ THREADing librray can you recommend (for visual C ++) that I can call a non-statistical member function of a class as a stream?
Best regards
Update:
So first question: he sees that it is not possible to call a non-stationary C ++ member function from the CreateThread win API ...
So any recommendation for C ++ Multithreading lib whic can call non-static functions as threads ...
Update 2: Well I'm trying to increase the lib stream ... it seems to work ...
MyStreamReader* streamReader = new MyStreamReader(myParameters);
boost::thread GetStreamsThread
( boost::bind( &MyStreamReader::ReaderThread, streamReader ) );
or (no need to link)
boost::thread GetStreamsThread(&MyStreamReader::ReaderThread, streamReader);
And in order to use boost :: thread I update my class definition as:
class MyStreamReader
{
public:
MyStreamReader(MyPramameter myPram) {.....}
~MyStreamReader() {}
void ReaderThread()
{
//....
}
};
source to share
One common answer to this is to use a static "thunk":
class Worker
{
public :
static DWORD Thunk(void *pv)
{
Worker *pThis = static_cast<Worker*>(pv);
return pThis->DoWork();
}
DWORD DoWork() { ... }
};
...
int main()
{
Worker worker;
CreateThread(NULL, 0, &Worker::Thunk, &worker);
}
You can, of course, pack more parameters into your pv call. Just ask them to choose them correctly.
To answer your question more directly, boost :: bind does not work with Winapi in this way. I would advise using boost :: thread instead, which works with boost :: bind (or, if you have a C ++ 0x compiler, use std :: thread with std :: bind).
source to share