2017-03-10 33 views
13

我已经试过了的boost ::纤维::屏障,我无法找出原因如下代码死锁:为什么我升压纤维代码死锁

#include <boost/fiber/all.hpp> 
#include <iostream> 
#include <boost/range/algorithm/generate.hpp> 
#include <boost/range/algorithm/for_each.hpp> 

void barrier_test() 
{ 
    boost::fibers::barrier barrier(2); 
    std::vector<boost::fibers::fiber> myfibers(4); 
    boost::generate(myfibers, [&barrier]() { 
     return boost::fibers::fiber([](boost::fibers::barrier& barrier) { 
      static unsigned id_inc = 0; 
      const auto id = ++id_inc; 
      std::cout << "fiber id: " << boost::this_fiber::get_id() << " - local id: " << id << std::endl; 
      barrier.wait(); 
      std::cout << "barrier passed, fiber id: " << boost::this_fiber::get_id() << " - local id: " << id << std::endl; 
     }, std::ref(barrier)); 
    }); 

    std::cout << "main fiber: " << boost::this_fiber::get_id() << std::endl; 
    boost::for_each(myfibers, [](boost::fibers::fiber& aFiber) { 
      aFiber.join(); 
    }); 
    std::cout << "end of program" << std::endl; 
} 

如果我设置的推出为“派遣”它可以通过。所以它与顺序有关,但我不知道什么是错的。所以我试图想象如何锁定幕后,什么得到运行等,但我无法找出,为什么原始代码无法完成。

如果有人不想尝试一下,让我把这里的输出我看到:

main fiber: 000000000042C960 
fiber id: 000000000042C6E0 - local id: 1 
fiber id: 000000000044CF60 - local id: 2 
barrier passed, fiber id: 000000000044CF60 - local id: 2 
fiber id: 000000000045D020 - local id: 3 
fiber id: 000000000046D0E0 - local id: 4 
barrier passed, fiber id: 000000000046D0E0 - local id: 4 
barrier passed, fiber id: 000000000045D020 - local id: 3 

我抄我的一个自定义的测试调度算法到测试代码,我看到的,过了一段时间,没有光纤可用于运行,本地ID 1光纤根本就没有继续。

升压版本1.63与Visual Studio 2015年预编译包和汇编是在64位

回答

1

我试着用boost-1.63.0在Debian/unstable上编译程序,我可以确认问题。当我运行该程序使用valgrind访问未初始化的内存被报告,并在以后也无效的读取,所以我想问题是与该特定的增强版本。

+0

谢谢,我没有尝试valgrind这个例子,谢谢你的确认。 – newhouse

-1

变化

boost::fibers::barrier barrier(2); 

boost::fibers::barrier barrier(4); 

或等到升压1.64释放

+1

这不是一个解决方案,在这种情况下,你只是将屏障扩大到更大的数量,它不会阻止成对的光纤,但没有。你写道,我应该等待boost-1.64,你知道这个偶然的错误吗? – newhouse

+0

我与@newhouse。除非你知道增加屏障数量的合乎逻辑的理由,否则这至少是一个货运咨询的答案。如果没有任何论证,他们会接受像这样的“神奇建议”,我会给我的团队中的程序员一个非常严厉的谈话。 – sehe

2

代码从来没有死锁,但有一些变数c旁观者:我在Mac上,我正在使用boost 1.64,而且我可能会以不同的方式编译boost。

boost? ./fibers 
main fiber: 0x7f877cc02bc0 
fiber id: 0x100cbce00 - local id: 1 
fiber id: 0x100ebce00 - local id: 2 
barrier passed, fiber id: 0x100ebce00 - local id: 2 
fiber id: 0x100fbce00 - local id: 3 
fiber id: 0x1010bce00 - local id: 4 
barrier passed, fiber id: 0x1010bce00 - local id: 4 
barrier passed, fiber id: 0x100cbce00 - local id: 1 
barrier passed, fiber id: 0x100fbce00 - local id: 3 
end of program 

仅供参考 - 我编译和运行这样的:

./bootstrap.sh --prefix=/Users/john/boost/boost_installation --without-libraries=python 

./b2 install cxxflags='-std=c++14' -j 6 threading=multi variant=release link=shared stage --reconfigure 

g++ -std=c++14 -I/Users/john/boost/boost_installation/include/ -L/Users/john/boost/boost_installation/lib -lboost_context -lboost_system -lboost_thread -lpthread -lboost_fiber -o fibers fibers.cpp 

DYLD_LIBRARY_PATH=/Users/john/boost/boost_installation/lib 

./fibers 

这可能不是你所需要的,但因为我的作品,我不能帮你。