简体   繁体   中英

Use boost strand and std::mutex

Follow up on this site

The Article 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.

Do I have to use a std::mutex to lock push_back function to prevent them call concurrently or it well managed by 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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