gpt4 book ai didi

c++ - 如何在类里面正确使用 boost channel (和光纤)?

转载 作者:行者123 更新时间:2023-11-28 01:46:07 28 4
gpt4 key购买 nike

我正在尝试在类里面使用 boost channel 和光纤。这是一个简单的测试用例,工作正常,但它并不是我想要的。如果我将“line:1”移至“loc:1”,程序将挂起(gdb 在 c->push(a) 之后显示在 boost::fibers 内的自旋锁处).谁能指出我做错了什么来帮助我?谢谢。

这是运行并生成以下内容的示例代码,

#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

最佳答案

channel_t temp{_n}; _chan = &temp; // <<<<<<<<<<<< line:1

in Block()不起作用,因为 temp 在离开 Block() 后超出范围' s body 和 _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) 保持 channel temp 为 Block<> 的成员变量:

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

关于c++ - 如何在类里面正确使用 boost channel (和光纤)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45023761/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com