Baruch
Baruch

Reputation: 21508

Context switching in different thread

Is there any way to perform a preemptive same-thread context switch?

I am pretty sure that is not the right way to describe what I am looking for, so here is a longer description:
I want the same type of behavior as POSIX ucontext_t or boost::context. There are probably others I am not aware of. I don't know the official term for it, but basically they allow single-threaded multitasking. Both those libraries require "cooperative" code, that is, the currently running "thread" must explicitly perform a context-switch.
I am looking for a way to force a context switch. I assume this would have to be done from a separate thread (real OS thread). I would want to have an array of "contexts", frozen execution paths, and be able to switch to any one I choose, freezing the current execution.

I don't care whether the solution uses a separate thread for the "switcher" (I assume this is required) or not.
I am fine with Java/C#/C++/C solutions, or a combo of C/C++ and some minimal Assembly.
I would like if it was cross platform (Windows and Linux) and cross compiler (GCC, MinGW and VS, assuming a c/c++ solution), but am aware this is not likely to be possible. At a minimum it should work on Windows with at least one compiler.
I am fine using almost any library or compatibility layer needed (e.g. boost or some sort of 'POSIX on Windows' layer)

I hope this isn't asking for to much.

Upvotes: 1

Views: 293

Answers (1)

olk
olk

Reputation: 400

boost.fiber - can be programmed like boost.thread but provides user-land threads

Upvotes: 1

Related Questions