本文介绍了Boost库和CreateThread的API取胜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,如:

 类MyStreamReader
{
上市:
    MyStreamReader(MyPramameter myPram){} .....
    〜MyStreamReader(){}    DWORD WINAPI ReaderThread(LPVOID * lpdwThreadParam)    {       // ....
    }
};

和我想打电话给ReaderThread是 WinAPI的CreateThread的但是CreateThread的希望ReaderThread功能想要一个静态函数。

在某些形式这是说,这是可能的Boost库,如

 的CreateThread(NULL,0,提高::绑定(安培; MyStreamReader :: ReaderThread,这一点),(无效*)及myParameterObject),0,NULL);

但我得到的编译错误:

 '的CreateThread:不能转换从参数x'的boost :: _双:: bind_t< R,F,L>'以LPTHREAD_START_ROUTINE

因此,作为一个结果,我的问题:


  1. 是否有可能调用类的非静态函数从
    CreateThread的使用升压LIB(或任何其他方法)

  2. 如果没有任何C ++线程librray你可以建议更换(对于Visual C ++),这我可以调用运行一类非静态成员函数作为一个线程?

最良好的祝愿

更新:

所以,第一个问题:它seesm,这是不可能调用非静态C ++成员函数CreateThread的距离赢得API ...

所以对于C ++多线程的lib任何recomandations whic可以调用非静态功能的线程...

更新2:
嗯,我尝试提升线程lib中......似乎它的工作原理...

  MyStreamReader * StreamReader的=新MyStreamReader(myParameters);
提高::线程GetStreamsThread   (::提振绑定(安培; MyStreamReader :: ReaderThread,StreamReader的));

或(不需要绑定)

 的boost ::线程GetStreamsThread(安培; MyStreamReader :: ReaderThread,StreamReader的);

和以使用boost ::线程更新我的类定义为:

 类MyStreamReader
  {
    上市:
        MyStreamReader(MyPramameter myPram){} .....
        〜MyStreamReader(){}        无效ReaderThread()
        {           // ....
        }
  };


解决方案

一个共同的答案是使用静态的thunk

 类工人
{
    上市 :
        静态DWORD咚(无效* PV)
        {
            工人* pThis =的static_cast<工人* GT;(PV);
            返回pThis-> DoWork的();
        }        DWORD的DoWork(){...}
};...诠释的main()
{
    用工;
    的CreateThread(NULL,0,&安培;工人::咚,&安培;工人);
}

您可以的,当然,更包到的参数您的通话光伏。只要有你的thunk正确地排序出来。

要更直接地回答你的问题,提高::绑定不与WINAPI工作方式。我会建议使用boost ::线程来代替,这并用的boost ::绑定工作(或者,如果你有一个的C ++ 0x编译器,使用std ::螺纹的std ::绑定)。

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

           //....
        }
  };
解决方案

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).

这篇关于Boost库和CreateThread的API取胜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:36