【发布时间】:2020-04-10 20:21:51
【问题描述】:
我下面有这段代码
volatile int done = 0;
int main() {
ucontext_t one;
ucontext_t two;
getcontext(&one);
printf("done = %d\n", done);
if(!done) {
done = 1;
printf(" - gurka -\n");
swapcontext(&two, &one);
printf(" - tomat -\n");
} else {
printf(" - salad -\n");
swapcontext(&one, &two);
printf(" - morot -\n");
}
printf(" - potät -\n");
return 0;
}
当它执行后,我在下面得到这个
done = 0
- gurka -
done = 1
- salad -
- tomat -
- potät -
为什么它会像这样打印出来我有这个函数 swapcontext() 并且在手册页中它是这样写的,它将上下文保存在另一个线程中我不明白为什么它会写出这个?
【问题讨论】:
-
您好,您应该在 C 中测试这个吗?
-
“POSIX.1-2008 删除了 getcontext() 规范,理由是可移植性问题,并建议重写应用程序以改用 POSIX 线程。”
标签: c operating-system