nbsdx / threadpool Goto Github PK
View Code? Open in Web Editor NEWLightweight, Generic, Pure C++11 ThreadPool
License: The Unlicense
Lightweight, Generic, Pure C++11 ThreadPool
License: The Unlicense
atomic_bool
does not have a constructor on VS 2012/2013 but can be replaced by atomic<bool>
which has one. Same for atomic_int
Is there a way to make the number of threads (ThreadCount
) dynamically used by the program?
Something like: ThreadPool pool(std::thread::hardware_concurrency())
#include <thread>
#include <iostream>
#include "ThreadPool.h"
using std::cout;
using std::endl;
int main() {
using namespace nbsdx::concurrent;
ThreadPool<20> pool;
for (auto j = 0; j < 3000; j++) {
cout << "Round:" << j << endl;
for (int i = 0; i < 5000; ++i) {
std::function<int(void)> task_function = [i]() {
return i * i;
};
pool.AddJob(task_function);
}
cout << "Finish Add" << endl;
pool.WaitAll();
}
}
I'm getting this error /usr/include/c++/5.3.0/condition_variable: In instantiation of ‘void std::condition_variable::wait(std::unique_lock<std::mutex>&, _Predicate) [with _Predicate = ThreadPool::next_job()::<lambda()>]’: lib/../lib/ThreadPool.hpp:53:78: required from here /usr/include/c++/5.3.0/condition_variable:97:13: error: could not convert ‘__p.ThreadPool::next_job()::<lambda()>()’ from ‘void’ to ‘bool’ while (!__p())
while using your header with lambda:
ThreadPool<8> pool;
pool.AddJob( [it, &l, this]() {
// Do something
if (...) {
}else {...}
std::lock_guard<std::mutex> lock(this->mtx);
{...}
});
Hi nbsdx,
I am confused about the introduction of jobs_left.
What is the purpose of introducing atomic_int type jobs_left(in line 27)?
I think, we could simply add the queue_mutex's lock in function WaitAll() (in line 148), rather than using the atomic_int variable jobs_left?
So, what is your consideration here?
Best,
CHE Yulin
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.