69,371
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
typedef struct ucontext
{
CONTEXT thread_context;
}
ucontext_t;
int getcontext( ucontext_t *a )
{
int ret;
a->thread_context.ContextFlags = CONTEXT_FULL;
ret = GetThreadContext(GetCurrentThread(), &a->thread_context) ? 0 : -1;
return ret;
}
int setcontext( ucontext_t *a )
{
return SetThreadContext(GetCurrentThread(), &a->thread_context) ? 0 : -1;
}
int swapcontext( ucontext_t *a, ucontext_t *b )
{
int ret;
if((ret = getcontext(a)) == 0)
ret = setcontext(b);
return ret;
}
int getcontext( ucontext_t *a )
{
int ret;
a->thread_context.ContextFlags = CONTEXT_FULL;
ret = GetThreadContext(GetCurrentThread(), &a->thread_context) ? 0 : -1;
return ret;
}
这个ret会导致占用栈空间4字节,而set的时候没有这个消耗,当切换回来a时,两边栈就不平衡,导致返回地址无效了。