0

I have a class such as :

class MyStreamReader
{
public:
    MyStreamReader(MyPramameter myPram) {.....}
    ~MyStreamReader() {}

    DWORD WINAPI  ReaderThread(LPVOID *lpdwThreadParam ) 

    {

       //....
    }
};

and i want to call ReaderThread with WinAPI CreateThread. But CreateThread wants ReaderThread function wants a static function.

In some forms it is said that this is possible with boost library such as :

CreateThread(NULL, 0, boost::bind(&MyStreamReader::ReaderThread,this),

(void*)&myParameterObject), 0, NULL);

But i got compilation error:

'CreateThread' : cannot convert parameter x from 'boost::_bi::bind_t<R,F,L>' 

to 'LPTHREAD_START_ROUTINE'

So as a result my questions:

  1. Is it possible to call non-static function of a class from CreateThread using boost lib(or any other method)
  2. If not any C++ THREADing librray you may recomend(for visual C++) which i can call-run non static member function of a class as a thread?

Best Wishes

Update:

So first question: It seesm that it is impossible to call non-static c++ member function from CreateThread win API...

So any recomandations for C++ Multithreading lib whic is possible to call non-static functions as threads...

Update 2: Well i try boost thread lib...seems it works...

MyStreamReader* streamReader = new MyStreamReader(myParameters);


boost::thread GetStreamsThread

   ( boost::bind( &MyStreamReader::ReaderThread, streamReader ) );

or (no need for bind)

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()
        {

           //....
        }
  };
Novalis
  • 2,123
  • 6
  • 35
  • 60
  • Stay away from `CreateThread` and use `_beginthread` or `_beginthreadex` if you really have to. Those are too low level functions to deal with threads. Just listen to @Kaz Dragon. – ali_bahoo Jul 27 '11 at 08:56
  • @sad_man Why is `_beginthread` better than `CreateThread`? – David Heffernan Jul 27 '11 at 09:34
  • @David: [This](http://stackoverflow.com/q/331536/425817) is enough for me unless you have something to say. – ali_bahoo Jul 27 '11 at 12:11
  • @sad_man _beginthread is a good alternative for non static c functions BUT sucks at C++ member functions... – Novalis Jul 27 '11 at 15:25
  • @sad_man those problems with using the C runtime from threads created by `CreateThread` have been fixed in recent versions of the runtime. It was a ludicrous limitation anyway. What if you want to run code in a thread that was created by some other module? – David Heffernan Jul 27 '11 at 15:44
  • @6.45.Vapuru biraderim, I do not understand what you mean by "sucks" BUT _C++ member functions_ title is a different story. `_beginthread` and `_beginthreadex` do almost the things, except that you do not use `CloseHandle()` with the handle returned by _beginthread. It is automatically closed. – ali_bahoo Jul 27 '11 at 15:54
  • @David, hmmmm I don't know please tell me. – ali_bahoo Jul 27 '11 at 15:57

1 Answers1

2

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 call to pv. Just have your thunk sort them out correctly.

To answer your question more directly, boost::bind doesn't work with the Winapi that way. I would advise using boost::thread instead, which does work with boost::bind (or, if you have a C++0x compiler, use std::thread with std::bind).

Kaz Dragon
  • 6,266
  • 2
  • 32
  • 44