多线程编程指南

初始化信号

使用 sema_init(3C) 可以通过 count 值来初始化 sp 所指向的信号变量。

sema_init 语法

#include <thread.h>



int sema_init(sema_t *sp, unsigned int count, int type,

    void *arg);

type 可以是以下值之一:

多个线程决不能同时初始化同一个信号。不得对其他线程正在使用的信号重新初始化。

进程内信号

#include <thread.h>



sema_t sp;

int ret;

int count;

count = 4;



/* to be used within this process only */

ret = sema_init(&sp, count, USYNC_THREAD, 0); 

进程间信号

#include <thread.h>



sema_t sp;

int ret;

int count;

count = 4;



/* to be used among all the processes */

ret = sema_init (&sp, count, USYNC_PROCESS, 0); 

sema_init 返回值

sema_init() 在成功运行后返回 0。如果检测到以下任一情况,sema_init() 将失败并返回对应的值。


EINVAL

描述:

sp 引用的信号无效。


EFAULT

描述:

sparg 指向的地址非法。