Coder Social home page Coder Social logo

Comments (5)

progschj avatar progschj commented on July 16, 2024

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.

thejacobpollack avatar thejacobpollack commented on July 16, 2024

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.

progschj avatar progschj commented on July 16, 2024

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.

thejacobpollack avatar thejacobpollack commented on July 16, 2024

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.

progschj avatar progschj commented on July 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.