Łuk*_*zyk 5 c++ concurrency multithreading boost-asio
我有一份asio::io_service::strand. 复制的链及其源是不同的执行者吗?换句话说,传递给复制链的函数和传递给源链的另一个函数是否有可能由两个不同的线程同时执行?
或者这两个分支在逻辑上都是“一个分支”,这意味着传递给它们中的任何一个的工作都不会与传递给它们的其他工作一起执行?
查看示例
asio::io_service ioService;
asio::io_service::strand strandFromIoService{ioService};
asio::io_service::strand strandFromStrand{strandFromIoService};
strandFromIoService.post(boost::bind(&firstFunction));
strandFromStrand.post(boost::bind(&secondFunction));
// then use a pool of threads to service io_service ...
// can firstFunction and secondFunction be executed in one time?
Run Code Online (Sandbox Code Playgroud)
The answer is no.
strand class has two attributes only
class io_service::strand
{
public:
// ...
private:
asio::detail::strand_service& service_;
asio::detail::strand_service::implementation_type impl_;
};
Run Code Online (Sandbox Code Playgroud)
impl_ is a pointer to strand_impl:
typedef strand_impl* implementation_type;
Run Code Online (Sandbox Code Playgroud)
There is also no user-defined copy constructor in strand class.
It is strand_impl, which contains que, mutex, counter and all stuff related to multithreading. And this stuff is not changed on strand copy since strand has only a pointer to it and only pointer is copied (saying nothing about io_service reference, but this reference has obviously no impact on strand copy)
Therefore a copy and a source strands are logically the same strand. They represent the same executor.
This also matches my experiments. firstFunction and secondFunction in question's example were executed indeed sequentially by 2 threads.
| 归档时间: |
|
| 查看次数: |
324 次 |
| 最近记录: |