Multitasking using setjmp, longjmp

后端 未结 4 693
北荒
北荒 2020-12-13 10:54

is there a way to implement multitasking using setjmp and longjmp functions

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 11:11

    This is a form of what is known as userspace context switching.

    It's possible but error-prone, especially if you use the default implementation of setjmp and longjmp. One problem with these functions is that in many operating systems they'll only save a subset of 64-bit registers, rather than the entire context. This is often not enough, e.g. when dealing with system libraries (my experience here is with a custom implementation for amd64/windows, which worked pretty stable all things considered).

    That said, if you're not trying to work with complex external codebases or event handlers, and you know what you're doing, and (especially) if you write your own version in assembler that saves more of the current context (if you're using 32-bit windows or linux this might not be necessary, if you use some versions of BSD I imagine it almost definitely is), and you debug it paying careful attention to the disassembly output, then you may be able to achieve what you want.

提交回复
热议问题