哪里有上下文切换的例子
假设指向 A 的函数指针被传递给 pthread_create 线程运行后,我将函数指针的地址从 A 更改为静态函数 B
我不明白一个线程如何通过 push 和 pop 在两个函数 A 和 B 之间切换 因为我想在中断函数 A 的线程时保存函数 A 的上下文,
任何示例都可以显示此上下文切换
assume function pointer pointed to A is passed to pthread_create
and after the thread run, i change the address of function pointer to static function B from A
I do not understand how one thread switch bwetween two functions A and B with push and pop
as i would like to save the context of function A when interrupt the thread of it
any example to show this context switching
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难说你在这里问什么......但我会尝试一下。
为了解决您的第一段,我认为您要问的是当您将函数指针(指向函数
A()
)传递给pthread_create()
时会发生什么,然后修改函数指针以指向不同的函数(函数B()
)。答案是修改函数指针对正在运行的线程没有影响;由于您将函数指针的副本传递给pthread_create()
,因此正在运行的线程稍后不会检测到您修改了函数指针。不过,我相信您确实在追求其他东西:线程上下文切换是如何实现的?我们暂时假设您正在执行协作式多任务处理,即正在运行的线程明确需要通过调用
yield()
函数来让出 CPU。下面是一个简单的yield 函数的伪代码;我们将当前正在运行的线程称为“线程 A”,将要切换到的线程称为“线程 B”:没错,线程上下文切换本质上只是意味着将堆栈指针切换到不同的堆栈。这样做的关键效果是,堆栈上的返回地址现在不再是线程 A 调用
yield()
时放入堆栈的地址;相反,返回地址是线程 B 上次调用yield()
时放入堆栈的地址。因此,当 yield() 返回时,它会返回到线程 B 上次放弃 CPU 控制权时的位置。很时髦,是吧?你需要花点时间才能理解它。
如果您想将这种协作式多任务处理扩展到抢占式多任务处理,那么原则上您所要做的就是创建一个由计时器定期调用的中断;该中断的中断服务程序就是
yield()
。真正的实现有很多细节需要处理,但原则上,这就是线程上下文切换的全部内容——它实际上只意味着切换堆栈。
It's hard to tell what you're asking here... but I'll give it a shot.
To address your first paragraph, I think what you're asking is what will happen when you pass a function pointer (which points to function
A()
) topthread_create()
, then modify the function pointer to point to a different function (functionB()
). The answer is that modifying the function pointer has no effect on the running thread; since you passed a copy of the function pointer topthread_create()
, the running thread will not detect that you modify your function pointer later on.I believe you're really after something else, though: How is a thread context switch implemented? Let's assume for the moment that you're doing cooperative multitasking, i.e. a running thread explicitly needs to yield the CPU by calling a
yield()
function. Here's pseudocode for what a simple yield function might look like; we'll call the currently running thread "thread A" and the thread to which we're switching "thread B":That's right, a thread context switch, in essence, simply means switching the stack pointer to a different stack. The key effect of this is that the return address on the stack is now no longer the one that was put on the stack when thread A called
yield()
; instead, the return address is the one that thread B put on the stack the last time it calledyield()
. So whenyield()
returns, it returns to wherever thread B was the last time it yielded control of the CPU.Pretty funky, huh? It takes a moment to get your head wrapped round it.
If you want to extend this cooperative multitasking to preemptive multitasking, then all you have to do in principle is create an interrupt that gets called at regular intervals by a timer; the interrupt service routine for this interrupt is simply
yield()
.Real implementations have a lot more details to take care of, but in principle, this is all there is to thread context switches -- it really just means switching stacks.