boost::asio with boost::unique_future

Boost.Asio only provides first-class support for asynchronous operations to return a C++11 std::future or an actual value in stackful coroutines. Nevertheless, the requirements on asynchronous operations documents how to customize the return type for other types, such as Boost.Thread’s boost::unique_future. It requires: A specialization of the handler_type template. This template is used to determine the … Read more

C++ Thread Pool [closed]

I think it is still not accepted into Boost, but a good staring point: threadpool. Some example of usage, from the web site: #include “threadpool.hpp” using namespace boost::threadpool; // Some example tasks void first_task() { … } void second_task() { … } void third_task() { … } void execute_with_threadpool() { // Create a thread pool. … Read more

c++ work queues with blocking

Well. That’s really quite simple; You’re rejecting the tasks posted! template< typename Task > void run_task(task task){ boost::unique_lock<boost::mutex> lock( mutex_ ); if(0 < available_) { –available_; io_service_.post(boost::bind(&tpool::wrap_task, this, boost::function< void() > ( task ))); } } Note that the lock “waits” until the mutex is not owned by a thread. This might already be the … Read more

Example for boost shared_mutex (multiple reads/one write)?

1800 INFORMATION is more or less correct, but there are a few issues I wanted to correct. boost::shared_mutex _access; void reader() { boost::shared_lock< boost::shared_mutex > lock(_access); // do work here, without anyone having exclusive access } void conditional_writer() { boost::upgrade_lock< boost::shared_mutex > lock(_access); // do work here, without anyone having exclusive access if (something) { … Read more

C++0x has no semaphores? How to synchronize threads?

You can easily build one from a mutex and a condition variable: #include <mutex> #include <condition_variable> class semaphore { std::mutex mutex_; std::condition_variable condition_; unsigned long count_ = 0; // Initialized as locked. public: void release() { std::lock_guard<decltype(mutex_)> lock(mutex_); ++count_; condition_.notify_one(); } void acquire() { std::unique_lock<decltype(mutex_)> lock(mutex_); while(!count_) // Handle spurious wake-ups. condition_.wait(lock); –count_; } bool … Read more