1

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.

2

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think there is a boost.fiber. At least, I just looked for it and couldn't find it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.