Comments (6)
// 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.
condition.notify_one(); ///----> Should this line be in the lock scope ?
would it cause a deadlock ?
from threadpool.
Not deadlock, but the thread could never wake up.
from threadpool.
@mgerhardy
excuse me, what's the final solution?
from threadpool.
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.
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)
- 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 5
- i don't understand why this demo is ok.... why have lots of ~Test().... HOT 2
- How dose this for loop end? HOT 4
- 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.