Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Dispatch Queue / Thread Pool implementation for C++11
License
NotificationsYou must be signed in to change notification settings
gilzoide/cpp-dispatch-queue
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Dispatch Queue / Thread Pool implementation for C++11.
- No external dependencies: uses only C++11 threading functionality
dispatchmethod with API similar tostd::async: receives a functor and its arguments, returns astd::futuredispatch_forgetfor a "fire and forget" style call, which avoids the overhead of creating thestd::future- Supports both synchronous (immediate) and asynchronous (threaded) execution.
- Asynchronous dispatch queues are also known as Thread Pools
- Synchronous dispatch queues are useful for multiplatform code that must work on platforms without thread support, for example WebAssembly on browsers that lack
SharedArrayBuffersupport
- Single implementation filesrc/dispatch_queue.cpp, easy to integrate in any project
#include<dispatch_queue.hpp>///////////////////////////////////////////////////////////// 1. Create a dispatch queue///////////////////////////////////////////////////////////// Default constructed dispatch queues are synchronous.// They execute tasks immediately in the same thread that called `dispatch`.dispatch_queue::dispatch_queue synchronous_dispatcher;// Dispatch queues with 0 threads are also synchronous.dispatch_queue::dispatch_queuesynchronous_dispatcher2(0);// A dispatch queue with 1 thread is called a serial queue:// it only runs a single task at a time.dispatch_queue::dispatch_queueserial_dispatcher(1);// A dispatch queue with more than 1 thread runs tasks concurrently.dispatch_queue::dispatch_queueconcurrent_dispatcher(4);// Pass a negative value to use the default thread count.// Current default is `std::thread::hardware_concurrency`.dispatch_queue::dispatch_queueconcurrent_dispatcher2(-1);///////////////////////////////////////////////////////////// 2. Dispatch some tasks!///////////////////////////////////////////////////////////// Use the returned future to get results or wait for completion.auto task = []() {return42; };std::future<int> async_result = dispatcher.dispatch(task);assert(async_result.get() ==42);// Pass arguments to forward to taskauto task2 = [](int value) {return value; };std::future<int> async_result2 = dispatcher.dispatch(task2,2);assert(async_result2.get() ==2);// For "fire and forget" calls, use `dispatch_forget`.// This avoids the overhead of creating `std::future`.dispatcher.dispatch_forget(task);dispatcher.dispatch_forget(task2,2);///////////////////////////////////////////////////////////// 3. Check some stats///////////////////////////////////////////////////////////int dispatcher_thread_count = dispatcher.thread_count();bool dispatcher_is_threaded = dispatcher.is_threaded();int pending_task_count = dispatcher.size();bool has_no_pending_tasks = dispatcher.empty();///////////////////////////////////////////////////////////// 4. Other operations///////////////////////////////////////////////////////////// Cancel all pending tasks.// Tasks already executing will still run to completion.dispatcher.clear();// Wait until pending tasks are completeddispatcher.wait();// Wait until pending tasks are completed, with timeoutdispatcher.wait_for(std::chrono::seconds(5));dispatcher.wait_until(std::chrono::system_clock::now() + std::chrono::seconds(5));
Add this project usingadd_subdirectory and link your target todispatch_queue:
add_subdirectory(path/to/dispatch_queue)target_link_libraries(my_target dispatch_queue)
You can pass a functor to the dispatch queue constructor that will run inside worker threads when they initialize.There you can set thread names:
dispatch_queue::dispatch_queuedispatcher(4, [](int worker_index) { std::string worker_name =std::format("worker{}", worker_index);// TODO: set thread name, platform-specific});
About
Dispatch Queue / Thread Pool implementation for C++11
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
No packages published