Comments (5)
Doesn't unique_lock achieve the same thing as lock_guard here? Admittedly unique_lock is maybe a little overkill in this case. But it should produce the same result.
Also even if stop was unprotected this wouldn't be an actual race condition since only one thread ever writes to stop. The lock is mostly there to force the required memory barrier on some architectures iirc.
from threadpool.
On construction a unique_lock
does not lock or unlock a mutex
, rather, it only acquires the mutex
and becomes responsible for those operations.
In the context of your problem, a lock_guard
will acquire the mutex
object and immediately lock it. It will then be unlocked when lock
goes out of scope.
Edit: I suppose that's true as well -- even with it being unprotected there is only one thread that's going to write to it. The best solution may be to just remove the extra block scope in the destructor?
from threadpool.
Aren't we in case 3 here? (http://en.cppreference.com/w/cpp/thread/unique_lock/unique_lock)
For the unique_lock to not lock there would have to be an additional defer_lock argument to the construction the way I read that.
from threadpool.
You're absolutely right. I apologize for both of the issues I posted -- I am learning these topics as well and your repository is a great resource.
Thank you for the clarifications on both issues.
from threadpool.
No problem. When it comes to this stuff it's always good to have the maximum amount of eyes on the code and resolve any doubts :).
from threadpool.
Related Issues (20)
- Support for dynamic running threads
- A simple problem about enqueue() HOT 1
- A problem about memory leak HOT 3
- testing if pool is empty HOT 1
- support c++17 and c++20 HOT 1
- How to add a Manager HOT 2
- Differences between dynamic linking and static linking for ThreadPool HOT 1
- std::result_of and std::result_of_t are deprecated in C++17. They are superseded by std::invoke_result and std::invoke_result_t HOT 2
- Unexpected stuck thread HOT 1
- a
- A simple sample of apply this threadpool HOT 1
- support multi-queues ?
- possible deadlock during ~ThreadPool HOT 2
- i don't understand why this demo is ok.... why have lots of ~Test().... HOT 2
- ❤️ HOT 1
- Problem with passing std::unique_ptr HOT 3
- Memory usage? HOT 1
- Is anyone willing to maintain this for the long term? The author seems to have given up HOT 2
- Compiles normally on Linux but generates core files when running
- What may cause a slowing down after running ThreadPool a few hours? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from threadpool.