BS::thread_pool : ไลบรารีพูล C ++ 17 ที่รวดเร็วและใช้งานง่ายและใช้งานง่าย โดย Barak Shoshany
อีเมล: [email protected]
เว็บไซต์: https://baraksh.com/
GitHub: https://github.com/bshoshany
นี่เป็นเอกสารที่สมบูรณ์สำหรับ v4.1.0 ของห้องสมุดที่วางจำหน่ายเมื่อปี 2024-03-22
BS::multi_future<T>BS::synced_streamBS::timerBS::signallerBS_thread_pool.hpp )BS::thread_poolBS::thread_poolBS::this_thread เนมสเปซBS::multi_future<T> คลาสBS_thread_pool_utils.hpp )BS::signallerBS::synced_streamBS::timer มัลติเธรดเป็นสิ่งจำเป็นสำหรับการคำนวณประสิทธิภาพสูงที่ทันสมัย ตั้งแต่ C ++ 11 ไลบรารีมาตรฐาน C ++ ได้รวมการสนับสนุนมัลติเธรดระดับต่ำในตัวโดยใช้โครงสร้างเช่น std::thread อย่างไรก็ตาม std::thread สร้างเธรดใหม่ทุกครั้งที่เรียกว่าซึ่งสามารถมีค่าใช้จ่ายที่สำคัญ นอกจากนี้ยังเป็นไปได้ที่จะสร้างเธรดมากกว่าฮาร์ดแวร์สามารถจัดการได้พร้อมกันซึ่งอาจทำให้เกิดการชะลอตัวลงอย่างมาก
ห้องสมุดที่นำเสนอที่นี่มีคลาสพูลเธรด C ++, BS::thread_pool ซึ่งหลีกเลี่ยงปัญหาเหล่านี้โดยการสร้างพูลของเธรดคงที่หนึ่งครั้งและสำหรับทั้งหมดแล้วนำกลับมาใช้ซ้ำอย่างต่อเนื่องเพื่อทำงานที่แตกต่างกันตลอดอายุการใช้งานของโปรแกรม โดยค่าเริ่มต้นจำนวนเธรดในพูลจะเท่ากับจำนวนเธรดสูงสุดที่ฮาร์ดแวร์สามารถทำงานแบบขนานได้
ผู้ใช้ส่งงานที่จะดำเนินการในคิว เมื่อใดก็ตามที่เธรดพร้อมใช้งานจะดึงงานถัดไปจากคิวและดำเนินการ พูลผลิต std::future สำหรับแต่ละงานโดยอัตโนมัติซึ่งช่วยให้ผู้ใช้สามารถรอให้งานเสร็จสิ้นการดำเนินการและ/หรือรับค่าคืนในที่สุดหากมี เธรดและงานได้รับการจัดการอย่างอิสระโดยพูลในพื้นหลังโดยไม่ต้องใช้อินพุตใด ๆ จากผู้ใช้นอกเหนือจากการส่งงานที่ต้องการ
การออกแบบห้องสมุดนี้ได้รับคำแนะนำจากหลักการสำคัญสี่ประการ ขั้นแรก ความกะทัดรัด : ห้องสมุดทั้งหมดประกอบด้วยไฟล์ส่วนหัวที่มีอยู่ในตัวเองเพียงหนึ่งไฟล์โดยไม่มีส่วนประกอบหรือการอ้างอิงอื่น ๆ นอกเหนือจากไฟล์ส่วนหัวที่มีอยู่ในตัวเองขนาดเล็กพร้อมยูทิลิตี้เสริม ประการที่สอง การพกพา : ห้องสมุดใช้เฉพาะไลบรารี C ++ 17 โดยไม่ต้องพึ่งพาส่วนขยายคอมไพเลอร์หรือไลบรารีที่ 3 ของบุคคลที่ 3 ดังนั้นจึงเข้ากันได้กับคอมไพเลอร์ C ++ 17 ที่ทันสมัยในแพลตฟอร์มใด ๆ ประการที่สาม ความสะดวกในการใช้งาน : ห้องสมุดได้รับการบันทึกไว้อย่างกว้างขวางและโปรแกรมเมอร์ทุกระดับควรจะสามารถใช้งานได้ทันที
หลักการชี้นำที่สี่และสุดท้ายคือ ประสิทธิภาพ : แต่ละบรรทัดของรหัสในห้องสมุดนี้ได้รับการออกแบบอย่างระมัดระวังโดยคำนึงถึงประสิทธิภาพสูงสุดในใจและประสิทธิภาพได้รับการทดสอบและตรวจสอบเกี่ยวกับคอมไพเลอร์และแพลตฟอร์มที่หลากหลาย อันที่จริงห้องสมุดได้รับการออกแบบมาเพื่อใช้ในโครงการคอมพิวเตอร์ทางวิทยาศาสตร์ที่ใช้การคำนวณอย่างเข้มข้นของผู้เขียนโดยใช้ทั้งคอมพิวเตอร์เดสก์ท็อป/แล็ปท็อประดับสูงและโหนดคอมพิวเตอร์ประสิทธิภาพสูง
ไลบรารีมัลติเธรดขั้นสูงอื่น ๆ อาจมีคุณสมบัติมากขึ้นและ/หรือประสิทธิภาพที่สูงขึ้น อย่างไรก็ตามโดยทั่วไปแล้วพวกเขาจะประกอบด้วยรหัสเบสขนาดใหญ่ที่มีส่วนประกอบและการพึ่งพาหลายอย่างและเกี่ยวข้องกับ APIs ที่ซับซ้อนซึ่งต้องใช้เวลาในการเรียนรู้อย่างมากในการเรียนรู้ ไลบรารีนี้ไม่ได้มีวัตถุประสงค์เพื่อแทนที่ไลบรารีขั้นสูงเหล่านี้ แต่มันถูกออกแบบมาสำหรับผู้ใช้ที่ไม่ต้องการคุณสมบัติขั้นสูงมากและชอบไลบรารีที่เรียบง่ายและมีน้ำหนักเบาซึ่งง่ายต่อการเรียนรู้และใช้งานและสามารถรวมเข้ากับโครงการที่มีอยู่หรือใหม่ได้อย่างง่ายดาย
#include "BS_thread_pool.hpp" และคุณก็พร้อมแล้ว!submit_task() จะสร้าง std::future โดยอัตโนมัติซึ่งสามารถใช้เพื่อรอให้งานเสร็จสิ้นการดำเนินการและ/หรือรับค่าคืนในที่สุดsubmit_loop() ซึ่งส่งคืน BS::multi_future ที่สามารถใช้ในการติดตามการดำเนินการของงานขนานทั้งหมดในครั้งเดียวdetach_task() และลูปสามารถขนานกันโดยใช้ detach_loop() - การเสียสละความสะดวกสบายเพื่อประสิทธิภาพที่ยิ่งใหญ่กว่า ในกรณีนั้น wait() , wait_for() และ wait_until() สามารถใช้เพื่อรองานทั้งหมดในคิวให้เสร็จสมบูรณ์BS_thread_pool_test.cpp สามารถใช้เพื่อทำการทดสอบอัตโนมัติและมาตรฐานที่ครบถ้วนสมบูรณ์และยังทำหน้าที่เป็นตัวอย่างที่ครอบคลุมของวิธีการใช้ห้องสมุดอย่างเหมาะสม สคริปต์ PowerShell BS_thread_pool_test.ps1 รวมถึงวิธีพกพาในการเรียกใช้การทดสอบด้วยคอมไพเลอร์หลายตัวBS_thread_pool_utils.hpp มีคลาสยูทิลิตี้ที่มีประโยชน์หลายคลาสBS::signallerBS::synced_streamBS::timerdetach_sequence() และ submit_sequence()reset()get_tasks_queued() , get_tasks_running() และ get_tasks_total() ฟังก์ชั่นสมาชิกget_thread_count()pause() , unpause() และ is_paused() ฟังก์ชั่น; เมื่อหยุดชั่วคราวเธรดจะไม่ดึงงานใหม่ออกจากคิวpurge()submit_task() หรือ submit_loop() จากหัวข้อหลักผ่านอนาคตของพวกเขาBS::this_thread::get_index() และตัวชี้ไปยังพูลที่เป็นเจ้าของเธรดโดยใช้ BS::this_thread::get_pool()get_thread_ids() หรือด้ามจับเธรดที่กำหนดโดยใช้ฟังก์ชันสมาชิก get_native_handles() เสริมห้องสมุดนี้ควรรวบรวมคอมไพเลอร์ C ++ 17 ที่สอดคล้องกับมาตรฐานใด ๆ ในระบบปฏิบัติการและสถาปัตยกรรมทั้งหมดที่มีคอมไพเลอร์ ความเข้ากันได้ได้รับการตรวจสอบด้วย 24-core (8p+16e) / 32-thread Intel I9-13900K CPU โดยใช้คอมไพเลอร์และแพลตฟอร์มต่อไปนี้:
นอกจากนี้ห้องสมุดนี้ยังได้รับการทดสอบใน Digital Research Alliance of Canada Node ที่ติดตั้ง CPU สองตัว 20 คอร์ / 40-Thread Gold 6148 (รวมทั้งหมด 40 คอร์และ 80 เธรด) โดยใช้ Centos Linux 7.9.2009 โดยใช้ GCC V13.2.0
โปรแกรมทดสอบ BS_thread_pool_test.cpp ถูกรวบรวมโดยไม่มีคำเตือน (ด้วยธงคำเตือน -Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow ใน GCC /clang และ /W4 ใน MSVC)
เนื่องจากไลบรารีนี้ต้องการคุณสมบัติ C ++ 17 รหัสจะต้องรวบรวมด้วยการสนับสนุน C ++ 17:
-std=c++17 FLAG บน Linux คุณจะต้องใช้ธง -pthread เพื่อเปิดใช้งานไลบรารี Posix Threads/std:c++17 และยัง /permissive- เพื่อให้แน่ใจว่ามาตรฐานสอดคล้องกันเพื่อประสิทธิภาพสูงสุดขอแนะนำให้รวบรวมด้วยการเพิ่มประสิทธิภาพคอมไพเลอร์ที่มีอยู่ทั้งหมด:
-O3/O2 ตัวอย่างเช่นในการรวบรวมโปรแกรมทดสอบ BS_thread_pool_test.cpp พร้อมคำเตือนและการเพิ่มประสิทธิภาพขอแนะนำให้ใช้คำสั่งต่อไปนี้:
g++ BS_thread_pool_test.cpp -std=c++17 -O3 -Wall -Wextra -Wconversion -Wsign-conversion -Wpedantic -Weffc++ -Wshadow -pthread -o BS_thread_pool_testg++ ด้วย clang++-o BS_thread_pool_test ด้วย -o BS_thread_pool_test.exe และลบ -pthreadcl BS_thread_pool_test.cpp /std:c++17 /permissive- /O2 /W4 /EHsc /Fo:BS_thread_pool_test.obj /Fe:BS_thread_pool_test.exe ในการติดตั้ง BS::thread_pool เพียงดาวน์โหลดรุ่นล่าสุดจากที่เก็บ GitHub ให้วางไฟล์ส่วนหัว BS_thread_pool.hpp จากโฟลเดอร์ include ในโฟลเดอร์ที่ต้องการและรวมไว้ในโปรแกรมของคุณ:
# include " BS_thread_pool.hpp " พูลเธรดจะสามารถเข้าถึงได้ผ่านคลาส BS::thread_pool สำหรับการติดตั้งที่รวดเร็วยิ่งขึ้นคุณสามารถดาวน์โหลดไฟล์ส่วนหัวได้โดยตรงที่ URL นี้
ไลบรารีนี้ยังมาพร้อมกับไฟล์ส่วนหัวยูทิลิตี้อิสระ BS_thread_pool_utils.hpp ซึ่งไม่จำเป็นต้องใช้พูลเธรด แต่มีคลาสยูทิลิตี้บางอย่างที่อาจเป็นประโยชน์สำหรับการมัลติเธรด ไฟล์ส่วนหัวนี้ยังอยู่ในโฟลเดอร์ include สามารถดาวน์โหลดได้โดยตรงที่ URL นี้
ห้องสมุดนี้ยังมีอยู่ในผู้จัดการแพ็คเกจและระบบสร้างต่างๆรวมถึง VCPKG, Conan, Meson และ CMake ด้วย CPM โปรดดูรายละเอียดเพิ่มเติมด้านล่าง
ตัวสร้างเริ่มต้นสร้างพูลเธรดที่มีเธรดมากเท่าที่ฮาร์ดแวร์สามารถจัดการได้พร้อมกันตามที่รายงานโดยการใช้งานผ่าน std::thread::hardware_concurrency() โดยปกติจะถูกกำหนดโดยจำนวนแกนใน CPU หากแกนกลางเป็น hyperthreaded มันจะนับเป็นสองเธรด ตัวอย่างเช่น:
// Constructs a thread pool with as many threads as available in the hardware.
BS::thread_pool pool;เป็นทางเลือกว่าจะมีเธรดจำนวนหนึ่งที่แตกต่างจากฮาร์ดแวร์พร้อมกันสามารถระบุได้ว่าเป็นอาร์กิวเมนต์ของตัวสร้าง อย่างไรก็ตามโปรดทราบว่าการเพิ่มเธรดมากกว่าฮาร์ดแวร์สามารถจัดการได้จะ ไม่ ปรับปรุงประสิทธิภาพและในความเป็นจริงแล้วน่าจะขัดขวางมันมากที่สุด ตัวเลือกนี้มีอยู่เพื่ออนุญาตให้ใช้เธรด น้อยกว่า ฮาร์ดแวร์พร้อมกันในกรณีที่คุณต้องการทิ้งเธรดบางอย่างไว้สำหรับกระบวนการอื่น ๆ ตัวอย่างเช่น:
// Constructs a thread pool with only 12 threads.
BS::thread_pool pool ( 12 );โดยปกติเมื่อใช้พูลเธรดเธรดหลักของโปรแกรมควรส่งงานไปยังพูลเธรดและรอให้พวกเขาเสร็จสิ้นและไม่ควรทำงานใด ๆ ที่ต้องคำนวณด้วยตัวเอง ในกรณีนี้ขอแนะนำให้ใช้ค่าเริ่มต้นสำหรับจำนวนเธรด สิ่งนี้ทำให้มั่นใจได้ว่าเธรดทั้งหมดที่มีอยู่ในฮาร์ดแวร์จะถูกนำไปใช้ในขณะที่เธรดหลักรอ
ฟังก์ชันสมาชิก get_thread_count() ส่งคืนจำนวนเธรดในพูล สิ่งนี้จะเท่ากับ std::thread::hardware_concurrency() หากใช้ตัวสร้างเริ่มต้น
โดยทั่วไปไม่จำเป็นต้องเปลี่ยนจำนวนเธรดในพูลหลังจากสร้างขึ้นเนื่องจากจุดทั้งหมดของพูลเธรดคือคุณสร้างเธรดเพียงครั้งเดียวเท่านั้น อย่างไรก็ตามหากจำเป็นสิ่งนี้สามารถทำได้อย่างปลอดภัยและตรงไปตรงมาโดยใช้ฟังก์ชั่นสมาชิก reset()
reset() จะรอให้งานที่ทำงานทั้งหมดเสร็จสมบูรณ์ แต่จะทิ้งงานที่เหลือไว้ในคิว จากนั้นมันจะทำลายพูลเธรดและสร้างใหม่ด้วยจำนวนเธรดใหม่ที่ต้องการตามที่ระบุไว้ในอาร์กิวเมนต์ของฟังก์ชั่น (หรือฮาร์ดแวร์พร้อมกันหากไม่มีการโต้แย้ง) พูลเธรดใหม่จะดำเนินการต่อการดำเนินงานที่ยังคงอยู่ในคิวและงานที่ส่งใหม่ใด ๆ
หากต้องการเวอร์ชันของไลบรารีนี้อาจถูกอ่านในระหว่างการรวบรวมเวลาจากสามแมโครต่อไปนี้:
BS_THREAD_POOL_VERSION_MAJOR - ระบุเวอร์ชันหลักBS_THREAD_POOL_VERSION_MINOR - ระบุรุ่นรองBS_THREAD_POOL_VERSION_PATCH - ระบุเวอร์ชันแพตช์ std::cout << " Thread pool library version is " << BS_THREAD_POOL_VERSION_MAJOR << ' . ' << BS_THREAD_POOL_VERSION_MINOR << ' . ' << BS_THREAD_POOL_VERSION_PATCH << " . n " ;ตัวอย่างเอาท์พุท:
Thread pool library version is 4.1.0.
ตัวอย่างเช่นสิ่งนี้สามารถใช้เพื่ออนุญาตให้ฐานรหัสเดียวกันทำงานกับไลบรารีที่เข้ากันไม่ได้หลายรุ่นโดยใช้คำสั่ง #if
หมายเหตุ: คุณสมบัตินี้ใช้ได้เฉพาะกับ v4.0.1 การเผยแพร่ก่อนหน้านี้ของไลบรารีนี้ไม่ได้กำหนดแมโครเหล่านี้
ในส่วนนี้เราจะได้เรียนรู้วิธีการส่งงานที่ไม่มีข้อโต้แย้ง แต่อาจมีค่าส่งคืนไปยังคิว เมื่อมีการส่งงานแล้วมันจะถูกดำเนินการทันทีที่มีเธรดพร้อมใช้งาน งานจะถูกดำเนินการตามลำดับที่พวกเขาถูกส่ง (ครั้งแรก, แรกออก) เว้นแต่จะมีการเปิดใช้งานลำดับความสำคัญของงาน (ดูด้านล่าง)
ตัวอย่างเช่นหากสระว่ายน้ำมี 8 เธรดและคิวเปล่าและเราส่งงาน 16 งานแล้วเราควรคาดหวังว่างาน 8 ชิ้นแรกจะถูกดำเนินการในคู่ขนานโดยมีงานที่เหลืออยู่ที่เธรดทีละตัว
ฟังก์ชั่นสมาชิก submit_task() ใช้เพื่อส่งงานไปยังคิว ต้องใช้อินพุตหนึ่งรายการที่จะส่ง งานนี้จะต้องเป็นฟังก์ชั่นที่ไม่มีอาร์กิวเมนต์ แต่สามารถมีค่าคืนได้ ค่าผลตอบแทนคือ std::future ที่เกี่ยวข้องกับงาน
หากฟังก์ชั่นที่ส่งมีค่าส่งคืนของประเภท T แล้วอนาคตจะเป็นประเภท std::future<T> และจะถูกตั้งค่าเป็นค่าคืนเมื่อฟังก์ชั่นเสร็จสิ้นการดำเนินการ หากฟังก์ชั่นที่ส่งไม่มีค่าส่งคืนอนาคตจะเป็น std::future<void> ซึ่งจะไม่ส่งคืนค่าใด ๆ แต่อาจยังคงใช้เพื่อรอให้ฟังก์ชั่นเสร็จสิ้น
การใช้ auto สำหรับค่าส่งคืนของ submit_task() หมายถึงคอมไพเลอร์จะตรวจจับโดยอัตโนมัติว่าอินสแตนซ์ของเทมเพลต std::future ที่จะใช้ อย่างไรก็ตามการระบุประเภทเฉพาะ std::future<T> เช่นเดียวกับในตัวอย่างด้านล่างขอแนะนำให้อ่านได้เพิ่มขึ้น
หากต้องการรอจนกว่างานจะเสร็จสิ้นให้ใช้ฟังก์ชั่นสมาชิก wait() ในอนาคต ในการรับค่าส่งคืนให้ใช้ฟังก์ชั่นสมาชิก get() ซึ่งจะรอให้งานเสร็จโดยอัตโนมัติหากยังไม่ได้ นี่คือตัวอย่างง่ายๆ:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < future > // std::future
# include < iostream > // std::cout
int the_answer ()
{
return 42 ;
}
int main ()
{
BS::thread_pool pool;
std::future< int > my_future = pool. submit_task (the_answer);
std::cout << my_future. get () << ' n ' ;
} ในตัวอย่างนี้เราส่งฟังก์ชั่น the_answer() ซึ่งส่งคืน int ฟังก์ชั่นสมาชิก submit_task() ของพูลจึงส่งคืน std::future<int> จากนั้นเราใช้ฟังก์ชั่นสมาชิก get() ในอนาคตเพื่อรับค่าคืนและพิมพ์ออกมา
นอกเหนือจากการส่งฟังก์ชั่นที่กำหนดไว้ล่วงหน้าแล้วเรายังสามารถใช้นิพจน์แลมบ์ดาเพื่อกำหนดงานได้อย่างรวดเร็ว เขียนตัวอย่างก่อนหน้านี้ใหม่ในแง่ของนิพจน์แลมบ์ดาเราได้รับ:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < future > // std::future
# include < iostream > // std::cout
int main ()
{
BS::thread_pool pool;
std::future< int > my_future = pool. submit_task ([]{ return 42 ; });
std::cout << my_future. get () << ' n ' ;
} ที่นี่นิพจน์แลมบ์ดา []{ return 42; } มีสองส่วน:
[] สิ่งนี้บ่งบอกถึงคอมไพเลอร์ว่ามีการกำหนดนิพจน์แลมบ์ดา{ return 42; } ที่ส่งคืนค่า 42โดยทั่วไปแล้วมันง่ายกว่าและเร็วกว่าในการส่งการแสดงออกของแลมบ์ดามากกว่าฟังก์ชั่นที่กำหนดไว้ล่วงหน้าโดยเฉพาะอย่างยิ่งเนื่องจากความสามารถในการจับตัวแปรท้องถิ่นซึ่งเราจะพูดถึงในส่วนถัดไป
แน่นอนว่างานไม่จำเป็นต้องส่งคืนค่า ในตัวอย่างต่อไปนี้เราส่งฟังก์ชั่นที่ไม่มีค่าส่งคืนจากนั้นใช้อนาคตเพื่อรอให้เสร็จสิ้นการดำเนินการ:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < chrono > // std::chrono
# include < future > // std::future
# include < iostream > // std::cout
# include < thread > // std::this_thread
int main ()
{
BS::thread_pool pool;
const std::future< void > my_future = pool. submit_task (
[]
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 500 ));
});
std::cout << " Waiting for the task to complete... " ;
my_future. wait ();
std::cout << " Done. " << ' n ' ;
} ที่นี่เราแบ่งแลมบ์ดาออกเป็นหลายบรรทัดเพื่อให้สามารถอ่านได้มากขึ้น คำสั่ง std::this_thread::sleep_for(std::chrono::milliseconds(500)) สั่งให้งานนอนเพียง 500 มิลลิวินาทีจำลองงานที่ต้องใช้การคำนวณอย่างเข้มข้น
ตามที่ระบุไว้ในส่วนก่อนหน้างานที่ส่งโดยใช้ submit_task() ไม่สามารถมีอาร์กิวเมนต์ใด ๆ อย่างไรก็ตามมันเป็นเรื่องง่ายที่จะส่งงานด้วยอาร์กิวเมนต์โดยการห่อฟังก์ชั่นในแลมบ์ดาหรือใช้แลมบ์ดาจับโดยตรง นี่คือสองตัวอย่าง
ต่อไปนี้เป็นตัวอย่างของการส่งฟังก์ชั่นที่กำหนดไว้ล่วงหน้าพร้อมอาร์กิวเมนต์โดยห่อด้วยแลมบ์ดา:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < future > // std::future
# include < iostream > // std::cout
double multiply ( const double lhs, const double rhs)
{
return lhs * rhs;
}
int main ()
{
BS::thread_pool pool;
std::future< double > my_future = pool. submit_task (
[]
{
return multiply ( 6 , 7 );
});
std::cout << my_future. get () << ' n ' ;
} อย่างที่คุณเห็นเพื่อผ่านข้อโต้แย้งเพื่อ multiply เราเพียงแค่เรียกว่า multiply(6, 7) อย่างชัดเจนภายในแลมบ์ดา หากข้อโต้แย้งไม่ใช่ตัวอักษรเราจำเป็นต้องใช้ประโยคการจับแลมบ์ดาเพื่อจับข้อโต้แย้งจากขอบเขตท้องถิ่น:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < future > // std::future
# include < iostream > // std::cout
double multiply ( const double lhs, const double rhs)
{
return lhs * rhs;
}
int main ()
{
BS::thread_pool pool;
constexpr double first = 6 ;
constexpr double second = 7 ;
std::future< double > my_future = pool. submit_task (
[first, second]
{
return multiply (first, second);
});
std::cout << my_future. get () << ' n ' ;
} เราสามารถกำจัดฟังก์ multiply ทั้งหมดและใส่ทุกอย่างไว้ในแลมบ์ดาได้หากต้องการ:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < future > // std::future
# include < iostream > // std::cout
int main ()
{
BS::thread_pool pool;
constexpr double first = 6 ;
constexpr double second = 7 ;
std::future< double > my_future = pool. submit_task (
[first, second]
{
return first * second;
});
std::cout << my_future. get () << ' n ' ;
} โดยปกติแล้วจะเป็นการดีที่สุดที่จะส่งงานไปยังคิวโดยใช้ submit_task() สิ่งนี้จะช่วยให้คุณรอให้งานเสร็จและ/หรือรับค่าส่งคืนในภายหลัง อย่างไรก็ตามบางครั้งไม่จำเป็นต้องมีอนาคตเช่นเมื่อคุณต้องการ "ตั้งค่าและลืม" งานบางอย่างหรือหากงานสื่อสารกับเธรดหลักหรืองานอื่น ๆ โดยไม่ต้องใช้ฟิวเจอร์สเช่นผ่านตัวแปรเงื่อนไข
ในกรณีเช่นนี้คุณอาจต้องการหลีกเลี่ยงค่าใช้จ่ายที่เกี่ยวข้องในการกำหนดอนาคตให้กับงานเพื่อเพิ่มประสิทธิภาพ สิ่งนี้เรียกว่า "การแยก" งานเป็นงานที่แยกออกจากเธรดหลักและทำงานอย่างอิสระ
งานถอดออกโดยใช้ฟังก์ชันสมาชิก detach_task() ซึ่งช่วยให้คุณสามารถแยกงานไปยังคิวโดยไม่ต้องสร้างอนาคต งานสามารถมีจำนวนอาร์กิวเมนต์ใด ๆ แต่ไม่สามารถมีค่าคืนได้เนื่องจากจะไม่มีทางที่เธรดหลักจะดึงค่านั้นได้
เนื่องจาก detach_task() ไม่กลับมาในอนาคตจึงไม่มีวิธีการในตัวสำหรับผู้ใช้ที่จะรู้ว่างานเสร็จสิ้นเมื่อเสร็จสิ้นการดำเนินการ คุณต้องตรวจสอบให้แน่ใจว่างานเสร็จสิ้นการดำเนินการก่อนที่จะพยายามใช้สิ่งที่ขึ้นอยู่กับผลลัพธ์ของมัน มิฉะนั้นสิ่งเลวร้ายจะเกิดขึ้น!
BS::thread_pool ให้ฟังก์ชั่นสมาชิก wait() เพื่ออำนวยความสะดวกในการรองานทั้งหมดในคิวให้เสร็จสมบูรณ์ไม่ว่าพวกเขาจะถูกถอดออกหรือส่งในอนาคต ฟังก์ชั่นสมาชิก wait() ทำงานคล้ายกับฟังก์ชั่นสมาชิก wait() ของ std::future พิจารณาตัวอย่างเช่นรหัสต่อไปนี้:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < chrono > // std::chrono
# include < iostream > // std::cout
# include < thread > // std::this_thread
int main ()
{
BS::thread_pool pool;
int result = 0 ;
pool. detach_task (
[&result]
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 100 ));
result = 42 ;
});
std::cout << result << ' n ' ;
} โปรแกรมนี้จะกำหนดตัวแปรท้องถิ่นชื่อ result และเริ่มต้นเป็น 0 จากนั้นจะแยกงานในรูปแบบของการแสดงออกของแลมบ์ดา โปรดทราบว่าแลมบ์ดาจับ result โดยการอ้างอิง ตามที่ระบุโดย & ด้านหน้าของมัน ซึ่งหมายความว่างานสามารถแก้ไข result และการปรับเปลี่ยนดังกล่าวจะสะท้อนให้เห็นในเธรดหลัก การเปลี่ยนแปลงงาน result ให้ 42 แต่ก่อนจะนอนหลับเป็นเวลา 100 มิลลิวินาที เมื่อเธรดหลักพิมพ์ค่าของ result งานยังไม่มีเวลาแก้ไขค่าของมันเนื่องจากยังคงนอนหลับอยู่ ดังนั้นโปรแกรมจะพิมพ์ค่าเริ่มต้น 0
เพื่อรอให้งานเสร็จสมบูรณ์เราต้องใช้ฟังก์ชั่นสมาชิก wait() หลังจากแยกออก:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < chrono > // std::chrono
# include < iostream > // std::cout
# include < thread > // std::this_thread
int main ()
{
BS::thread_pool pool;
int result = 0 ;
pool. detach_task (
[&result]
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 100 ));
result = 42 ;
});
pool. wait ();
std::cout << result << ' n ' ;
} ตอนนี้โปรแกรมจะพิมพ์ค่า 42 ตามที่คาดไว้ อย่างไรก็ตามหมายเหตุ wait() จะรองาน ทั้งหมด ในคิวรวมถึงงานอื่น ๆ ที่อาจส่งมาก่อนหรือหลังงานที่เราสนใจ หากเราต้องการรอเพียงงานเดียว submit_task() จะเป็นตัวเลือกที่ดีกว่า
บางครั้งคุณอาจต้องการรอให้งานเสร็จสมบูรณ์ แต่ในระยะเวลาหนึ่งหรือจนกว่าจะถึงเวลาที่กำหนด ตัวอย่างเช่นหากงานยังไม่เสร็จสิ้นหลังจากผ่านไประยะหนึ่งคุณอาจต้องการแจ้งให้ผู้ใช้ทราบว่ามีความล่าช้า
สำหรับงานที่ส่งมาพร้อมกับฟิวเจอร์สโดยใช้ submit_task() สิ่งนี้สามารถทำได้โดยใช้ฟังก์ชั่นสมาชิกสองคนของ std::future :
wait_for() รอให้งานเสร็จสมบูรณ์ แต่หยุดรอหลังจากระยะเวลาที่กำหนดซึ่งกำหนดให้เป็นอาร์กิวเมนต์ประเภท std::chrono::duration ผ่านไปแล้วwait_until() รอให้งานเสร็จสมบูรณ์ แต่หยุดรอหลังจากจุดเวลาที่กำหนดซึ่งได้รับเป็นอาร์กิวเมนต์ประเภท std::chrono::time_point ได้รับการติดต่อแล้ว ในทั้งสองกรณีฟังก์ชั่นจะส่งคืน future_status::ready ถ้าอนาคตพร้อมหมายความว่างานจะเสร็จสิ้นและได้รับค่าผลตอบแทนหากมี อย่างไรก็ตามมันจะส่งคืน std::future_status::timeout หากอนาคตยังไม่พร้อมตามเวลาที่หมดเวลาหมดอายุ
นี่คือตัวอย่าง:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < chrono > // std::chrono
# include < future > // std::future
# include < iostream > // std::cout
# include < thread > // std::this_thread
int main ()
{
BS::thread_pool pool;
const std::future< void > my_future = pool. submit_task (
[]
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
std::cout << " Task done! n " ;
});
while ( true )
{
if (my_future. wait_for ( std::chrono::milliseconds ( 200 )) != std::future_status::ready)
std::cout << " Sorry, the task is not done yet. n " ;
else
break ;
}
}ผลลัพธ์ควรมีลักษณะคล้ายกับนี้:
Sorry, the task is not done yet.
Sorry, the task is not done yet.
Sorry, the task is not done yet.
Sorry, the task is not done yet.
Task done!
สำหรับงานเดี่ยวเนื่องจากเราไม่มีอนาคตสำหรับพวกเขาเราจึงไม่สามารถใช้วิธีนี้ได้ อย่างไรก็ตาม BS::thread_pool มีฟังก์ชั่นสมาชิกสองฟังก์ชั่นชื่อ wait_for() และ wait_until() ซึ่งในทำนองเดียวกันรอระยะเวลาที่กำหนดหรือจนกว่าจะถึงเวลาที่กำหนด แต่ทำเช่นนั้นสำหรับงาน ทั้งหมด (ไม่ว่าจะส่งหรือแยกออก) แทนที่จะเป็น std::future_status ฟังก์ชั่นการรอของพูลเธรดจะกลับมา true หากงานทั้งหมดเสร็จสิ้นหรือ false ถ้าระยะเวลาหมดอายุหรือถึงเวลาถึงเวลา แต่งานบางอย่างยังคงทำงานอยู่
นี่คือตัวอย่างเดียวกับข้างต้นโดยใช้ detach_task() และ pool.wait_for() :
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < chrono > // std::chrono
# include < iostream > // std::cout
# include < thread > // std::this_thread
int main ()
{
BS::thread_pool pool;
pool. detach_task (
[]
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
std::cout << " Task done! n " ;
});
while ( true )
{
if (!pool. wait_for ( std::chrono::milliseconds ( 200 )))
std::cout << " Sorry, the task is not done yet. n " ;
else
break ;
}
}ให้เราพิจารณาโปรแกรมต่อไปนี้:
# include < iostream > // std::cout, std::boolalpha
class flag_class
{
public:
[[nodiscard]] bool get_flag () const
{
return flag;
}
void set_flag ( const bool arg)
{
flag = arg;
}
private:
bool flag = false ;
};
int main ()
{
flag_class flag_object;
flag_object. set_flag ( true );
std::cout << std::boolalpha << flag_object. get_flag () << ' n ' ;
} โปรแกรมนี้สร้างวัตถุใหม่ flag_object ของคลาส flag_class ตั้งค่าสถานะเป็น true โดยใช้ฟังก์ชั่นสมาชิก setter set_flag() จากนั้นพิมพ์ค่าของธงโดยใช้ฟังก์ชั่นสมาชิก getter get_flag()
ถ้าเราต้องการส่งฟังก์ชั่นสมาชิก set_flag() เป็นงานไปยังพูลเธรด เราเพียงแค่สรุปคำสั่งทั้งหมด flag_object.set_flag(true); จากบรรทัดในแลมบ์ดาและส่ง flag_object ไปยังแลมบ์ดาโดยอ้างอิงดังตัวอย่างนี้:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iostream > // std::cout, std::boolalpha
class flag_class
{
public:
[[nodiscard]] bool get_flag () const
{
return flag;
}
void set_flag ( const bool arg)
{
flag = arg;
}
private:
bool flag = false ;
};
int main ()
{
BS::thread_pool pool;
flag_class flag_object;
pool. submit_task (
[&flag_object]
{
flag_object. set_flag ( true );
})
. wait ();
std::cout << std::boolalpha << flag_object. get_flag () << ' n ' ;
} แน่นอนว่าสิ่งนี้จะทำงานร่วมกับ detach_task() ถ้าเราเรียกว่า wait() บนสระว่ายน้ำเองแทนที่จะเป็นอนาคตที่กลับมา
โปรดทราบว่าในตัวอย่างนี้แทนที่จะได้รับอนาคตจาก submit_task() จากนั้นรออนาคตนั้นเราก็เรียกว่า wait() ในอนาคตนั้นทันที นี่เป็นวิธีทั่วไปในการรอให้งานเสร็จสมบูรณ์หากเราไม่มีอะไรทำในระหว่างนี้ โปรดทราบว่าเราผ่าน flag_object โดยอ้างอิงกับแลมบ์ดาเนื่องจากเราต้องการตั้งค่าสถานะบนวัตถุเดียวกันนั้นไม่ใช่สำเนาของมัน (การส่งผ่านตามค่าจะไม่ทำงาน const ไป
อีกสิ่งหนึ่งที่คุณอาจต้องการทำคือเรียกฟังก์ชั่นสมาชิกจากภายในวัตถุนั่นคือจากฟังก์ชั่นสมาชิกอื่น สิ่งนี้เป็นไปตามไวยากรณ์ที่คล้ายกันยกเว้นว่าคุณจะต้องจับ this (เช่นตัวชี้ไปยังวัตถุปัจจุบัน) ในแลมบ์ดา นี่คือตัวอย่าง:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iostream > // std::cout, std::boolalpha
BS::thread_pool pool;
class flag_class
{
public:
[[nodiscard]] bool get_flag () const
{
return flag;
}
void set_flag ( const bool arg)
{
flag = arg;
}
void set_flag_to_true ()
{
pool. submit_task (
[ this ]
{
set_flag ( true );
})
. wait ();
}
private:
bool flag = false ;
};
int main ()
{
flag_class flag_object;
flag_object. set_flag_to_true ();
std::cout << std::boolalpha << flag_object. get_flag () << ' n ' ;
} โปรดทราบว่าในตัวอย่างนี้เราได้กำหนดพูลเธรดเป็นวัตถุระดับโลกเพื่อให้สามารถเข้าถึงได้นอกฟังก์ชั่น main()
หนึ่งในวิธีที่พบได้บ่อยที่สุดและมีประสิทธิภาพในการขนานคือการแยกลูปออกเป็นห่วงขนาดเล็กและเรียกใช้ในแบบคู่ขนาน มันมีประสิทธิภาพมากที่สุดในการคำนวณ "ขนานกันอย่างน่าอาย" เช่นการดำเนินการเวกเตอร์หรือเมทริกซ์ซึ่งการวนซ้ำแต่ละครั้งของลูปนั้นเป็นอิสระจากการทำซ้ำกันอย่างสมบูรณ์
ตัวอย่างเช่นหากเรากำลังรวมเวกเตอร์สองตัวของ 1,000 องค์ประกอบแต่ละรายการและเรามี 10 เธรดเราสามารถแบ่งการรวมเป็น 10 บล็อกของ 100 องค์ประกอบแต่ละรายการและเรียกใช้บล็อกทั้งหมดในแบบคู่ขนานซึ่งอาจเพิ่มประสิทธิภาพได้สูงสุด 10
BS::thread_pool สามารถทำให้ลูปขนานโดยอัตโนมัติ หากต้องการดูวิธีการทำงานให้พิจารณาลูปทั่วไปต่อไปนี้:
for (T i = start; i < end; ++i)
loop (i);ที่ไหน:
T คือประเภทจำนวนเต็มที่ลงนามหรือไม่ได้ลงนาม[start, end) คือรวมถึง start แต่ไม่รวมถึง endloop() เป็นการดำเนินการที่ดำเนินการสำหรับแต่ละดัชนีลูป i เช่นการปรับเปลี่ยนอาร์เรย์ด้วยองค์ประกอบ end - start ลูปนี้อาจถูกขนานโดยอัตโนมัติและส่งไปยังคิวของพูลเธรดโดยใช้ฟังก์ชั่นสมาชิก submit_loop() ซึ่งมีไวยากรณ์ดังต่อไปนี้:
pool.submit_loop(start, end, loop, num_blocks);ที่ไหน:
start เป็นดัชนีแรกในช่วงend คือดัชนีหลังจากดัชนีสุดท้ายในช่วงเช่นช่วงเต็มคือ [start, end) กล่าวอีกนัยหนึ่งลูปจะเทียบเท่ากับหนึ่งด้านบนถ้า start และ end เหมือนกันstart และ end ทั้งสองจะต้องเป็นจำนวนเต็มชนิดเดียวกัน T ดูด้านล่างสำหรับตัวอย่างของสิ่งที่ต้องทำเมื่อพวกเขาไม่ได้เป็นประเภทเดียวกันend <= start จะไม่มีอะไรเกิดขึ้นloop() เป็นฟังก์ชั่นที่ควรทำงานในการวนซ้ำทุกครั้งของลูปและใช้เวลาหนึ่งอาร์กิวเมนต์ดัชนีลูปnum_blocks คือจำนวนบล็อกของแบบฟอร์ม [a, b) เพื่อแยกลูปออกเป็น ตัวอย่างเช่นหากช่วงคือ [0, 9) และมี 3 บล็อกดังนั้นบล็อกจะเป็นช่วง [0, 3) , [3, 6) และ [6, 9)[0, 100) แบ่งออกเป็น 15 บล็อกผลลัพธ์จะเป็น 10 บล็อกขนาด 7 ซึ่งจะถูกดำเนินการก่อนและ 5 บล็อกขนาด 6แต่ละบล็อกจะถูกส่งไปยังคิวของพูลเธรดเป็นงานแยกต่างหาก ดังนั้นลูปที่แบ่งออกเป็น 3 บล็อกจะถูกแบ่งออกเป็น 3 งานแต่ละงานซึ่งอาจทำงานแบบขนาน หากมีเพียงบล็อกเดียวลูปทั้งหมดจะทำงานเป็นงานเดียวและจะไม่มีการขนานกัน
เพื่อขนานลูปทั่วไปด้านบนเราใช้คำสั่งต่อไปนี้:
BS::multi_future< void > loop_future = pool.submit_loop(start, end, loop, num_blocks);
loop_future.wait(); submit_loop() ส่งคืนวัตถุของเทมเพลตคลาส Helper BS::multi_future นี่คือความเชี่ยวชาญเฉพาะของ std::vector<std::future<T>> ด้วยฟังก์ชั่นสมาชิกเพิ่มเติม บล็อก num_blocks แต่ละตัวจะมี std::future ที่ได้รับมอบหมายและอนาคตทั้งหมดเหล่านี้จะถูกเก็บไว้ใน BS::multi_future ที่ส่งคืน เมื่อเรียกว่า loop_future.wait() เธรดหลักจะรอจนกว่างานทั้งหมดที่สร้างโดย submit_loop() เสร็จสิ้นการดำเนินการเสร็จสิ้นและเฉพาะงานเหล่านั้น - ไม่ใช่งานอื่น ๆ ที่เกิดขึ้นในคิว นี่คือบทบาทของคลาส BS::multi_future : เพื่อรอ กลุ่มงาน เฉพาะในกรณีนี้งานที่ใช้งานบล็อกลูป
คุณควรใช้ค่าใดสำหรับ num_blocks การละเว้นอาร์กิวเมนต์นี้เพื่อให้จำนวนบล็อกจะเท่ากับจำนวนเธรดในพูลมักเป็นตัวเลือกที่ดี เพื่อประสิทธิภาพที่ดีที่สุดขอแนะนำให้ทำเกณฑ์มาตรฐานของคุณเองเพื่อค้นหาจำนวนบล็อกที่เหมาะสมที่สุดสำหรับแต่ละลูป (คุณสามารถใช้ BS::timer Utility Class) การใช้งานน้อยกว่าที่มีเธรดอาจเป็นที่ต้องการหากคุณทำงานอื่น ๆ ในแบบคู่ขนาน การใช้งานมากกว่าที่มีเธรดอาจปรับปรุงประสิทธิภาพในบางกรณี แต่การทำให้เป็นคู่ขนานกับงานมากเกินไปจะได้รับผลตอบแทนที่ลดลง
เป็นตัวอย่างง่ายๆรหัสต่อไปนี้จะคำนวณและพิมพ์ตารางสี่เหลี่ยมของจำนวนเต็มทั้งหมดตั้งแต่ 0 ถึง 99:
# include < iomanip > // std::setw
# include < iostream > // std::cout
int main ()
{
constexpr unsigned int max = 100 ;
unsigned int squares[max];
for ( unsigned int i = 0 ; i < max; ++i)
squares[i] = i * i;
for ( unsigned int i = 0 ; i < max; ++i)
std::cout << std::setw ( 2 ) << i << " ^2 = " << std::setw ( 4 ) << squares[i] << ((i % 5 != 4 ) ? " | " : " n " );
}เราสามารถขนานได้ดังนี้:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iomanip > // std::setw
# include < iostream > // std::cout
int main ()
{
BS::thread_pool pool ( 10 );
constexpr unsigned int max = 100 ;
unsigned int squares[max];
const BS::multi_future< void > loop_future = pool. submit_loop < unsigned int >( 0 , max,
[&squares]( const unsigned int i)
{
squares[i] = i * i;
});
loop_future. wait ();
for ( unsigned int i = 0 ; i < max; ++i)
std::cout << std::setw ( 2 ) << i << " ^2 = " << std::setw ( 4 ) << squares[i] << ((i % 5 != 4 ) ? " | " : " n " );
} เนื่องจากมี 10 เธรดและเราจึงละเว้นอาร์กิวเมนต์ num_blocks ลูปจะถูกแบ่งออกเป็น 10 บล็อกแต่ละการคำนวณ 10 สี่เหลี่ยม
โปรดทราบว่า submit_loop() ถูกดำเนินการกับพารามิเตอร์เทมเพลตที่ชัดเจน <unsigned int> เหตุผลก็คือดัชนีลูปทั้งสองต้องเป็นประเภทเดียวกัน อย่างไรก็ตามที่นี่ max เป็น unsigned int ในขณะที่ 0 คือ in (ลงนาม) int ดังนั้นประเภทไม่ตรงกันและรหัสจะไม่รวบรวมเว้นแต่ว่าเราบังคับให้ 0 เป็นประเภทที่ถูกต้อง สิ่งนี้สามารถทำได้อย่างสง่างามที่สุดโดยการระบุประเภทของดัชนีอย่างชัดเจนโดยใช้พารามิเตอร์เทมเพลต
เหตุผลที่ไม่ได้ทำโดยอัตโนมัติ (เช่นการใช้ std::common_type คือมันอาจส่งผลให้เกิดดัชนีเชิงลบโดยไม่ตั้งใจไปยังประเภทที่ไม่ได้ลงนามหรือดัชนีจำนวนเต็มไปยังประเภทจำนวนเต็มแคบเกินไปซึ่งอาจนำไปสู่ช่วงลูปที่ไม่ถูกต้อง
นอกจากนี้เรายังสามารถใช้ 0 อย่างชัดเจนถึง Int ที่ไม่ได้ลงชื่อ แต่นั่นก็ดูไม่ดี:
pool.submit_loop( static_cast < unsigned int >( 0 ), max, /* ... */ );หรือเราสามารถใช้นักแสดงสไตล์ C:
pool.submit_loop(( unsigned int )( 0 ), max, /* ... */ );หรือเราสามารถใช้คำต่อท้ายตัวอักษรจำนวนเต็ม:
pool.submit_loop< size_t >( 0U , max, ...);ในฐานะที่เป็นบันทึกด้านข้างโปรดสังเกตว่าที่นี่เราขนานกับการคำนวณของสี่เหลี่ยม แต่เราไม่ได้ขนานกับการพิมพ์ผลลัพธ์ นี่คือสองเหตุผล:
เช่นเดียวกับในกรณีของ detach_task() กับ submit_task() บางครั้งคุณอาจต้องการขนานกับลูป แต่คุณไม่ต้องการให้คืน BS::multi_future ในกรณีนี้คุณสามารถบันทึกค่าใช้จ่ายของการสร้างอนาคต (ซึ่งอาจมีความสำคัญขึ้นอยู่กับจำนวนบล็อก) โดยใช้ detach_loop() แทน submit_loop() ด้วยอาร์กิวเมนต์เดียวกัน
ตัวอย่างเช่นเราสามารถแยกตัวอย่างสี่เหลี่ยมด้านบนดังนี้:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iomanip > // std::setw
# include < iostream > // std::cout
int main ()
{
BS::thread_pool pool ( 10 );
constexpr unsigned int max = 100 ;
unsigned int squares[max];
pool. detach_loop < unsigned int >( 0 , max,
[&squares]( const unsigned int i)
{
squares[i] = i * i;
});
pool. wait ();
for ( unsigned int i = 0 ; i < max; ++i)
std::cout << std::setw ( 2 ) << i << " ^2 = " << std::setw ( 4 ) << squares[i] << ((i % 5 != 4 ) ? " | " : " n " );
} Warning: Since detach_loop() does not return a BS::multi_future , there is no built-in way for the user to know when the loop finishes executing. You must use either wait() as we did here, or some other method such as condition variables, to ensure that the loop finishes executing before trying to use anything that depends on its output. Otherwise, bad things will happen!
We have seen that detach_loop() and submit_loop() execute the function loop(i) for each index i in the loop. However, behind the scenes, the loop is split into blocks, and each block executes the loop() function multiple times. Each block has an internal loop of the form (where T is the type of the indices):
for (T i = start; i < end; ++i)
loop (i); The start and end indices of each block are determined automatically by the pool. For example, in the previous section, the loop from 0 to 100 was split into 10 blocks of 10 indices each: start = 0 to end = 10 , start = 10 to end = 20 , and so on; the blocks are not inclusive of the last index, since the for loop has the condition i < end and not i <= end .
However, this also means that the loop() function is executed multiple times per block. This generates additional overhead due to the multiple function calls. For short loops, this should not affect performance. However, for very long loops, with millions of indices, the performance cost may be significate.
For this reason, the thread pool library provides two additional member functions for parallelizing loops: detach_blocks() and submit_blocks() . While detach_loop() and submit_loop() execute a function loop(i) once per index but multiple times per block, detach_blocks() and submit_blocks() execute a function block(start, end) once per block.
The main advantage of this method is increased performance, but the main disadvantage is slightly more complicated code. In particular, the user must define the loop from start to end manually within each block. Here is the previous example using detach_blocks() :
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iomanip > // std::setw
# include < iostream > // std::cout
int main ()
{
BS::thread_pool pool ( 10 );
constexpr unsigned int max = 100 ;
unsigned int squares[max];
pool. detach_blocks < unsigned int >( 0 , max,
[&squares]( const unsigned int start, const unsigned int end)
{
for ( unsigned int i = start; i < end; ++i)
squares[i] = i * i;
});
pool. wait ();
for ( unsigned int i = 0 ; i < max; ++i)
std::cout << std::setw ( 2 ) << i << " ^2 = " << std::setw ( 4 ) << squares[i] << ((i % 5 != 4 ) ? " | " : " n " );
}Note how the block function takes two arguments, and includes the internal loop.
Generally, compiler optimizations should be able to make detach_loop() and submit_loop() perform roughly the same as detach_blocks() and submit_blocks() . However, you should perform your own benchmarks to see which option works best for your particular use case.
Unlike submit_task() , the member function submit_loop() only takes loop functions with no return values. The reason is that it wouldn't make sense to return a future for every single index of the loop. However, submit_blocks() does allow the block function to have a return value, as the number of blocks will generally not be too large, unlike the number of indices.
The block function will be executed once for each block, but the blocks are managed by the thread pool, with the user only able to select the number of blocks, but not the range of each block. Therefore, there is limited usability in returning one value per block. However, for cases where this is desired, such as for summation or some sorting algorithms, submit_blocks() does accept functions with return values, in which case it returns a BS::multi_future<T> object where T is the type of the return values.
Here's an example of a function template summing all elements of type T in a given range:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < cstdint > // std::uint64_t
# include < future > // std::future
# include < iostream > // std::cout
BS::thread_pool pool;
template < typename T>
T sum (T min, T max)
{
BS::multi_future<T> loop_future = pool. submit_blocks <T>(
min, max + 1 ,
[]( const T start, const T end)
{
T block_total = 0 ;
for (T i = start; i < end; ++i)
block_total += i;
return block_total;
},
100 );
T result = 0 ;
for (std::future<T>& future : loop_future)
result += future. get ();
return result;
}
int main ()
{
std::cout << sum<std:: uint64_t >( 1 , 1'000'000 );
} Here we used the fact that BS::multi_future<T> is a specialization of std::vector<std::future<T>> , so we can use a range-based for loop to iterate over the futures, and use the get() member function of each future to get its value. The values of the futures will be the partial sums from each block, so when we add them up, we will get the total sum. Note that we divided the loop into 100 blocks, so there will be 100 futures in total, each with the partial sum of 10,000 numbers.
The range-based for loop will likely start before the loop finished executing, and each time it calls a future, it will get the value of that future if it is ready, or it will wait until the future is ready and then get the value. This increases performance, since we can start summing the results without waiting for the entire loop to finish executing first - we only need to wait for individual blocks.
If we did want to wait until the entire loop finishes before summing the results, we could have used the get() member function of the BS::multi_future<T> object itself, which returns an std::vector<T> with the values obtained from each future. In that case, the sum could be obtained after calling submit_blocks() as follows:
std::vector<T> partial_sums = loop_future.get();
T result = std::reduce(partial_sums.begin(), partial_sums.end());
return result; The member functions detach_loop() , submit_loop() , detach_blocks() , and submit_blocks() parallelize a loop by splitting it into blocks, and submitting each block as an individual task to the queue, with each such task iterating over all the indices in the corresponding block's range, which can be numerous. However, sometimes we have loops with few indices, or more generally, a sequence of tasks enumerated by some index. In such cases, we can avoid the overhead of splitting into blocks and simply submit each individual index as its own independent task to the pool's queue.
This can be done with detach_sequence() and submit_sequence() . The syntax of these functions is similar to detach_loop() and submit_loop() , except that they don't have the num_blocks argument at the end. The sequence function must take only one argument, the index. As usual, detach_sequence() detaches the tasks and does not return a future, while submit_sequence() returns a BS::multi_future . If the tasks in the sequence return values, then the futures will contain those values, otherwise they will be void futures.
Here is a simple example:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < cstdint > // std::uint64_t
# include < iostream > // std::cout
# include < vector > // std::vector
using ui64 = std:: uint64_t ;
ui64 factorial ( const ui64 n)
{
ui64 result = 1 ;
for (ui64 i = 2 ; i <= n; ++i)
result *= i;
return result;
}
int main ()
{
BS::thread_pool pool;
constexpr ui64 max = 20 ;
BS::multi_future<ui64> sequence_future = pool. submit_sequence <ui64>( 0 , max + 1 , factorial);
std::vector<ui64> factorials = sequence_future. get ();
for (ui64 i = 0 ; i < max + 1 ; ++i)
std::cout << i << " ! = " << factorials[i] << ' n ' ;
}BS::multi_future<T> The helper class template BS::multi_future<T> , which we have been using throughout this section, provides a convenient way to collect and access groups of futures. This class is a specialization of std::vector<T> , so it should be used in a similar way:
[] operator to access the future at a specific index, or the push_back() member function to append a new future to the list.size() member function tells you how many futures are currently stored in the object. However, BS::multi_future<T> also has additional member functions that are aimed specifically at handling futures:
wait() to wait for all of them at once or get() to get an std::vector<T> with the results from all of them.ready_count() .valid() .wait_for() or wait until a specific time with wait_until() . These functions return true if all futures have been waited for before the duration expired or the time point was reached, and false otherwise. Aside from using BS::multi_future<T> to track the execution of parallelized loops, it can also be used, for example, whenever you have several different groups of tasks and you want to track the execution of each group individually.
The optional header file BS_thread_pool_utils.hpp contains several useful utility classes. These are not necessary for using the thread pool itself; BS_thread_pool.hpp is the only header file required. However, the utility classes can make writing multithreading code more convenient.
As with the main header file, the version of the utilities header file can be found by checking three macros:
BS_THREAD_POOL_UTILS_VERSION_MAJOR - indicates the major version.BS_THREAD_POOL_UTILS_VERSION_MINOR - indicates the minor version.BS_THREAD_POOL_UTILS_VERSION_PATCH - indicates the patch version.BS::synced_streamWhen printing to an output stream from multiple threads in parallel, the output may become garbled. For example, consider this code:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iostream > // std::cout
BS::thread_pool pool;
int main ()
{
pool. detach_sequence ( 0 , 5 ,
[]( int i)
{
std::cout << " Task no. " << i << " executing. n " ;
});
}The output will be a mess similar to this:
Task no. Task no. Task no. 3 executing.
0 executing.
Task no. 41 executing.
Task no. 2 executing.
executing.
The reason is that, although each individual insertion to std::cout is thread-safe, there is no mechanism in place to ensure subsequent insertions from the same thread are printed contiguously.
The utility class BS::synced_stream is designed to eliminate such synchronization issues. The constructor takes one optional argument, specifying the output stream to print to. If no argument is supplied, std::cout will be used:
// Construct a synced stream that will print to std::cout.
BS::synced_stream sync_out;
// Construct a synced stream that will print to the output stream my_stream.
BS::synced_stream sync_out (my_stream); The member function print() takes an arbitrary number of arguments, which are inserted into the stream one by one, in the order they were given. println() does the same, but also prints a newline character n at the end, for convenience. A mutex is used to synchronize this process, so that any other calls to print() or println() using the same BS::synced_stream object must wait until the previous call has finished.
As an example, this code:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
BS::synced_stream sync_out;
BS::thread_pool pool;
int main ()
{
pool. detach_sequence ( 0 , 5 ,
[]( int i)
{
sync_out. println ( " Task no. " , i, " executing. " );
});
}Will print out:
Task no. 0 executing.
Task no. 1 executing.
Task no. 2 executing.
Task no. 3 executing.
Task no. 4 executing.
Warning: Always create the BS::synced_stream object before the BS::thread_pool object, as we did in this example. When the BS::thread_pool object goes out of scope, it waits for the remaining tasks to be executed. If the BS::synced_stream object goes out of scope before the BS::thread_pool object, then any tasks using the BS::synced_stream will crash. Since objects are destructed in the opposite order of construction, creating the BS::synced_stream object before the BS::thread_pool object ensures that the BS::synced_stream is always available to the tasks, even while the pool is destructing.
Most stream manipulators defined in the headers <ios> and <iomanip> , such as std::setw (set the character width of the next output), std::setprecision (set the precision of floating point numbers), and std::fixed (display floating point numbers with a fixed number of digits), can be passed to print() and println() just as you would pass them to a stream.
The only exceptions are the flushing manipulators std::endl and std::flush , which will not work because the compiler will not be able to figure out which template specializations to use. Instead, use BS::synced_stream::endl and BS::synced_stream::flush . Here is an example:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < cmath > // std::sqrt
# include < iomanip > // std::setprecision, std::setw
# include < ios > // std::fixed
BS::synced_stream sync_out;
BS::thread_pool pool;
int main ()
{
sync_out. print ( std::setprecision ( 10 ), std::fixed);
pool. detach_sequence ( 0 , 16 ,
[]( int i)
{
sync_out. print ( " The square root of " , std::setw ( 2 ), i, " is " , std::sqrt (i), " . " , BS::synced_stream::endl);
});
} Note, however, that BS::synced_stream::endl should only be used if flushing is desired; otherwise, a newline character should be used instead.
BS::timerIf you are using a thread pool, then your code is most likely performance-critical. Achieving maximum performance requires performing a considerable amount of benchmarking to determine the optimal settings and algorithms. Therefore, it is important to be able to measure the execution time of various computations and operations under different conditions.
The utility class BS::timer provides a simple way to measure execution time. It is very straightforward to use:
BS::timer object.start() member function.stop() member function.ms() to obtain the elapsed time for the computation in milliseconds.current_ms() to obtain the elapsed time so far but keep the timer ticking.ตัวอย่างเช่น:
BS::timer tmr;
tmr.start();
do_something ();
tmr.stop();
std::cout << " The elapsed time was " << tmr.ms() << " ms. n " ; A practical application of the BS::timer class can be found in the benchmark portion of the test program BS_thread_pool_test.cpp .
BS::signaller BS::signaller is a utility class which can be used to allow simple signalling between threads. To use it, construct an object and then pass it to the different threads. Multiple threads can call the wait() member function of the signaller. When another thread calls the ready() member function, the waiting threads will stop waiting.
That's really all there is to it; BS::signaller is really just a convenient wrapper around std::promise , which contains both the promise and its future. For usage examples, please see the test program BS_thread_pool_test.cpp .
Sometimes you may wish to monitor what is happening with the tasks you submitted to the pool. This may be done using three member functions:
get_tasks_queued() gets the number of tasks currently waiting in the queue to be executed by the threads.get_tasks_running() gets the number of tasks currently being executed by the threads.get_tasks_total() gets the total number of unfinished tasks: either still in the queue, or running in a thread.get_tasks_total() == get_tasks_queued() + get_tasks_running() .These functions are demonstrated in the following program:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < chrono > // std::chrono
# include < thread > // std::this_thread
BS::synced_stream sync_out;
BS::thread_pool pool ( 4 );
void sleep_half_second ( const int i)
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 500 ));
sync_out. println ( " Task " , i, " done. " );
}
void monitor_tasks ()
{
sync_out. println (pool. get_tasks_total (), " tasks total, " , pool. get_tasks_running (), " tasks running, " , pool. get_tasks_queued (), " tasks queued. " );
}
int main ()
{
pool. wait ();
pool. detach_sequence ( 0 , 12 , sleep_half_second);
monitor_tasks ();
std::this_thread::sleep_for ( std::chrono::milliseconds ( 750 ));
monitor_tasks ();
std::this_thread::sleep_for ( std::chrono::milliseconds ( 500 ));
monitor_tasks ();
std::this_thread::sleep_for ( std::chrono::milliseconds ( 500 ));
monitor_tasks ();
}Assuming you have at least 4 hardware threads (so that 4 tasks can run concurrently), the output should be similar to:
12 tasks total, 0 tasks running, 12 tasks queued.
Task 0 done.
Task 1 done.
Task 2 done.
Task 3 done.
8 tasks total, 4 tasks running, 4 tasks queued.
Task 4 done.
Task 5 done.
Task 6 done.
Task 7 done.
4 tasks total, 4 tasks running, 0 tasks queued.
Task 8 done.
Task 9 done.
Task 10 done.
Task 11 done.
0 tasks total, 0 tasks running, 0 tasks queued.
The reason we called pool.wait() in the beginning is that when the thread pool is created, an initialization task runs in each thread, so if we don't wait, the first line will say there are 16 tasks in total, including the 4 initialization tasks. ดูรายละเอียดเพิ่มเติมด้านล่าง
Consider a situation where the user cancels a multithreaded operation while it is still ongoing. Perhaps the operation was split into multiple tasks, and half of the tasks are currently being executed by the pool's threads, but the other half are still waiting in the queue.
The thread pool cannot terminate the tasks that are already running, as the C++17 standard does not provide that functionality (and in any case, abruptly terminating a task while it's running could have extremely bad consequences, such as memory leaks and data corruption). However, the tasks that are still waiting in the queue can be purged using the purge() member function.
Once purge() is called, any tasks still waiting in the queue will be discarded, and will never be executed by the threads. Please note that there is no way to restore the purged tasks; they are gone forever.
Consider for example the following program:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < chrono > // std::chrono
# include < thread > // std::this_thread
BS::synced_stream sync_out;
BS::thread_pool pool ( 4 );
int main ()
{
for ( size_t i = 0 ; i < 8 ; ++i)
{
pool. detach_task (
[i]
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 100 ));
sync_out. println ( " Task " , i, " done. " );
});
}
std::this_thread::sleep_for ( std::chrono::milliseconds ( 50 ));
pool. purge ();
pool. wait ();
} The program submit 8 tasks to the queue. Each task waits 100 milliseconds and then prints a message. The thread pool has 4 threads, so it will execute the first 4 tasks in parallel, and then the remaining 4. We wait 50 milliseconds, to ensure that the first 4 tasks have all started running. Then we call purge() to purge the remaining 4 tasks. As a result, these tasks never get executed. However, since the first 4 tasks are still running when purge() is called, they will finish uninterrupted; purge() only discards tasks that have not yet started running. The output of the program therefore only contains the messages from the first 4 tasks:
Task 0 done.
Task 1 done.
Task 2 done.
Task 3 done.
submit_task() catches any exceptions thrown by the submitted task and forwards them to the corresponding future. They can then be caught when invoking the get() member function of the future. ตัวอย่างเช่น:
# include " BS_thread_pool.hpp "
BS::synced_stream sync_out;
BS::thread_pool pool;
double inverse ( const double x)
{
if (x == 0 )
throw std::runtime_error ( " Division by zero! " );
else
return 1 / x;
}
int main ()
{
constexpr double num = 0 ;
std::future< double > my_future = pool. submit_task (inverse, num);
try
{
const double result = my_future. get ();
sync_out. println ( " The inverse of " , num, " is " , result, " . " );
}
catch ( const std:: exception & e)
{
sync_out. println ( " Caught exception: " , e. what ());
}
}The output will be:
Caught exception: Division by zero!
However, if you change num to any non-zero number, no exceptions will be thrown and the inverse will be printed.
It is important to note that wait() does not throw any exceptions; only get() does. Therefore, even if your task does not return anything, ie your future is an std::future<void> , you must still use get() on the future obtained from it if you want to catch exceptions thrown by it. Here is an example:
# include " BS_thread_pool.hpp "
BS::synced_stream sync_out;
BS::thread_pool pool;
void print_inverse ( const double x)
{
if (x == 0 )
throw std::runtime_error ( " Division by zero! " );
else
sync_out. println ( " The inverse of " , x, " is " , 1 / x, " . " );
}
int main ()
{
constexpr double num = 0 ;
std::future< void > my_future = pool. submit_task (print_inverse, num);
try
{
my_future. get ();
}
catch ( const std:: exception & e)
{
sync_out. println ( " Caught exception: " , e. what ());
}
} When using BS::multi_future to handle multiple futures at once, exception handling works the same way: if any of the futures may throw exceptions, you may catch these exceptions when calling get() , even in the case of BS::multi_future<void> .
If you do not require exception handling, or if exceptions are explicitly disabled in your codebase, you can define the macro BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING before including BS_thread_pool.hpp , which will disable exception handling in submit_task() . Note that if the feature-test macro __cpp_exceptions is undefined, BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING will be automatically defined.
BS::thread_pool comes with a variety of methods to obtain information about the threads in the pool:
BS::this_thread provides functionality similar to std::this_thread . If the current thread belongs to a BS::thread_pool object, then BS::this_thread::get_index() can be used to get the index of the current thread, and BS::this_thread::get_pool() can be used to get the pointer to the thread pool that owns the current thread. Please see the reference below for more details.get_thread_ids() returns a vector containing the unique identifiers for each of the pool's threads, as obtained by std::thread::get_id() . These values are not so useful on their own, but can be used for whatever the user wants to use them for.get_native_handles() , if enabled, returns a vector containing the underlying implementation-defined thread handles for each of the pool's threads, as obtained by std::thread::native_handle() . For more information, see the relevant section below. Sometimes, it is necessary to initialize the threads before they run any tasks. This can be done by submitting a proper initialization function to the constructor or to reset() , either as the only argument or as the second argument after the desired number of threads. The thread initialization must take no arguments and have no return value. However, if needed, the function can use BS::this_thread::get_index() and BS::this_thread::get_pool() to figure out which thread and pool it belongs to.
The thread initialization function is submitted as a set of special tasks, one per thread, which bypass the queue, but still count towards the number of running tasks, which means get_tasks_total() and get_tasks_running() will report that these tasks are running if they are checked immediately after the pool is initialized.
This is done so that the user has the option to either wait for the initialization tasks to finish, by calling wait() on the pool, or just keep going. In either case, the initialization tasks will always finish executing before any tasks are picked out of the queue, so there is no reason to wait for them to finish unless they have some side-effects that affect the main thread.
Here is a simple example:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < random > // std::mt19937_64, std::random_device
BS::synced_stream sync_out;
thread_local std::mt19937_64 twister;
int main ()
{
BS::thread_pool pool (
[]
{
twister. seed ( std::random_device ()());
});
pool. submit_sequence ( 0 , 4 ,
[]( int )
{
sync_out. println ( " I generated a random number: " , twister ());
})
. wait ();
} In this example, we create a thread_local Mersenne twister engine, meaning that each thread has its own independent engine. However, we did not seed the engine, so each thread will generate the exact same sequence of pseudo-random numbers. To remedy this, we pass an initialization function to the BS::thread_pool constructor which seeds the twister in each thread with the (hopefully) non-deterministic random number generator std::random_device .
In C++, it is often crucial to pass function arguments by reference or constant reference, instead of by value. This allows the function to access the object being passed directly, rather than creating a new copy of the object. We have already seen that submitting an argument by reference is a simple matter of capturing it with a & in the lambda capture list. To submit as constant reference, we can use std::as_const as in the following example:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < utility > // std::as_const
BS::synced_stream sync_out;
void increment ( int & x)
{
++x;
}
void print ( const int & x)
{
sync_out. println (x);
}
int main ()
{
BS::thread_pool pool;
int n = 0 ;
pool. submit_task (
[&n]
{
increment (n);
})
. wait ();
pool. submit_task (
[&n = std::as_const (n)]
{
print (n);
})
. wait ();
} The increment() function takes a reference to an integer, and increments that integer. Passing the argument by reference guarantees that n itself, in the scope of main() , will be incremented - rather than a copy of it in the scope of increment() .
Similarly, the print() function takes a constant reference to an integer, and prints that integer. Passing the argument by constant reference guarantees that the variable will not be accidentally modified by the function, even though we are accessing n itself, rather than a copy. If we replace print with increment , the program won't compile, as increment cannot take constant references.
Generally, it is not really necessary to pass arguments by constant reference, but it is more "correct" to do so, if we would like to guarantee that the variable being referenced is indeed never modified. This section is therefore included here for completeness.
Sometimes you may wish to temporarily pause the execution of tasks, or perhaps you want to submit tasks to the queue in advance and only start executing them at a later time. You can do this using the member functions pause() , unpause() , and is_paused() .
However, these functions are disabled by default, and must be explicitly enabled by defining the macro BS_THREAD_POOL_ENABLE_PAUSE before including BS_thread_pool.hpp . The reason is that pausing the pool adds additional checks to the waiting and worker functions, which have a very small but non-zero overhead.
When you call pause() , the workers will temporarily stop retrieving new tasks out of the queue. However, any tasks already executed will keep running until they are done, since the thread pool has no control over the internal code of your tasks. If you need to pause a task in the middle of its execution, you must do that manually by programming your own pause mechanism into the task itself. To resume retrieving tasks, call unpause() . To check whether the pool is currently paused, call is_paused() .
Here is an example:
# define BS_THREAD_POOL_ENABLE_PAUSE
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < chrono > // std::chrono
# include < thread > // std::this_thread
BS::synced_stream sync_out;
BS::thread_pool pool ( 4 );
void sleep_half_second ( const int i)
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 500 ));
sync_out. println ( " Task " , i, " done. " );
}
void check_if_paused ()
{
if (pool. is_paused ())
sync_out. println ( " Pool paused. " );
else
sync_out. println ( " Pool unpaused. " );
}
int main ()
{
pool. detach_sequence ( 0 , 8 , sleep_half_second);
sync_out. println ( " Submitted 8 tasks. " );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 250 ));
pool. pause ();
check_if_paused ();
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
sync_out. println ( " Still paused... " );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
pool. detach_sequence ( 8 , 12 , sleep_half_second);
sync_out. println ( " Submitted 4 more tasks. " );
sync_out. println ( " Still paused... " );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
pool. unpause ();
check_if_paused ();
}Assuming you have at least 4 hardware threads, the output should be similar to:
Submitted 8 tasks.
Pool paused.
Task 0 done.
Task 1 done.
Task 2 done.
Task 3 done.
Still paused...
Submitted 4 more tasks.
Still paused...
Pool unpaused.
Task 4 done.
Task 5 done.
Task 6 done.
Task 7 done.
Task 8 done.
Task 9 done.
Task 10 done.
Task 11 done.
Here is what happened. We initially submitted a total of 8 tasks to the queue. Since we waited for 250ms before pausing, the first 4 tasks have already started running, so they kept running until they finished. While the pool was paused, we submitted 4 more tasks to the queue, but they just waited at the end of the queue. When we unpaused, the remaining 4 initial tasks were executed, followed by the 4 new tasks.
While the workers are paused, wait() will wait for the running tasks instead of all tasks (otherwise it would wait forever). This is demonstrated by the following program:
# define BS_THREAD_POOL_ENABLE_PAUSE
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < chrono > // std::chrono
# include < thread > // std::this_thread
BS::synced_stream sync_out;
BS::thread_pool pool ( 4 );
void sleep_half_second ( const int i)
{
std::this_thread::sleep_for ( std::chrono::milliseconds ( 500 ));
sync_out. println ( " Task " , i, " done. " );
}
void check_if_paused ()
{
if (pool. is_paused ())
sync_out. println ( " Pool paused. " );
else
sync_out. println ( " Pool unpaused. " );
}
int main ()
{
pool. detach_sequence ( 0 , 8 , sleep_half_second);
sync_out. println ( " Submitted 8 tasks. Waiting for them to complete. " );
pool. wait ();
pool. detach_sequence ( 8 , 20 , sleep_half_second);
sync_out. println ( " Submitted 12 more tasks. " );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 250 ));
pool. pause ();
check_if_paused ();
sync_out. println ( " Waiting for the " , pool. get_tasks_running (), " running tasks to complete. " );
pool. wait ();
sync_out. println ( " All running tasks completed. " , pool. get_tasks_queued (), " tasks still queued. " );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
sync_out. println ( " Still paused... " );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
sync_out. println ( " Still paused... " );
std::this_thread::sleep_for ( std::chrono::milliseconds ( 1000 ));
pool. unpause ();
check_if_paused ();
std::this_thread::sleep_for ( std::chrono::milliseconds ( 250 ));
sync_out. println ( " Waiting for the remaining " , pool. get_tasks_total (), " tasks ( " , pool. get_tasks_running (), " running and " , pool. get_tasks_queued (), " queued) to complete. " );
pool. wait ();
sync_out. println ( " All tasks completed. " );
}The output should be similar to:
Submitted 8 tasks. Waiting for them to complete.
Task 0 done.
Task 1 done.
Task 2 done.
Task 3 done.
Task 4 done.
Task 5 done.
Task 6 done.
Task 7 done.
Submitted 12 more tasks.
Pool paused.
Waiting for the 4 running tasks to complete.
Task 8 done.
Task 9 done.
Task 10 done.
Task 11 done.
All running tasks completed. 8 tasks still queued.
Still paused...
Still paused...
Pool unpaused.
Waiting for the remaining 8 tasks (4 running and 4 queued) to complete.
Task 12 done.
Task 13 done.
Task 14 done.
Task 15 done.
Task 16 done.
Task 17 done.
Task 18 done.
Task 19 done.
All tasks completed.
The first wait() , which was called while the pool was not paused, waited for all 8 tasks, both running and queued. The second wait() , which was called after pausing the pool, only waited for the 4 running tasks, while the other 8 tasks remained queued, and were not executed since the pool was paused. Finally, the third wait() , which was called after unpausing the pool, waited for the remaining 8 tasks, both running and queued.
Warning: If the thread pool is destroyed while paused, any tasks still in the queue will never be executed!
Consider the following program:
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iostream > // std::cout
int main ()
{
BS::thread_pool pool;
pool. detach_task (
[&pool]
{
pool. wait ();
std::cout << " Done waiting. n " ;
});
}This program creates a thread pool, and then detaches a task that waits for tasks in the same thread pool to complete. If you run this program, it will never print the message "Done waiting", because the task will wait for itself to complete. This causes a deadlock , and the program will wait forever.
Usually, in simple programs, this will never happen. However, in more complicated programs, perhaps ones running multiple thread pools in parallel, wait deadlocks could potentially occur. In such cases, the macro BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK can be defined before including BS_thread_pool.hpp . wait() will then check whether the user tried to call it from within a thread of the same pool, and if so, it will throw the exception BS::thread_pool::wait_deadlock instead of waiting. This check is disabled by default because wait deadlocks are not something that happens often, and the check adds a small but non-zero overhead every time wait() is called.
Here is an example:
# define BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK
# include " BS_thread_pool.hpp " // BS::thread_pool
# include < iostream > // std::cout
int main ()
{
BS::thread_pool pool;
pool. detach_task (
[&pool]
{
try
{
pool. wait ();
std::cout << " Done waiting. n " ;
}
catch ( const BS::thread_pool::wait_deadlock&)
{
std::cout << " Error: Deadlock! n " ;
}
});
} This time, wait() will detect the deadlock, and will throw an exception, causing the output to be "Error: Deadlock!" -
Note that if the feature-test macro __cpp_exceptions is undefined, BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK will be automatically undefined.
The BS::thread_pool member function get_native_handles() returns a vector containing the underlying implementation-defined thread handles for each of the pool's threads. These can then be used in an implementation-specific way to manage the threads at the OS level
However, note that this will generally not be portable code. Furthermore, this feature uses std::thread::native_handle(), which is in the C++ standard library, but is not guaranteed to be present on all systems. Therefore, this feature is turned off by default, and must be turned on by defining the macro BS_THREAD_POOL_ENABLE_NATIVE_HANDLES before including BS_thread_pool.hpp .
Here is an example:
# define BS_THREAD_POOL_ENABLE_NATIVE_HANDLES
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
# include < thread > // std::thread
# include < vector > // std::vector
BS::synced_stream sync_out;
BS::thread_pool pool ( 4 );
int main ()
{
std::vector<std::thread::native_handle_type> handles = pool. get_native_handles ();
for (BS:: concurrency_t i = 0 ; i < handles. size (); ++i)
sync_out. println ( " Thread " , i, " native handle: " , handles[i]);
}The output will depend on your compiler and operating system. Here is an example:
Thread 0 native handle: 000000F4
Thread 1 native handle: 000000F8
Thread 2 native handle: 000000EC
Thread 3 native handle: 000000FC
Defining the macro BS_THREAD_POOL_ENABLE_PRIORITY before including BS_thread_pool.hpp enables task priority. The priority of a task or group of tasks may then be specified as an additional argument (at the end of the argument list) to detach_task() , submit_task() , detach_blocks() , submit_blocks() , detach_loop() , submit_loop() , detach_sequence() , and submit_sequence() . If the priority is not specified, the default value will be 0.
The priority is a number of type BS::priority_t , which is a signed 16-bit integer, so it can have any value between -32,768 and 32,767. The tasks will be executed in priority order from highest to lowest. If priority is assigned to the block/loop/sequence parallelization functions, which submit multiple tasks, then all of these tasks will have the same priority.
The namespace BS::pr contains some pre-defined priorities for users who wish to avoid magic numbers and enjoy better future-proofing. In order of decreasing priority, the pre-defined priorities are: BS::pr::highest , BS::pr::high , BS::pr::normal , BS::pr::low , and BS::pr::lowest .
Here is a simple example:
# define BS_THREAD_POOL_ENABLE_PRIORITY
# include " BS_thread_pool.hpp " // BS::thread_pool
# include " BS_thread_pool_utils.hpp " // BS::synced_stream
BS::synced_stream sync_out;
BS::thread_pool pool ( 1 );
int main ()
{
pool. detach_task ([] { sync_out. println ( " This task will execute third. " ); }, BS::pr:: normal );
pool. detach_task ([] { sync_out. println ( " This task will execute fifth. " ); }, BS::pr::lowest);
pool. detach_task ([] { sync_out. println ( " This task will execute second. " ); }, BS::pr::high);
pool. detach_task ([] { sync_out. println ( " This task will execute first. " ); }, BS::pr::highest);
pool. detach_task ([] { sync_out. println ( " This task will execute fourth. " ); }, BS::pr::low);
}This program will print out the tasks in the correct priority order. Note that for simplicity, we used a pool with just one thread, so the tasks will run one at a time. In a pool with 5 or more threads, all 5 tasks will actually run more or less at the same time, because, for example, the task with the second-highest priority will be picked up by another thread while the task with the highest priority is still running.
Of course, this is just a pedagogical example. In a realistic use case we may want, for example, to submit tasks that must be completed immediately with high priority so they skip over other tasks already in the queue, or background non-urgent tasks with low priority so they evaluate only after higher-priority tasks are done.
Here are some subtleties to note when using task priority:
std::priority_queue , which has O(log n) complexity for storing new tasks, but only O(1) complexity for retrieving the next (ie highest-priority) task. This is in contrast with std::queue , used if priority is disabled, which both stores and retrieves with O(1) complexity.std::priority_queue as a binary heap, which means tasks are stored as a binary tree instead of sequentially. To execute tasks in submission order, give them monotonically decreasing priorities.BS::priority_t is defined to be ( std::int_least16_t ), since this type is guaranteed to be present on all systems, rather than std::int16_t , which is optional in the C++ standard. This means that on some exotic systems BS::priority_t may actually have more than 16 bits. However, the pre-defined priorities are 100% portable, and will always have the same values (eg: BS::pr::highest = 32767 ) regardless of the actual bit width. The file BS_thread_pool_test.cpp in the tests folder of the GitHub repository will perform automated tests of all aspects of the library. The output will be printed both to std::cout and to a file with the same name as the executable and the suffix -yyyy-mm-dd_hh.mm.ss.log based on the current date and time. In addition, the code is meant to serve as an extensive example of how to properly use the library.
Please make sure to:
BS_thread_pool_test.cpp with optimization flags enabled (eg -O3 on GCC / Clang or /O2 on MSVC).The test program also takes command line arguments for automation purposes:
help : Show a help message and exit. Any other arguments will be ignored.log : Create a log file.tests : Perform standard tests.deadlock Perform long deadlock tests.benchmarks : Perform benchmarks. If no options are entered, the default is: log tests benchmarks .
By default, the test program enables all the optional features by defining the suitable macros, so it can test them. However, if the macro BS_THREAD_POOL_LIGHT_TEST is defined during compilation, the optional features will not be tested.
A PowerShell script, BS_thread_pool_test.ps1 , is provided for your convenience in the tests folder to make running the test on multiple compilers and operating systems easier. Since it is written in PowerShell, it is fully portable and works on Windows, Linux, and macOS. The script will automatically detect if Clang, GCC, and/or MSVC are available, and compile the test program using each available compiler twice - with and without all the optional features. It will then run each compiled test program and report on any errors.
If any of the tests fail, please submit a bug report including the exact specifications of your system (OS, CPU, compiler, etc.) and the generated log file.
If all checks passed, BS_thread_pool_test.cpp performs simple benchmarks by filling a very large vector with values using detach_blocks() . The program decides what the size of the vector should be by testing how many elements are needed to reach a certain target duration when parallelizing using a number of blocks equal to the number of threads. This ensures that the test takes approximately the same amount of time on all systems, and is thus more consistent and portable.
Once the appropriate size of the vector has been determined, the program allocates the vector and fills it with values, calculated according to a fixed prescription. This operation is performed both single-threaded and multithreaded, with the multithreaded computation spread across multiple tasks submitted to the pool.
Several different multithreaded tests are performed, with the number of tasks either equal to, smaller than, or larger than the pool's thread count. Each test is repeated multiple times, with the run times averaged over all runs of the same test. The program keeps increasing the number of blocks by a factor of 2 until diminishing returns are encountered. The run times of the tests are compared, and the maximum speedup obtained is calculated.
As an example, here are the results of the benchmarks from a Digital Research Alliance of Canada node equipped with two 20-core / 40-thread Intel Xeon Gold 6148 CPUs (for a total of 40 cores and 80 threads), running CentOS Linux 7.9.2009. The tests were compiled using GCC v13.2.0 with the -O3 and -march=native flags. The output was as follows:
======================
Performing benchmarks:
======================
Using 80 threads.
Determining the number of elements to generate in order to achieve an approximate mean execution time of 50 ms with 80 tasks...
Each test will be repeated up to 30 times to collect reliable statistics.
Generating 27962000 elements:
[......]
Single-threaded, mean execution time was 2815.2 ms with standard deviation 3.5 ms.
[......]
With 2 tasks, mean execution time was 1431.3 ms with standard deviation 10.1 ms.
[.......]
With 4 tasks, mean execution time was 722.1 ms with standard deviation 11.4 ms.
[..............]
With 8 tasks, mean execution time was 364.9 ms with standard deviation 10.9 ms.
[............................]
With 16 tasks, mean execution time was 181.9 ms with standard deviation 8.0 ms.
[..............................]
With 32 tasks, mean execution time was 110.6 ms with standard deviation 1.8 ms.
[..............................]
With 64 tasks, mean execution time was 64.0 ms with standard deviation 6.3 ms.
[..............................]
With 128 tasks, mean execution time was 59.8 ms with standard deviation 0.8 ms.
[..............................]
With 256 tasks, mean execution time was 59.0 ms with standard deviation 0.0 ms.
[..............................]
With 512 tasks, mean execution time was 52.8 ms with standard deviation 0.4 ms.
[..............................]
With 1024 tasks, mean execution time was 50.7 ms with standard deviation 0.9 ms.
[..............................]
With 2048 tasks, mean execution time was 50.0 ms with standard deviation 0.5 ms.
[..............................]
With 4096 tasks, mean execution time was 49.4 ms with standard deviation 0.5 ms.
[..............................]
With 8192 tasks, mean execution time was 50.2 ms with standard deviation 0.4 ms.
Maximum speedup obtained by multithreading vs. single-threading: 56.9x, using 4096 tasks.
+++++++++++++++++++++++++++++++++++++++
Thread pool performance test completed!
+++++++++++++++++++++++++++++++++++++++
These two CPUs have 40 physical cores in total, with each core providing two separate logical cores via hyperthreading, for a total of 80 threads. Without hyperthreading, we would expect a maximum theoretical speedup of 40x. With hyperthreading, one might naively expect to achieve up to an 80x speedup, but this is in fact impossible, as each pair of hyperthreaded logical cores share the same physical core's resources. However, generally we would expect at most an estimated 30% additional speedup from hyperthreading, which amounts to around 52x in this case. The speedup of 56.9x in our performance test exceeds this estimate.
If you are using the vcpkg C/C++ package manager, you can easily install BS::thread_pool with the following commands:
On Linux/macOS:
./vcpkg install bshoshany-thread-pool
On Windows:
.vcpkg install bshoshany-thread-pool:x86-windows bshoshany-thread-pool:x64-windows
To update the package to the latest version, run:
vcpkg upgrade
If you are using the Conan C/C++ package manager, you can easily integrate BS::thread_pool into your project by adding the following lines to your conanfile.txt :
[requires]
bshoshany-thread-pool/4.1.0To update the package to the latest version, simply change the version number. Please refer to this package's page on ConanCenter for more information.
If you are using the Meson build system, you can install BS::thread_pool from WrapDB. To do so, create a subprojects folder in your project (if it does not already exist) and run the following command:
meson wrap install bshoshany-thread-pool
Then, use dependency('bshoshany-thread-pool') in your meson.build file to include the package. To update the package to the latest version, run:
meson wrap update bshoshany-thread-pool
If you are using CMake, you can install BS::thread_pool with CPM. If CPM is already installed, simply add the following to your project's CMakeLists.txt :
CPMAddPackage(
NAME BS_thread_pool
GITHUB_REPOSITORY bshoshany/thread-pool
VERSION 4.1.0)
add_library (BS_thread_pool INTERFACE )
target_include_directories (BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR} / include )This will automatically download the indicated version of the package from the GitHub repository and include it in your project.
It is also possible to use CPM without installing it first, by adding the following lines to CMakeLists.txt before CPMAddPackage :
set (CPM_DOWNLOAD_LOCATION " ${CMAKE_BINARY_DIR} /cmake/CPM.cmake" )
if ( NOT ( EXISTS ${CPM_DOWNLOAD_LOCATION} ))
message ( STATUS "Downloading CPM.cmake" )
file (DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake ${CPM_DOWNLOAD_LOCATION} )
endif ()
include ( ${CPM_DOWNLOAD_LOCATION} ) Here is an example of a complete CMakeLists.txt for a project named my_project consisting of a single source file main.cpp which uses BS_thread_pool.hpp :
cmake_minimum_required ( VERSION 3.19)
project (my_project LANGUAGES CXX)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON )
set (CMAKE_CXX_EXTENSIONS OFF )
set (CPM_DOWNLOAD_LOCATION " ${CMAKE_BINARY_DIR} /cmake/CPM.cmake" )
if ( NOT ( EXISTS ${CPM_DOWNLOAD_LOCATION} ))
message ( STATUS "Downloading CPM.cmake" )
file (DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/CPM.cmake ${CPM_DOWNLOAD_LOCATION} )
endif ()
include ( ${CPM_DOWNLOAD_LOCATION} )
CPMAddPackage(
NAME BS_thread_pool
GITHUB_REPOSITORY bshoshany/thread-pool
VERSION 4.1.0)
add_library (BS_thread_pool INTERFACE )
target_include_directories (BS_thread_pool INTERFACE ${BS_thread_pool_SOURCE_DIR} / include )
add_executable (my_project main.cpp)
target_link_libraries (my_project BS_thread_pool) With both CMakeLists.txt and main.cpp in the same folder, type the following commands to build the project:
cmake -S . -B build
cmake --build build
This section provides a complete reference to classes, member functions, objects, and macros available in this library, along with other important information. Member functions are given here with simplified prototypes (eg removing const ) for ease of reading.
More information can be found in the provided Doxygen comments. Any modern IDE, such as Visual Studio Code, can use the Doxygen comments to provide automatic documentation for any class and member function in this library when hovering over code with the mouse or using auto-complete.
BS_thread_pool.hpp ) BS::thread_pool class The class BS::thread_pool is the main thread pool class. It can be used to create a pool of threads and submit tasks to a queue. When a thread becomes available, it takes a task from the queue and executes it. The member functions that are available by default, when no macros are defined, are:
thread_pool() : Construct a new thread pool. The number of threads will be the total number of hardware threads available, as reported by the implementation. This is usually determined by the number of cores in the CPU. If a core is hyperthreaded, it will count as two threads.thread_pool(BS::concurrency_t num_threads) : Construct a new thread pool with the specified number of threads.thread_pool(std::function<void()>& init_task) : Construct a new thread pool with the specified initialization function.thread_pool(BS::concurrency_t num_threads, std::function<void()>& init_task) : Construct a new thread pool with the specified number of threads and initialization function.void reset() : Reset the pool with the total number of hardware threads available, as reported by the implementation. Waits for all currently running tasks to be completed, then destroys all threads in the pool and creates a new thread pool with the new number of threads. Any tasks that were waiting in the queue before the pool was reset will then be executed by the new threads. If the pool was paused before resetting it, the new pool will be paused as well.void reset(BS::concurrency_t num_threads) : Reset the pool with a new number of threads.void reset(std::function<void()>& init_task) Reset the pool with the total number of hardware threads available, as reported by the implementation, and a new initialization function.void reset(BS::concurrency_t num_threads, std::function<void()>& init_task) : Reset the pool with a new number of threads and a new initialization function.size_t get_tasks_queued() : Get the number of tasks currently waiting in the queue to be executed by the threads.size_t get_tasks_running() : Get the number of tasks currently being executed by the threads.size_t get_tasks_total() : Get the total number of unfinished tasks: either still waiting in the queue, or running in a thread. Note that get_tasks_total() == get_tasks_queued() + get_tasks_running() .BS::concurrency_t get_thread_count() : Get the number of threads in the pool.std::vector<std::thread::id> get_thread_ids() : Get a vector containing the unique identifiers for each of the pool's threads, as obtained by std::thread::get_id() .T and F are template parameters):void detach_task(F&& task) : Submit a function with no arguments and no return value into the task queue. To push a function with arguments, enclose it in a lambda expression. Does not return a future, so the user must use wait() or some other method to ensure that the task finishes executing, otherwise bad things will happen.void detach_blocks(T first_index, T index_after_last, F&& block, size_t num_blocks = 0) : Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The block function takes two arguments, the start and end of the block, so that it is only called only once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Does not return a BS::multi_future , so the user must use wait() or some other method to ensure that the loop finishes executing, otherwise bad things will happen.void detach_loop(T first_index, T index_after_last, F&& loop, size_t num_blocks = 0) : Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The loop function takes one argument, the loop index, so that it is called many times per block. Does not return a BS::multi_future , so the user must use wait() or some other method to ensure that the loop finishes executing, otherwise bad things will happen.void detach_sequence(T first_index, T index_after_last, F&& sequence) : Submit a sequence of tasks enumerated by indices to the queue. Does not return a BS::multi_future , so the user must use wait() or some other method to ensure that the sequence finishes executing, otherwise bad things will happen.T , F , and R are template parameters):std::future<R> submit_task(F&& task) : Submit a function with no arguments into the task queue. To submit a function with arguments, enclose it in a lambda expression. If the function has a return value, get a future for the eventual returned value. If the function has no return value, get an std::future<void> which can be used to wait until the task finishes.BS::multi_future<R> submit_blocks(T first_index, T index_after_last, F&& block, size_t num_blocks = 0) : Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The block function takes two arguments, the start and end of the block, so that it is only called only once per block, but it is up to the user make sure the block function correctly deals with all the indices in each block. Returns a BS::multi_future that contains the futures for all of the blocks.BS::multi_future<void> submit_loop(T first_index, T index_after_last, F&& loop, size_t num_blocks = 0) : Parallelize a loop by automatically splitting it into blocks and submitting each block separately to the queue. The loop function takes one argument, the loop index, so that it is called many times per block. It must have no return value. Returns a BS::multi_future that contains the futures for all of the blocks.BS::multi_future<R> submit_sequence(T first_index, T index_after_last, F&& sequence) : Submit a sequence of tasks enumerated by indices to the queue. Returns a BS::multi_future that contains the futures for all of the tasks.void purge() : Purge all the tasks waiting in the queue. Tasks that are currently running will not be affected, but any tasks still waiting in the queue will be discarded, and will never be executed by the threads. Please note that there is no way to restore the purged tasks.R and P , C , and D are template parameters):void wait() : Wait for tasks to be completed. Normally, this function waits for all tasks, both those that are currently running in the threads and those that are still waiting in the queue. However, if the pool is paused, this function only waits for the currently running tasks (otherwise it would wait forever). Note: To wait for just one specific task, use submit_task() instead, and call the wait() member function of the generated future.bool wait_for(std::chrono::duration<R, P>& duration) : Wait for tasks to be completed, but stop waiting after the specified duration has passed. Returns true if all tasks finished running, false if the duration expired but some tasks are still running.bool wait_until(std::chrono::time_point<C, D>& timeout_time) : Wait for tasks to be completed, but stop waiting after the specified time point has been reached. Returns true if all tasks finished running, false if the time point was reached but some tasks are still running. When a BS::thread_pool object goes out of scope, the destructor first waits for all tasks to complete, then destroys all threads. Note that if the pool is paused, then any tasks still in the queue will never be executed.
BS::thread_pool classThe thread pool has several optional features that must be explicitly enabled using macros.
BS_THREAD_POOL_ENABLE_PRIORITY .detach_task() , submit_task() , detach_blocks() , submit_blocks() , detach_loop() , submit_loop() , detach_sequence() , and submit_sequence() . If the priority is not specified, the default value will be 0.BS::priority_t , which is a signed 16-bit integer, so it can have any value between -32,768 and 32,767. The tasks will be executed in priority order from highest to lowest.BS::pr contains some pre-defined priorities: BS::pr::highest , BS::pr::high , BS::pr::normal , BS::pr::low , and BS::pr::lowest .BS_THREAD_POOL_ENABLE_PAUSE . Adds the following member functions:void pause() : Pause the pool. The workers will temporarily stop retrieving new tasks out of the queue, although any tasks already executed will keep running until they are finished.void unpause() : Unpause the pool. The workers will resume retrieving new tasks out of the queue.bool is_paused() : Check whether the pool is currently paused.BS_THREAD_POOL_ENABLE_NATIVE_HANDLES . Adds the following member function:std::vector<std::thread::native_handle_type> get_native_handles() : Get a vector containing the underlying implementation-defined thread handles for each of the pool's threads.BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK .wait() , wait_for() , and wait_until() will check whether the user tried to call them from within a thread of the same pool, which would result in a deadlock. If so, they will throw the exception BS::thread_pool::wait_deadlock instead of waiting.BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING .submit_task() if it is not needed, or if exceptions are explicitly disabled in the codebase.BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK . Disabling exception handling removes the try - catch block from submit_task() , while enabling wait deadlock checks adds a throw expression to wait() , wait_for() , and wait_until() .__cpp_exceptions is undefined, BS_THREAD_POOL_DISABLE_EXCEPTION_HANDLING is automatically defined, and BS_THREAD_POOL_ENABLE_WAIT_DEADLOCK_CHECK is automatically undefined. BS::this_thread namespace The namespace BS::this_thread provides functionality similar to std::this_thread . It contains the following function objects:
BS::this_thread::get_index() can be used to get the index of the current thread. If this thread belongs to a BS::thread_pool object, it will have an index from 0 to BS::thread_pool::get_thread_count() - 1 . Otherwise, for example if this thread is the main thread or an independent std::thread , std::nullopt will be returned.BS::this_thread::get_pool() can be used to get the pointer to the thread pool that owns the current thread. If this thread belongs to a BS::thread_pool object, a pointer to that object will be returned. Otherwise, std::nullopt will be returned.std::optional object will be returned, of type BS::this_thread::optional_index or BS::this_thread::optional_pool respectively. Unless you are 100% sure this thread is in a pool, first use std::optional::has_value() to check if it contains a value, and if so, use std::optional::value() to obtain that value. BS::multi_future<T> class BS::multi_future<T> is a helper class used to facilitate waiting for and/or getting the results of multiple futures at once. It is defined as a specialization of std::vector<std::future<T>> . This means that all of the member functions that can be used on an std::vector can also be used on a BS::multi_future . For example, you may use a range-based for loop with a BS::multi_future , since it has iterators.
In addition to inherited member functions, BS::multi_future has the following specialized member functions ( R and P , C , and D are template parameters):
[void or std::vector<T>] get() : Get the results from all the futures stored in this BS::multi_future , rethrowing any stored exceptions. If the futures return void , this function returns void as well. If the futures return a type T , this function returns a vector containing the results.size_t ready_count() : Check how many of the futures stored in this BS::multi_future are ready.bool valid() : Check if all the futures stored in this BS::multi_future are valid.void wait() : Wait for all the futures stored in this BS::multi_future .bool wait_for(std::chrono::duration<R, P>& duration) : Wait for all the futures stored in this BS::multi_future , but stop waiting after the specified duration has passed. Returns true if all futures have been waited for before the duration expired, false otherwise.bool wait_until(std::chrono::time_point<C, D>& timeout_time) : Wait for all the futures stored in this multi_future object, but stop waiting after the specified time point has been reached. Returns true if all futures have been waited for before the time point was reached, false otherwise.BS_thread_pool_utils.hpp ) BS::signaller class BS::signaller is a utility class which can be used to allow simple signalling between threads. This class is really just a convenient wrapper around std::promise , which contains both the promise and its future. It has the following member functions:
signaller() : Construct a new signaller.void wait() : Wait until the signaller is ready.void ready() : Inform any waiting threads that the signaller is ready. BS::synced_stream class BS::synced_stream is a utility class which can be used to synchronize printing to an output stream by different threads. It has the following member functions ( T is a template parameter pack):
synced_stream(std::ostream& stream = std::cout) : Construct a new synced stream which prints to the given output stream.void print(T&&... items) : Print any number of items into the output stream. Ensures that no other threads print to this stream simultaneously, as long as they all exclusively use the same synced_stream object to print.void println(T&&... items) : Print any number of items into the output stream, followed by a newline character.In addition, the class comes with two stream manipulators, which are meant to help the compiler figure out which template specializations to use with the class:
BS::synced_stream::endl : An explicit cast of std::endl . Prints a newline character to the stream, and then flushes it. Should only be used if flushing is desired, otherwise a newline character should be used instead.BS::synced_stream::flush : An explicit cast of std::flush . Used to flush the stream. BS::timer class BS::timer is a utility class which can be used to measure execution time for benchmarking purposes. It has the following member functions:
timer() : Construct a new timer and immediately start measuring time.void start() : Start (or restart) measuring time. Note that the timer starts ticking as soon as the object is created, so this is only necessary if we want to restart the clock later.void stop() : Stop measuring time and store the elapsed time since the object was constructed or since start() was last called.std::chrono::milliseconds::rep current_ms() : Get the number of milliseconds that have elapsed since the object was constructed or since start() was last called, but keep the timer ticking.std::chrono::milliseconds::rep ms() : Get the number of milliseconds stored when stop() was last called. This library is under continuous and active development. If you encounter any bugs, or if you would like to request any additional features, please feel free to open a new issue on GitHub and I will look into it as soon as I can.
Contributions are always welcome. However, I release my projects in cumulative updates after editing and testing them locally on my system, so my policy is not to accept any pull requests. If you open a pull request, and I decide to incorporate your suggestion into the project, I will first modify your code to comply with the project's coding conventions (formatting, syntax, naming, comments, programming practices, etc.), and perform some tests to ensure that the change doesn't break anything. I will then merge it into the next release of the project, possibly together with some other changes. The new release will also include a note in CHANGELOG.md with a link to your pull request, and modifications to the documentation in README.md as needed.
Many GitHub users have helped improve this project, directly or indirectly, via issues, pull requests, comments, and/or personal correspondence. Please see CHANGELOG.md for links to specific issues and pull requests that have been the most helpful. Thank you all for your contribution! -
If you found this project useful, please consider starring it on GitHub! This allows me to see how many people are using my code, and motivates me to keep working to improve it.
Copyright (c) 2024 Barak Shoshany. Licensed under the MIT license.
If you use this C++ thread pool library in software of any kind, please provide a link to the GitHub repository in the source code and documentation.
If you use this library in published research, please cite it as follows:
You can use the following BibTeX entry:
@article { Shoshany2024_ThreadPool ,
archiveprefix = { arXiv } ,
author = { Barak Shoshany } ,
doi = { 10.1016/j.softx.2024.101687 } ,
eprint = { 2105.00613 } ,
journal = { SoftwareX } ,
pages = { 101687 } ,
title = { {A C++17 Thread Pool for High-Performance Scientific Computing} } ,
url = { https://www.sciencedirect.com/science/article/pii/S235271102400058X } ,
volume = { 26 } ,
year = { 2024 }
} Please note that the papers on SoftwareX and arXiv are not up to date with the latest version of the library. These publications are only intended to facilitate discovery of this library by scientists, and to enable citing it in scientific research. Documentation for the latest version is provided only by the README.md file in the GitHub repository.
Beginner C++ programmers may be interested in my lecture notes for a course taught at McMaster University, which teach modern C and C++ from scratch, including some of the advanced techniques and programming practices used in developing this library.