【发布时间】:2013-09-14 12:45:54
【问题描述】:
我遇到了一个不寻常的问题,但找不到导致它的原因。
我正在开发用户级非抢占线程库。
void MyThreadYield(void) {
if(myThreadLib.readyQ->size > 0) {
ucontext_t currentContext;
getcontext(¤tContext);
myThreadLib.runningMyThread->ctx = currentContext;
queueEnqueue(myThreadLib.readyQ, myThreadLib.runningMyThread);
myThreadLib.runningMyThread = queueDequeue(myThreadLib.readyQ);
setcontext(&(myThreadLib.runningMyThread->ctx));
}
}
getcontext(&currentContext) 执行后,currentContext 中的 ss_stack 为 NULL。以下是 currentContext 的快照
而且myThreadLib.runningMyThread->ctx = currentContext;执行后修改了readyQ,不知道为什么。
但是,setcontext(&(myThreadLib.runningMyThread->ctx)); 执行良好并且上下文更改成功,但在再次调用 MyThreadYield() 时会出现 Segfault。
你能帮我解决这个问题吗?
谢谢。
【问题讨论】:
-
嗨,查理,但是在这里,在执行 getcontext(¤tContext) 之后,currentContext 的 uc_stack 为 NULL。这是我还在 MyThreadYield() 函数中的时候。此外,我将 currentContext 分配给动态分配的 MyThread 结构。
标签: c process operating-system pthreads