【问题标题】:Why this tbb program cannot compile?为什么这个 tbb 程序无法编译?
【发布时间】:2012-04-19 06:09:20
【问题描述】:

我在 centos 目录 /home/is_admin/tbb40_233oss/ 中安装线程构建块(http://threadingbuildingblocks.org/ver.php?fid=174)

这是我的代码:

#include "tbb/concurrent_queue.h" 
#include <iostream> 
using namespace std; 
using namespace tbb; 
int main() { 
    concurrent_queue<int> queue; 
    for( int i=0; i<10; ++i ) 
        queue.push(i); 
    for( concurrent_queue<int>::const_iterator i(queue.begin()); 
i!=queue.end(); ++i ) 
        cout << *i << " "; 
    cout << endl; 
    return 0; 
}

我使用这个命令编译代码:

g++ test_concurrent_queue.cpp -I/home/is_admin/tbb40_233od/linux_intel64_gcc_cc4.1.2_libc2.5_kernel2.6.18_release -ltbb -o tcq

但它给出了这个错误:

class tbb::strict_ppl::concurrent_queue<int, tbb::cache_aligned_allocator<int> > has no member named begin

class tbb::strict_ppl::concurrent_queue<int, tbb::cache_aligned_allocator<int> > has no member named end

我不知道为什么?任何有tbb经验的人可以帮助我吗?

【问题讨论】:

    标签: c++ multithreading thread-safety queue tbb


    【解决方案1】:

    编辑:

    您使用的文档已过时,不再适用于 concurrent_queue。我的其余答案仍然有效。


    因为concurrent_queue 没有beginend 方法: http://threadingbuildingblocks.org/files/documentation/a00134.html

    有一个unsafe_begin 和一个unsafe_end 方法,之所以这样命名,是因为您应该只在您的队列未被多个线程使用时才使用它们(也就是说,它们是不安全的 em> 在多线程环境中使用)。

    遍历队列的一般方法是弹出元素直到它为空:

    int i;
    while(queue.try_pop(i)) // as long as you can pop, pop.
        cout << i << " "; 
    

    【讨论】:

    • 你的意思是我必须使用concurrent_bounded_queue?代码是threadingbuildingblocks.org cache-www.intel.com/cd/00/00/30/11/301114_301114.pdf#page=61的示例代码
    • 嗯,我可能错过了什么。我给出的答案是针对tbb::strict_ppl::concurrent_queue,这是您的编译器认为concurrent_queue 所指的内容。看起来还有另一个具有不同接口的 concurrent_queue 类。
    • @Greper:您链接到的文档是 2007 年的。有更新的文档 here
    • @Jesse 是的,他使用过时的文档是我最好的猜测。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多