Coder Social home page Coder Social logo

Comments (6)

yinchunxiang avatar yinchunxiang commented on August 15, 2024
// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args) 
    -> std::future<typename std::result_of<F(Args...)>::type>
{
    using return_type = typename std::result_of<F(Args...)>::type;

    auto task = std::make_shared< std::packaged_task<return_type()> >(
            std::bind(std::forward<F>(f), std::forward<Args>(args)...)
        );

    std::future<return_type> res = task->get_future();
    {
        std::unique_lock<std::mutex> lock(queue_mutex);

        // don't allow enqueueing after stopping the pool
        if(stop)
            throw std::runtime_error("enqueue on stopped ThreadPool");

        tasks.emplace([task](){ (*task)(); });
    }
**    condition.notify_one(); ///----> Should this line be in the lock scope ?**
    return res;
}

// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
    {
        std::unique_lock<std::mutex> lock(queue_mutex);
        stop = true;
    }
**    condition.notify_all(); ///----> Should this line be in the lock scope ?**
    for(std::thread &worker: workers)
        worker.join();
}
```cpp

from threadpool.

foobar avatar foobar commented on August 15, 2024

condition.notify_one(); ///----> Should this line be in the lock scope ?

would it cause a deadlock ?

from threadpool.

mnowotnik avatar mnowotnik commented on August 15, 2024

Not deadlock, but the thread could never wake up.

from threadpool.

lazylazypig avatar lazylazypig commented on August 15, 2024

@mgerhardy
excuse me, what's the final solution?

from threadpool.

wilx avatar wilx commented on August 15, 2024

I think that in general it is a good idea to notify inside the locked region because you can miss wakeups otherwise. But this code is not edge triggered but level triggered, I think. In this particular case, I do not think it is possible to miss a wake up.

from threadpool.

ilyapopov avatar ilyapopov commented on August 15, 2024

This SO question is relevant: http://stackoverflow.com/questions/17101922/do-i-have-to-acquire-lock-before-calling-condition-variable-notify-one/

TL;DR: No, you don't need to hold a lock while doing notify, but there can be performance implications.

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.