c - where have example of context switching -
assume function pointer pointed passed pthread_create , after thread run, change address of function pointer static function b a
i not understand how 1 thread switch bwetween 2 functions , b push , pop save context of function when interrupt thread of it
any example show context switching
it's hard tell you're asking here... i'll give shot.
to address first paragraph, think you're asking happen when pass function pointer (which points function a()
) pthread_create()
, modify function pointer point different function (function b()
). answer modifying function pointer has no effect on running thread; since passed copy of function pointer pthread_create()
, running thread not detect modify function pointer later on.
i believe you're after else, though: how thread context switch implemented? let's assume moment you're doing cooperative multitasking, i.e. running thread explicitly needs yield cpu calling yield()
function. here's pseudocode simple yield function might like; we'll call running thread "thread a" , thread we're switching "thread b":
void yield() { save registers stack save current stack pointer in data structure dedicated thread choose different thread yield (in our case, thread b) change stack pointer stack pointer saved thread b restore registers }
that's right, thread context switch, in essence, means switching stack pointer different stack. key effect of return address on stack no longer 1 put on stack when thread called yield()
; instead, return address 1 thread b put on stack last time it called yield()
. when yield()
returns, returns wherever thread b last time yielded control of cpu.
pretty funky, huh? takes moment head wrapped round it.
if want extend cooperative multitasking preemptive multitasking, have in principle create interrupt gets called @ regular intervals timer; interrupt service routine interrupt yield()
.
real implementations have lot more details take care of, in principle, there thread context switches -- means switching stacks.
Comments
Post a Comment