I'd like to use Boost.Fibers hosted by worker threads instead of just threads. I think I've done everything as in the manual while writing the code shown below, but it seems not to work – "<anonymous> called" is missing in the output.
Does anyone have an idea why?
#include <boost/fiber/all.hpp>
#include <boost/thread/barrier.hpp>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <thread>
#define RIGHTNOW (std::cerr << "[thread " << std::this_thread::get_id() << "] ")
static inline
void run_worker(uint32_t threads, boost::barrier& barrier) {
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") called" << std::endl;
boost::fibers::use_scheduling_algorithm<boost::fibers::algo::work_stealing>(threads);
barrier.wait();
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") awaited everything else" << std::endl;
boost::fibers::mutex mutex;
boost::fibers::condition_variable_any cv;
mutex.lock();
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") locked mutex" << std::endl;
cv.wait(mutex);
RIGHTNOW << "run_worker(" << threads << ", " << (void*)&barrier << ") awaited CV" << std::endl;
mutex.unlock();
}
void start_engine(uint32_t threads) {
RIGHTNOW << "start_engine(" << threads << ") called" << std::endl;
if (threads < 1u) {
threads = 1;
}
boost::barrier barrier (threads + 1u);
auto scheduler ([threads, &barrier]() { run_worker(threads, barrier); });
for (auto i (threads); i; --i) {
std::thread(scheduler).detach();
}
barrier.wait();
RIGHTNOW << "start_engine(" << threads << ") awaited everything else" << std::endl;
}
int main() {
RIGHTNOW << "main() called" << std::endl;
start_engine(1);
boost::fibers::fiber([]() {
RIGHTNOW << "<anonymous> called" << std::endl;
_Exit(1);
}).detach();
RIGHTNOW << "main() waiting" << std::endl;
for (;;) {
}
}
clang++ -std=c++11 -I ~/homebrew/Cellar/boost/1.70.0/include -L ~/homebrew/Cellar/boost/1.70.0/lib -l boost_fiber-mt -l boost_context-mt -l boost_thread-mt -o fibers fibers.cpp
./fibers
Aucun commentaire:
Enregistrer un commentaire