实现用户级线程包 [英] Implementing a User-Level Threads Package
问题描述
我的职责在一个类来创建一个C.用户级线程库我想知道是否有人可以给我的事情的清单上读到做到这一点。我有一个好主意,因为从哪里开始,但可能会帮助用户级线程的任何资源以及C语言的一些应用方面将是非常有价值的。
I have been tasked in a class to create a user level thread library in C. I was wondering if anyone could give me a list of things to read up on to accomplish this. I have a good idea as to where to start, but any resources on user level threads and some applicable aspects of the C language that might help would be extremely valuable.
我为我将如何实现这样的调度很不清楚。假设我有C语言的pretty很好的理解和它的一些更有帮助的库函数。
I am very unclear as to how I would implement a scheduler for such. Assume that I have a pretty good understanding of the C language and some of its more helpful library functions.
推荐答案
我无需编写任何汇编都这样做了家庭作业。线程切换机制是的setjmp
/ 的longjmp
。这是什么是涉及每个线程的堆栈中分配内存,然后非常仔细地按摩中的值 jmp_buff
因此执行跳转到下一个线程的堆栈。
I’ve done this for a homework assignment without writing any assembler at all. The thread switch mechanism was setjmp
/longjmp
. What this involved was allocating memory for each thread’s stack, then very carefully massaging the values in the jmp_buff
so execution jumps to the next thread’s stack.
又见拉斯考克斯的pretty可读 libtask 。
See also Russ Cox’s pretty readable libtask.
编辑回应OP的评论:在决定何时切换线程两个主要方向有:preemptive&安培;合作社。在preemptive模式,你就会有类似的东西,使执行流程跳转到一个中央调度线程,选择下一个线程来运行一个计时器信号。在合作模式中,线程收益给对方,明确地(的例如的,通过调用收益率()
功能,你会提供)或隐式(例如的,请求由另一个线程持有的锁)。
Edit in response to OP’s comment: In deciding when to switch threads there are two main directions: preemptive & cooperative. In the preemptive model, you’ll have something like a timer signal that causes execution flow to jump to a central dispatcher thread, which chooses the next thread to run. In a cooperative model, threads "yield" to each other, either explicitly (e.g., by calling a yield()
function you’ll provide) or implicitly (e.g., requesting a lock held by another thread).
看看libtask的API的合作模式,该功能特别说明的一个例子 taskyield()
。这是明确的收益,我提到。也有非阻塞I / O功能,包括一个隐含的收益率,目前的任务被搁置,直到I / O完成,但其他任务得到他们的机会运行。
Take a look at the API of libtask for an example of the cooperative model, particularly the description of the function taskyield()
. That’s the explicit yield I mentioned. There are also the non-blocking I/O functions which include an implicit yield—the current "task" is put on hold until the I/O completes, but the other tasks get their chance to run.
这篇关于实现用户级线程包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!