线程实施 [英] Threading Implementation

查看:183
本文介绍了线程实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何为实施我自己的线程库。结果
我已经是一个CPU(PowerPC架构)和C标准库。

I wanted to know how to implement my own threading library.
What I have is a CPU (PowerPC architecture) and the C Standard Library.

是否有一个开放源码的轻型实现我可以看看?

Is there an open source light-weight implementation I can look at?

推荐答案

在其最简单的一个线程需要:

At its very simplest a thread will need:


  1. 某些内存堆栈空间

  2. 在某处存储其上下文(即寄存器的内容,程序计数器,堆栈指针,等等。)

在,你将需要实现一个简单的内核,将负责线程切换顶部。如果你想实现pre先发制人的线程,那么你还需要中断的周期性来源。例如。一个定时器。在这种情况下,你可以在定时器中断执行你的线程切换code。

On top of that you will need to implement a simple "kernel" that will be responsible for the thread switching. And if you're trying to implement pre-emptive threading then you'll also need a periodic source of interrupts. eg. a timer. In this case you can execute your thread switching code in the timer interrupt.

看看在setjmp()/ longjmp的()程序,以及相应的jmp_buf结构。这会给你方便地访问堆栈指针,以便您可以指定自己的堆栈空间,并且会给你捕捉所有的寄存器内容提供您的线程上下文的一个简单的方法。

Take a look at the setjmp()/longjmp() routines, and the corresponding jmp_buf structure. This will give you easy access to the stack pointer so that you can assign your own stack space, and will give you a simple way of capturing all of the register contents to provide your thread's context.

通常,所述的longjmp()函数是用于从中断指令,这适合非常很好地与在定时器中断具有线程调度功能的返回的包装。您需要检查虽然longjmp的实施()和jmp_buf为您的平台。

Typically the longjmp() function is a wrapper for a return from interrupt instruction, which fits very nicely with having thread scheduling functionality in the timer interrupt. You will need to check the implementation of longjmp() and jmp_buf for your platform though.

尝试寻找在较小的微处理器,其通常不具有操作系统的线程实现。例如。爱特梅尔AVR或微芯片PIC。
例如: rel=\"nofollow\">讨论

Try looking for thread implementations on smaller microprocessors, which typically don't have OS's. eg. Atmel AVR, or Microchip PIC. For example : discussion on AVRFreaks

这篇关于线程实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆