Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
/** copy from https://softwarerecs.stackexchange.com/questions/11009/c11-thread-pool-implementation -> https://github.com/progschj/ThreadPool */ #include <vector> #include <queue> #include <memory> #include <thread> #include <mutex> #include <condition_variable> #include <future> #include <functional> #include <stdexcept> #include <iostream> namespace ctpl { namespace detail { template <typename T> class Queue { public: bool push(T const & value) { std::unique_lock<std::mutex> lock(this->mutex); this->q.push(value); return true; } // deletes the retrieved element, do not use for non integral types bool pop(T & v) { std::unique_lock<std::mutex> lock(this->mutex); if (this->q.empty()) return false; v = this->q.front(); this->q.pop(); return true; } bool empty() { std::unique_lock<std::mutex> lock(this->mutex); return this->q.empty(); } private: std::queue<T> q; std::mutex mutex; }; } class thread_pool { public: thread_pool() { this->init(); } thread_pool(int nThreads) { this->init(); this->resize(nThreads); } // the destructor waits for all the functions in the queue to be finished ~thread_pool() { this->stop(true); } // get the number of running threads in the pool int size() { return static_cast<int>(this->threads.size()); } // number of idle threads int n_idle() { return this->nWaiting; } std::thread & get_thread(int i) { return *this->threads[i]; } // change the number of threads in the pool // should be called from one thread, otherwise be careful to not interleave, also with this->stop() // nThreads must be >= 0 void resize(int nThreads) { if (!this->isStop && !this->isDone) { int oldNThreads = static_cast<int>(this->threads.size()); if (oldNThreads <= nThreads) { // if the number of threads is increased this->threads.resize(nThreads); this->flags.resize(nThreads); for (int i = oldNThreads; i < nThreads; ++i) { this->flags[i] = std::make_shared<std::atomic<bool>>(false); this->set_thread(i); } } else { // the number of threads is decreased for (int i = oldNThreads - 1; i >= nThreads; --i) { *this->flags[i] = true; // this thread will finish this->threads[i]->detach(); } { // stop the detached threads that were waiting std::unique_lock<std::mutex> lock(this->mutex); this->cv.notify_all(); } this->threads.resize(nThreads); // safe to delete because the threads are detached this->flags.resize(nThreads); // safe to delete because the threads have copies of shared_ptr of the flags, not originals } } } // empty the queue void clear_queue() { std::function<void(int id)> * _f; while (this->q.pop(_f)) delete _f; // empty the queue } // pops a functional wrapper to the original function std::function<void(int)> pop() { std::function<void(int id)> * _f = nullptr; this->q.pop(_f); std::unique_ptr<std::function<void(int id)>> func(_f); // at return, delete the function even if an exception occurred std::function<void(int)> f; if (_f) f = *_f; return f; } // wait for all computing threads to finish and stop all threads // may be called asynchronously to not pause the calling thread while waiting // if isWait == true, all the functions in the queue are run, otherwise the queue is cleared without running the functions void stop(bool isWait = false) { if (!isWait) { if (this->isStop) return; this->isStop = true; for (int i = 0, n = this->size(); i < n; ++i) { *this->flags[i] = true; // command the threads to stop } this->clear_queue(); // empty the queue } else { if (this->isDone || this->isStop) return; this->isDone = true; // give the waiting threads a command to finish } { std::unique_lock<std::mutex> lock(this->mutex); this->cv.notify_all(); // stop all waiting threads } for (int i = 0; i < static_cast<int>(this->threads.size()); ++i) { // wait for the computing threads to finish if (this->threads[i]->joinable()) this->threads[i]->join(); } // if there were no threads in the pool but some functors in the queue, the functors are not deleted by the threads // therefore delete them here this->clear_queue(); this->threads.clear(); this->flags.clear(); } template<typename F, typename... Rest> auto push(F && f, Rest&&... rest) ->std::future<decltype(f( 0,rest...))> { //note: me edit auto pck = std::make_shared<std::packaged_task<decltype(f( 0,rest...))(int)>>( //note: me edit std::bind(std::forward<F>(f), std::placeholders::_1, std::forward<Rest>(rest)...) ); auto _f = new std::function<void(int id)>([pck](int id) { (*pck)(id); }); this->q.push(_f); std::unique_lock<std::mutex> lock(this->mutex); this->cv.notify_one(); return pck->get_future(); } template<typename F, typename... Rest> auto add_immediate(F && f, Rest&&... rest) ->std::future<decltype(f( rest...))> { //note: me edit // return push([&](int zerooo,auto restLocal...){ // return f(restLocal...); // },rest...); // return push([&](int zerooo){ // return f(rest...); // }); auto pck = std::make_shared<std::packaged_task<decltype(f( rest...))(int)>>( //note: me edit std::bind(std::forward<F>(f), std::forward<Rest>(rest)...) ); auto _f = new std::function<void(int id)>([pck](int id) { (*pck)(id); }); this->q.push(_f); std::unique_lock<std::mutex> lock(this->mutex); this->cv.notify_one(); return pck->get_future(); } template<typename F> auto add_immediate(F && f) ->std::future<decltype(f())> { //note: me edit return push([&](int zerooo){ return f(); }); } // run the user's function that excepts argument int - id of the running thread. returned value is templatized // operator returns std::future, where the user can get the result and rethrow the catched exceptins template<typename F> auto push(F && f) ->std::future<decltype(f(0))> { //note: me edit auto pck = std::make_shared<std::packaged_task<decltype(f(0))(int)>>(std::forward<F>(f)); //note: me edit auto _f = new std::function<void(int id)>([pck](int id) { (*pck)(id); }); this->q.push(_f); std::unique_lock<std::mutex> lock(this->mutex); this->cv.notify_one(); return pck->get_future(); } private: // deleted thread_pool(const thread_pool &);// = delete; thread_pool(thread_pool &&);// = delete; thread_pool & operator=(const thread_pool &);// = delete; thread_pool & operator=(thread_pool &&);// = delete; void set_thread(int i) { std::shared_ptr<std::atomic<bool>> flag(this->flags[i]); // a copy of the shared ptr to the flag auto f = [this, i, flag/* a copy of the shared ptr to the flag */]() { std::atomic<bool> & _flag = *flag; std::function<void(int id)> * _f; bool isPop = this->q.pop(_f); while (true) { while (isPop) { // if there is anything in the queue std::unique_ptr<std::function<void(int id)>> func(_f); // at return, delete the function even if an exception occurred (*_f)(i); if (_flag) return; // the thread is wanted to stop, return even if the queue is not empty yet else isPop = this->q.pop(_f); } // the queue is empty here, wait for the next command std::unique_lock<std::mutex> lock(this->mutex); ++this->nWaiting; this->cv.wait(lock, [this, &_f, &isPop, &_flag](){ isPop = this->q.pop(_f); return isPop || this->isDone || _flag; }); --this->nWaiting; if (!isPop) return; // if the queue is empty and this->isDone == true or *flag then return } }; this->threads[i].reset(new std::thread(f)); // compiler may not support std::make_unique() } void init() { this->nWaiting = 0; this->isStop = false; this->isDone = false; } std::vector<std::unique_ptr<std::thread>> threads; std::vector<std::shared_ptr<std::atomic<bool>>> flags; detail::Queue<std::function<void(int id)> *> q; std::atomic<bool> isDone; std::atomic<bool> isStop; std::atomic<int> nWaiting; // how many threads are waiting std::mutex mutex; std::condition_variable cv; }; } //-------------user here ctpl::thread_pool pool(4); class C{ public: int d=35; }; class B{ public: C* c; public: void test(){ std::vector<std::future<void>> cac; for(int n=0;n<5;n++){ cac.push_back( pool.push([&](int threadId){ test2(); }) ); } for(auto& ele : cac){ ele.get(); } }; public: void test2(){ std::vector<std::future<void>> cac; for(int n=0;n<5;n++){ cac.push_back( pool.push([&](int threadId){ int accu=0; for(int i=0;i<10000;i++){ accu+=i; } std::cout<<accu<<" access c="<<c->d<<std::endl; }) ); } for(auto& ele : cac){ ele.get(); } } }; int main(){ C c; B b; b.c=&c; b.test(); std::cout<<"end"<<std::endl; }
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
🔥🔥 +100,000$ in 1 month 🔥🚨
JavaScript | 9 sec ago | 0.65 KB
⭐⭐ FREE BTC GUIDE ✅ Working ⭐⭐
JavaScript | 1 min ago | 0.67 KB
💎 2OOO$ 15 MIN INSANE METHOD 📌🔥
JavaScript | 1 min ago | 0.65 KB
📌📌 Swapzone Glitch 2000/15MIN 💵🔥
JavaScript | 3 min ago | 0.65 KB
⭐⭐ Free Crypto Method ⭐⭐
JavaScript | 3 min ago | 0.67 KB
💰💰 ChangeNOW Exploit
JavaScript | 3 min ago | 0.67 KB
⭐⭐ Instant Money Method ⭐ 💵📌
JavaScript | 5 min ago | 0.65 KB
⭐⭐ INSTANT MONEY EXPLOIT ⭐⭐
JavaScript | 6 min ago | 0.67 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!