1

我对堆栈溢出很陌生,事实上这是我的第一篇文章,大家好。所以让我们进入正题。使用 boost 库线程版本。1.54.0 使用 VS2010 32 位 - 专业 我已经为 boost 线程构建了库,没有在 vs C++ 设置中使用预编译头文件,将库链接到项目,这里是代码

    #include <boost\thread\thread_only.hpp>
#include <iostream>
#include <conio.h>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")
#define BOOST_LIB_NAME libboost_thread-vc100-mt-gd-1_54.lib


struct callable
{
     void blah();
};

void callable::blah()
{
    std::cout << "Threading test !\n";
}
boost::thread createThread()
{
    callable x;
    return boost::thread(x);
}
int main()
{
    createThread();
    _getch();
    return 0;
}

毕竟这我得到这个错误

Error   1   error C2064: term does not evaluate to a function taking 0 arguments    ..\..\boost_1_54_0\boost\thread\detail\thread.hpp   117 1   BoostTrial

你能帮我让这个例子工作吗?我使用这个例子的原因是因为我有另一个应用程序,它的设置方式完全相同,但由于这个错误它不能工作:-(我的目标是让多线程工作,然后我可以从那里得到它。 谢谢你的时间。

4

1 回答 1

0

您需要operator()callable.

不要忘记使用join()detach()线程来防止程序异常终止。

有关更多示例,请参见boost::thread教程

#include <boost\thread\thread_only.hpp>
#include <iostream>
#pragma comment(lib, "libboost_thread-vc100-mt-gd-1_54.lib")

using namespace boost;

struct callable
{
    void operator()()
    {
        std::cout << "Threading test !\n";
    }
};


boost::thread createThread()
{
    callable x;
    return boost::thread(x);
}
int main()
{
    boost::thread th = createThread();
    th.join();

}

std::thread带有;的示例

于 2013-10-10T13:12:23.383 回答