Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Dilawar Singh
Dilawar Singh

Posted on • Edited on

An example of `boost::fiber` library

Fiber isjust a thread implemented in user space.

Fibers are easier to reason about than threads. And have much cheaper context switching cost. Fibers are well suited for handling concurrent IO operations where a processor mostly wait for data, and threads usually have pretty big context switching cost. So multiple fibers running in a single thread is an effective solution.

Here is a simple program I wrote to explore fibers. You can find the full example below.

playground/fiber.cpp at a5fba9f21ff121249c71bff75ee8964c1016aa3f · dilawar/playground

This program has two functions:print_a printsa andprint_b printsb and then launches a thread that printsB (in detached mode).

voidprint_a(){cout<<"a";boost::this_fiber::yield();}voidprint_b(){cout<<"b";std::threadj([](){printf("B");});j.detach();boost::this_fiber::yield();}
Enter fullscreen modeExit fullscreen mode

Following is themain function. We created a shared variablei initialized to 0. We create twodetached fibers. The first one keeps callingprint_a tilli < 20. Similarly, the second one loops onprint_b tilli < 20. Both incrementi by 1. Wheni = 20, both fibers should be able tojoin.

intmain(){inti=0;boost::fibers::fiber([&](){do{print_a();i++;}while(i<20);}).detach();boost::fibers::fiber([&](){do{i++;print_b();}while(i<20);}).detach();printf("X");return0;}
Enter fullscreen modeExit fullscreen mode

Let’s guess the output of this program. It is most likely to be the same as ifstd::threads were used instead of fiber.

X is printed first?Yes. Note thatdetach() is called on each fiber so neither of their functions is called. They are put in the background. Control passes to the fiber manager atreturn 0; when it asks the fibers tojoin. In fact, you can put more computations after theprintf("X"); statement, and it would be computed before any fiber is called.

As soon as we try to return from themain, the fiber manager is asked tojoin the fibers. The first fiberawakes,a is printed, and the fiberyields the control to the manager. The fiber manager then wakes up the second fiber (who was waiting in the queue) that printsb and also launch a thread in the background that printsB. We can not be sure ifB will be printed immediately after theb (it is astd::thread).print_b yields the control and goes to sleep. The fiber manager wakes up first fiber again that callsprint_a again, anda is printed, and so on. Note thati is incremented every time either of the fibers is called.

Wheni hits 20, both fibers terminate andjoined, and the main functionreturn 0;. So we haveprint_a called ten times, andprint_b is also called ten times. In the output, we should have tenas, tenbs, and 10Bs.B may not strictly followb, butb must come aftera.

Here are a few runs of the program. Note that the location ofB is not deterministic.

  • XababBabBabBababBBabBabBabBabBB
  • XababBabBabBabBabBabBabaBbBabBB
  • XababBabBabBabBabBabBabBabBabBB
  • XababBabBabBabBabBabBabBabBabBB
  • XababBabBabBBababBabBabBabBabBB
  • XababBabBabBabBabBabBabBabBabBB
  • XababBabBabBababBBabBabBababBBB
  • XababBabBabBababBBabBabBabBabBB
  • XababBabBabBababBBabBabBabBabBB
  • XababBabBabBabBabBababBBabBabBB
  • XababBabBabBabBabBabBabBabBabBB

References

  1. A great talk by Nat on Boost fibershttps://youtu.be/e-NUmyBou8Q

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp