Questions tagged [boost-context]

Boost.Context is a foundational library that provides a sort of cooperative multitasking on a single thread. By providing an abstraction of the current execution state in the current thread, an `fcontext_t` instance represents a specific point in the application's execution path. This is useful for building higher-level abstractions, like coroutines, cooperative threads (userland threads) or an equivalent to C# keyword `yield` in C++.

Boost.Context is a foundational library that provides a sort of cooperative multitasking on a single thread. By providing an abstraction of the current execution state in the current thread, including the stack (with local variables) and stack pointer, all registers and CPU flags, and the instruction pointer, an fcontext_t instance represents a specific point in the application's execution path. This is useful for building higher-level abstractions, like coroutines, cooperative threads (userland threads) or an equivalent to the C# keyword yield in C++.

An fcontext_t provides the means to suspend the current execution path and to transfer execution control, thereby permitting another fcontext_t to run on the current thread. This stateful transfer mechanism enables an fcontext_t to suspend execution from within nested functions and, later, to resume from where it was suspended. While the execution path represented by an fcontext_t only runs on a single thread, it can be migrated to another thread at any given time.

A context switch between threads requires system calls (involving the OS kernel), which can cost more than a thousand CPU cycles on x86 CPUs. By contrast, transferring control among them requires only fewer than a hundred CPU cycles because it does not involve system calls as it is done within a single thread.

More information about the Boost.Context library can be found on the boost website.

19 questions
15
votes
1 answer

Boost Context library

In the most recent version of the Boost the new library Context appeared. After reading the documentation I understood what it does, but can hardly see the use-cases. What are the benefits of using this library? For which tasks you could recommend…
nogard
  • 9,432
  • 6
  • 33
  • 53
11
votes
1 answer

Making thread_local variables fully volatile

I'm working on a runtime library that uses user-level context switching (using Boost::Context), and am having trouble using thread_level variables. Consider the following (reduced) code: thread_local int* volatile tli; int main() { tli = new…
Eran
  • 21,632
  • 6
  • 56
  • 89
6
votes
1 answer

Can I set the stack pointer in LLVM?

I'm working on a small c++-like language which I'll be compiling to LLVM. One of the things I want to implement is cooperative multitasking; there will be a "yield" operator which will hopefully switch the stack pointer and program counter to the…
Verdagon
  • 2,456
  • 3
  • 22
  • 36
4
votes
3 answers

boost context class

I found boost has a class called context which is used for context switching, right? I try to Google it but did not found any document or example. I am just wondering if anyone can provide some information.
Michael D
  • 1,449
  • 5
  • 18
  • 31
2
votes
1 answer

SetUnhandledExceptionFilter does not work with boost context

I'm using make_fcontext and jump_fcontext from boost context to achieve userspace context switching. I use another library to catch and report application crashes. I found these 2 does not work together on Windows. The crash library calls…
yijiem
  • 359
  • 2
  • 17
2
votes
1 answer

Minimal possible stack size on Windows when using C++ exceptions (using boost context fibers)

I'm using boost context 1.67 to create a fiber (fcontext API) with a minimal as possible stack size on Windows 10. Probably this issue isn't only specific to boost context and applies to any scenario where we use a Windows thread with a minimal…
Naios
  • 1,513
  • 1
  • 12
  • 26
2
votes
2 answers

Coroutine-local variable in boost

Im searching for something similar to thread-local variables, but for boost::corotine (actually i use boost:asio::spawn). Consider following code: void coroutine_work(boost::asio::yield_context yield) { async_foo( yield ); …
Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
2
votes
2 answers

How to use Boost.Coroutine with Boehm GC?

Boost.Coroutine allocates its own call stacks. Does Boehm GC consider pointers on these stacks as roots, and if not how can I make it do that? After a context switch to a coroutine, Boehm terminates the program.
user1804599
2
votes
1 answer

Does Boost::Context work on iOS?

I am trying to build boost::context for an iOS app. I tried following the instructions on boost's site, but I am finding them a little complicated. So I copied the /boost/context directory, and any other header's xcode asked for into my xcode…
CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
1
vote
1 answer

How to use boost::log named_scope with boost::asio::yield_context?

The thing is that when there are coroutines they can work in random order, and they can end up but BOOST_LOG_NAMED_SCOPE(...) keeps scope's name on each stackframe being oblivious to the fact that those stackframes are not nested, so they can be…
unegare
  • 2,197
  • 1
  • 11
  • 25
1
vote
1 answer

Boost 1.57 Boost.Context fcontext_t resource management

Some context In Boost 1.57 f_context & make_fcontext looks like this // fcontext.hpp: typedef void* fcontext_t; fcontext_t BOOST_CONTEXT_CALLDECL make_fcontext( void * sp, std::size_t size, void (* fn)( intptr_t) ); How should I manage the resource…
Kalevi
  • 591
  • 6
  • 14
1
vote
1 answer

Boost ASIO/Coroutines: Attempting to write an echo server using boost asio and coroutines, but am getting inconsistent behaviour

It appears that I misunderstood how windows handles sockets in TIME_WAIT when there are many sockets being opened. If too many are hanging out in TIME_WAIT, it just errors. Linux cleans up the older connections and succeeds (at least on my box,…
Seamus Connor
  • 1,765
  • 1
  • 18
  • 24
1
vote
1 answer

boost::context for arm64?

I am trying to compile boost::context for iOS(armv7, armv7s, arm64): errors jump_arm_aapcs_macho_gas.S:52:18: error: ',' expected stmia a1, {v1-v8,sp-lr} @ save V1-V8,SP-LR ^ jump_arm_aapcs_macho_gas.S:53:14: error: register…
CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
0
votes
1 answer

How to customize a coroutine state in boost asio coroutine?

The thing is that I would like to create a global instance which I would be able to use separately by each coroutine to keep there, for instance, the list of named scopes, e.g. for log purposes. so that when boost::asio::spawn is called a new custom…
unegare
  • 2,197
  • 1
  • 11
  • 25
0
votes
1 answer

boost context: problems with exception propagation

I'm using boost::context::execution_context (version 2) to write a C++ 11 library and I want to propagate exceptions from an execution_context to the calling execution. I'd like to handle exceptions inside of a lambda that a client gives to my…
1
2