如何在课堂上正确使用助推通道(和光纤)?

时间:2017-07-11 01:00:39

标签: c++ boost boost-thread

我正在尝试在课堂上使用增强通道和光纤。这是一个简单的测试用例正常工作但它并不完全是我想要的。如果我将“ line:1 ”移动到“ loc:1 ”,程序会挂起(gdb显示在c ::> push之后的boost :: fibers内的spinlock(a ))。任何人都可以通过指出我做错了什么来帮助我吗?感谢。

以下是工作并生成以下内容的示例代码,

#include <iostream>
#include <boost/fiber/all.hpp>

using namespace std;

template <class T>
class Block
{
    private:
        typedef boost::fibers::buffered_channel<T> channel_t;
        typedef boost::fibers::fiber fiber_t;
        fiber_t _thread_send;
        fiber_t _thread_recv;
        size_t _n;
        channel_t* _chan;

    public:
        Block(size_t n) : _n(n), _chan(nullptr) {
            // >>>>>>>>>> loc:1 <<<<<<<<<<<
        }
        virtual ~Block() {}
        void _send(channel_t *c) {
            cout << __func__ << endl;
            int a = 1000;
            cout << "Sending: " << a << endl;
            c->push(a);
        }
        void _recv(channel_t *c) {
            cout << __func__ << endl;
            int a = 0;
            c->pop(a);
            cout << "Received: " << a << endl;
        }
        void do_work() {
            cout << "do_work\n";
            channel_t temp{_n}; _chan = &temp; // <<<<<<<<<<<< line:1
            _thread_send = boost::fibers::fiber(bind(&Block::_send, this, _chan));
            _thread_recv = boost::fibers::fiber(bind(&Block::_recv, this, _chan));
            _thread_send.join();
            _thread_recv.join();
        }
};

int main()
{
    Block<int> B(2);
    B.do_work(); 
    return 0;
}

输出:

do_work
_send
Sending: 1000
_recv
Received: 1000

使用编译:

GNU/Linux 64 bit x86-64
g++ (GCC) 7.1.1 2017051
boost 1.64.0
g++ -c --std=c++14 -g -Wall -Wpedantic boost_channels.cpp -o boost_channels.o
g++ -lboost_context -lboost_fiber boost_channels.o -o boost_channels

3 个答案:

答案 0 :(得分:1)

当您在Block构造函数中构造通道并获取指向它的指针时,指针_chantemp超出范围时指向垃圾。您可以让temp成为Block的成员,也可以将其留在工作地点,以便转发。

更新:  C ++中的括号(大括号)定义范围

Block(size_t n) : _n(n), _chan(nullptr)
    //the scope of the constructor starts at this brace
{
    //temp gets instantiated
    channel_t temp{_n};
    //assign the pointer to the object
    _chan = &temp;

} //put a break point here

然后使用记忆手表来查看_chan。当你移过结束括号时,你应该看到内存在温度被破坏时变成垃圾。如果你追踪到那一点,你会看到temp遇到它的经销商。

我会将temp留在do_work

答案 1 :(得分:1)

channel_t temp{_n}; _chan = &temp; // <<<<<<<<<<<< line:1
Block()中的

无法正常工作,因为 temp 在离开Block()的正文后超出范围,_chan将指向垃圾/释放内存

可能有两个版本:

1)将channel temp 保存为do_work()的局部变量:

template <class T>
class Block
{
private:
    typedef boost::fibers::buffered_channel<T> channel_t;
    typedef boost::fibers::fiber fiber_t;
    fiber_t _thread_send;
    fiber_t _thread_recv;
    size_t _n;

public:
    Block(size_t n) : _n(n) {
    }
    virtual ~Block() {}
    void _send(channel_t *c) {
        cout << __func__ << endl;
        int a = 1000;
        cout << "Sending: " << a << endl;
        c->push(a);
    }
    void _recv(channel_t *c) {
        cout << __func__ << endl;
        int a = 0;
        c->pop(a);
        cout << "Received: " << a << endl;
    }
    void do_work() {
        cout << "do_work\n";
        channel_t chan{_n};
        _thread_send = boost::fibers::fiber(bind(&Block::_send, this, & chan));
        _thread_recv = boost::fibers::fiber(bind(&Block::_recv, this, & chan));
        _thread_send.join();
        _thread_recv.join();
    }
};

2)将频道 temp 保留为Block&lt;&gt;的成员变量:

template <class T>
class Block
{
private:
    typedef boost::fibers::buffered_channel<T> channel_t;
    typedef boost::fibers::fiber fiber_t;
    fiber_t _thread_send;
    fiber_t _thread_recv;
    channel_t _chan;

public:
    Block(size_t n) : _chan(n) {
    }
    virtual ~Block() {}
    void _send(channel_t *c) {
        cout << __func__ << endl;
        int a = 1000;
        cout << "Sending: " << a << endl;
        c->push(a);
    }
    void _recv(channel_t *c) {
        cout << __func__ << endl;
        int a = 0;
        c->pop(a);
        cout << "Received: " << a << endl;
    }
    void do_work() {
        cout << "do_work\n";
        _thread_send = boost::fibers::fiber(bind(&Block::_send, this, & _chan));
        _thread_recv = boost::fibers::fiber(bind(&Block::_recv, this, & _chan));
        _thread_send.join();
        _thread_recv.join();
    }
};

两个版本都会生成:

do_work
_send
Sending: 1000
_recv
Received: 1000

答案 2 :(得分:0)

好的,将channel_t声明为成员可以正常工作。我猜它指的是垃圾。另外我了解到boost同步原语不喜欢std :: move(ed)。

感谢各位帮忙。