使用setjumploopjump在线程中跳转的问题使用setjump到另一个线程中跳转,跳转成功了,后面的代码不执行了#define_CRT_SECURE_NO_WARNINGS#include#include#include#include#include#include#includejmp_bufbuf;void*print_a(void*);void*print_b(void*);//线程A方法void*print_a(void*a){for(inti=0;i
2 回答
一只萌萌小番薯
TA贡献1795条经验 获得超7个赞
讲真,我没想到还能这么用....setjmp/longjmpisimplementedbysavingtheregister(includingstackandcodepointeretc)whenfirstpassed,andrestoringthemjumping.当线程b通过longjmp跳到print_a时,它的栈帧和线程a的栈帧所在的内存就是一个地方了(原来栈的内存没人管)。当线程a退出时,自然要弹出所有栈帧,那么线程b之后对栈访问就是对非法内存的访问了。就像你把双胞胎中的弟弟的头嫁接到哥哥身上,哥哥变成了连体婴儿,哥哥挂掉之后,弟弟也活不成了,反之也是
慕桂英3389331
TA贡献2036条经验 获得超8个赞
longjmp不能跨线程。C117.13.2.1/2:Thelongjmpfunctionrestorestheenvironmentsavedbythemostrecentinvocationofthesetjmpmacrointhesameinvocationoftheprogramwiththecorrespondingjmp_bufargument.Iftherehasbeennosuchinvocation,oriftheinvocationwasfromanotherthreadofexecution,orifthefunctioncontainingtheinvocationofthesetjmpmacrohasterminatedexecutionintheinterim,oriftheinvocationofthesetjmpmacrowaswithinthescopeofanidentifierwithvariablymodifiedtypeandexecutionhasleftthatscopeintheinterim,thebehaviorisundefined
添加回答
举报
0/150
提交
取消