【发布时间】:2019-01-03 23:04:03
【问题描述】:
可以使用 setjmp() 和 longjmp() 对 C 光纤进行编码,以在用户级别实现上下文切换。
如中所述
evanjones.ca 和Portable Multithreading(pdf) 还要求每根光纤都有一个新分配的堆栈。
由于纤程存在于线程上下文中,当它被调用时,它会自动关联一个堆栈帧,那么为什么它需要这个新分配的堆栈呢? : 当一根光纤要切换到另一根光纤时,可以使用以下方法:
cpu_context[N] :global array where the i-th entry is the cpu context(jmp_buffer) of the i-th fiber
fiber_ith :
[...]
if ( setjmp(cpu_context[i]) == 0 ){
longjmp(cpu_context[j])
}
[...]
新堆栈的必要性是因为正如 here 所写的那样,不可能使用 longjmp() 回到从 Fiber 调用的那一刻起堆栈帧不再有效的 Fiber 执行longjmp()?
编辑:这些光纤必须是非抢占式的,并且可以自愿从一根光纤切换到另一根光纤
【问题讨论】:
-
这个问题是基于错误的前提。 longjmp 不能这样用。
标签: c multithreading stack fiber