简体   繁体   English

使用boost链和std :: mutex

[英]Use boost strand and std::mutex

Follow up on this site 在这个网站上跟进

The Article HTTP Server 3 文章HTTP Server 3

They're using stand to guarantee that none of those handlers will execute concurrently. 他们使用立场保证这些处理程序中的任何一个都不会同时执行。

so, I have 3 threads that calls io_context.run() and which leads them to enter to channel and may have to use vector's push_back at the same time. 因此,我有3个线程,它们调用io_context.run()并导致它们进入通道,并且可能必须同时使用vector的push_back。

Do I have to use a std::mutex to lock push_back function to prevent them call concurrently or it well managed by strand ? 我是否必须使用std :: mutex来锁定push_back函数以防止它们并发调用,或者由strand对其进行良好的管理?

Assuming all of your code is correctly running within the strand then only one thread will be executing your code at a time. 假设所有代码都在该链中正确运行,那么一次只有一个线程将在执行您的代码。

The thread that is executing your code will change between the three threads that are available but only one function will be executing at a time. 正在执行代码的线程将在可用的三个线程之间切换,但一次只能执行一个功能。

You therefore shouldn't need mutexes, this is the part of the idea of using strands as mutexes are difficult to use correctly in asynchronous code. 因此,您不需要互斥体,这是使用链的想法的一部分,因为互斥体很难在异步代码中正确使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM