为什么这个tbb程序无法编译

Why this tbb program cannot compile?

本文关键字:编译 程序 tbb 为什么      更新时间:2023-10-16

我在 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 经验可以帮助我吗?

编辑:

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


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

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

运行队列的一般方法是弹出元素,直到它为空:

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