How to have multiple fibers in C++ -- like a vector of fibers

Viewed 160

I want to have multiple fibers running like so.

#include <boost/fiber/fiber.hpp>    

#include <vector>

void printNum(int i) {
   std::cout << i << std::endl;
}

void handleInts (std::vector<std::unique_ptr<int>>& ints) {
  std::vector<boost::fibers::fiber> runningFibers;
  for (auto& integer : ints) {
     runningFibers.emplace_back(boost::fibers::fiber([&](){ printNum(*integer);));
  }
}

int main() {
   
  std::vector<std::unique_ptr<int>> ints;
  
  auto firstInt = std::make_unique<int>(1);
  auto secondInt = std::make_unique<int>(2);

  ints.emplace_back(std::move(firstInt));
  ints.emplace_back(std::move(secondInt));

  handleInts(ints);
  
  return 0;
}

However this causes some kind of memory leak when I run with valgrind. How do I do what I am trying to do here but without memory leaks. I looked at boost's documentation but got confused.

0 Answers
Related