使用 sema_init(3C) 可以通过 count 值来初始化 sp 所指向的信号变量。
#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() 在成功运行后返回 0。如果检测到以下任一情况,sema_init() 将失败并返回对应的值。
EINVAL
描述:sp 引用的信号无效。
EFAULT
描述:sp 或 arg 指向的地址非法。