2012-12-13 64 views
3

好吧..首先,我必须说我使用BOOST和它的源代码(我必须)。 我既是一个BOOST也是一个C++新手,但我并不陌生编码(我习惯于托管语言)。我在一个有点大项目遇到了这个问题,那么我转载它在这个小的代码片段,我在这里提出:BOOST线程属性的使用导致绑定编译错误

#include <boost/thread.hpp> 

void foo(int bar) { 
    printf("Chu %d!",bar); 
} 

int main() { 
    boost::thread_attributes attrs; 

    boost::thread causeTrouble(attrs,foo,42); // <-- Probably problematic line 
    causeTrouble.join(); 
} 

根据BOOST 1.52.0 Documentation该段应编译和运行正常;但是,它给了我一个Boost库的头文件,一个奇怪的编译问题(没有其他错误或警告存在):

<boost_path>/bind/bind.hpp:313: error: no match for call to '(boost::thread_attributes) (void (*&)(int), int&) 

对我来说,它看起来像有没有实际的boost ::线程(升压:: thread_attributes ,F f)构造函数,即使它应该按照之前链接的文档。 无论如何,有什么好笑的是,无论是以下行做编译罚款

boost::thread noTrouble(attrs,foo); 

boost::thread noTroubleEither(foo,42); 

即使我彻底搜查的StackOverflow和互联网的休息,我不知道在哪里转我的头:(其实这是我第一次被迫实际上问一个新的问题。帮助!

+1

哪个编译器? – user7116

+0

我正在使用GCC编译器 –

+0

哪个版本?它支持可变模板和rvale引用吗? –

回答

4

你说,

它看起来像有没有实际的boost ::线程(升压:: thread_attributes,F F)

这不是你想虽然调用构造函数。您正在拨打boost::thread(attrs, foo, 42)。根据链接,看起来没有boost::thread(boost::attributes, F, Args)构造函数实现,因此投诉。

尝试首先使用boost::bind明确地将42绑定到foo并启动绑定函数对象上的线程。

事情是这样的:

boost::function<void> f = boost::bind(foo, 42); 
boost::thread(attrs, f) 
+0

你是什么意思?你能提供一个代码示例吗? (无论如何) –

+0

好吧这工作: boost :: thread causeTrouble(attrs,boost :: bind(foo,42)); 但是,它现在在大型项目中给出了一个不同的问题(我有8个整数参数),但无论如何这是一个开始。明天我会尝试修复它,如果我有问题,我会再次发布。 非常感谢! –

+0

好吧,看起来另一个问题是由于我在这个大项目上犯的一个无关的错误。它现在编译好!再次感谢你! –

1

看起来像一个转发的问题米尝试定义一个值为42的int变量并将其作为第三个参数传递。

+0

试过了:同样的问题。 =/ –

0

所需要的过载

template <class F, class Arg, class ...Args> 
thread(attributes const& attrs, F&& f, Arg&& arg, Args&&... args) 

定义仅当编译器支持可变参数模板和右值引用。这可以解释你的问题。

我想文档可以改善,以清楚地表明这一点。请你可以创建一个Trac票来跟踪这个问题?

0

您可能需要使用boost::bind,像这样:

boost::function<void> f = boost::bind(foo, 42); 

boost::thread(attrs, f) 
0

你应该为属性提供STACKSIZE,这里有一个例子:

#include <boost/thread.hpp> 
    #include <boost/chrono.hpp> 
    #include <iostream> 

    void wait(int seconds) 
    { 
    boost::this_thread::sleep_for(boost::chrono::seconds{seconds}); 
    } 

    void thread() 
    { 

    try 
    { 
     for (int i = 0; i < 5; ++i) 
     { 
     wait(1); 
     std::cout << i << '\n'; 
     } 
    } 
    catch (boost::thread_interrupted&) {} 
    } 

    int main() 
    { 
    boost::thread::attributes attrs; 
    attrs.set_stack_size(1024); 
    boost::thread t{attrs, thread}; 
    t.join(); 
    }