1

I'm trying to play with boost::fiber library but I have the problem with the "Hello, World" example. In the following code the program flow blocks after the fiber is finished and the main function never returns.

#include <iostream>
#include <boost/fiber/all.hpp>

using namespace std;

void helloFiber()
{
  cout << "Hello, boost::fiber" << endl;
}

int main()
{
  boost::fibers::fiber f(helloFiber);

  cout << "Before join." << endl;
  f.join();
  cout << "After join." << endl;

  return 0;
}

The result is:

Before join.
Hello, boost::fiber

I built boost::fiber current develop branch with the current develop branch of modular-boost. Is this behavior bug in the current implementation or there is something wrong in my usage?

bobeff
  • 3,543
  • 3
  • 34
  • 62

2 Answers2

1

your test app prints:

Before join. Hello, boost::fiber After join.

maybe you've checkout an broken version from branch develop

olk
  • 372
  • 1
  • 3
-2

try this

int main()
{
  boost::fibers::fiber f(helloFiber);
  f.detach();
  f.join();

  return 0;
}

http://www.boost.org/doc/libs/1_58_0/doc/html/thread.html

  • Welcome to SO and thank you for posting a answer. Your answer appears to get the job done, but you could improve your answer by providing a bit of context for your code (even just one complete sentence). You may find other tips at the [help page on answering question](http://stackoverflow.com/help/how-to-answer). – Richard Erickson Jul 10 '15 at 14:06
  • This throws an instance of exception: **boost::fibers::fiber_resource_error**, since after detach the fiber is no longer in joinable state. The exact error message is *boost fiber: fiber not joinable: Invalid argument*. – bobeff Jul 10 '15 at 14:08
  • hmm i'll think i through next time. Sorry for my poor quality answer. I currently do not have a machine to test on. Just one more thought: cout is not thread-safe. does your program complete if you remove the cout statements from the main routine and only keep the one in the fiber? – user2386098 Jul 10 '15 at 14:38
  • detach() followed by a join() is nonsense - after detaching `f` doesn't refer to a fiber – olk Sep 02 '15 at 12:24
  • this is nonsense – newhouse Mar 10 '17 at 09:25