关于信号量sem_wait的整理(转)
SYNOPSIS
#include
int sem_init(sem_t sem, int pshared, unsigned int value);
//初始化信号量
int sem_wait(sem_t sem);
//等待信号,获取拥有权
int sem_trywait(sem_t sem);
int sem_post(sem_t sem);
//发出信号即释放拥有权
int sem_getvalue(sem_t sem, int sval);
int sem_destroy(sem_t * sem);
//注销信号量,在linux中其本质是没有任何作用的,它不做任何事情。
DESCRIPTION
This manual page documents POSIX 1003.1b semaphores, not to be confused with SystemV semaphores as described in ipc(5),
semctl(2) and semop(2).
Semaphores are counters for resources shared between threads. The basic operations on semaphores are: increment the
counter atomically, and wait until the counter is non-null and decrement it atomically.
//信号量是在多线程环境中共享资源的计数器。对信号量的基本操作无非有三个:对信号量的增加;然后阻塞线程等待,直到信号量不为空才返回;然后就是对信号量的减少。
// 在编程中,信号量最常用的方式就是一个线程A使用sem_wait阻塞,因为此时信号量计数为0,直到另外一个线程B发出信号post后,信号量计数加 1,此时,线程A得到了信号,信号量的计数为1不为空,所以就从sem_wait返回了,然后信号量的计数又减1变为零。
EPC中的
usrTimerOutProcessThread::usrTimerOutProcessThread(usrTimerLoop loop) : Mthread()
{
uiRunPosition = 0;
uiAddTimeOutEventCount = 0;
uiTakeTimeOutEventCount = 0;
uiProcTimeOutEventCount = 0;
m_Loop = loop;
m_stop = false;
m_mtx_sem = new SYS::Semaphore(1);
m_sync_sem = new SYS::Semaphore(0);
m_event_queue = new SYS::List<void >;
}
就是这么使用的。
sem_init initializes the semaphore object pointed to by sem. The count associated with the semaphore is set initially to<br /> value. The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to<br /> be shared between several processes ( pshared is not zero). LinuxThreads currently does not support process-shared<br /> semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.<br />//在使用信号量之前,我们必须初始化信号。第三个参数通常设置为零,初始化信号的计数为0,这样第一次使用sem_wait的时候会因为信号计数为0而等待,直到在其他地方信号量post了才返回。除非你明白你在干什么,否则不要将第三个参数设置为大于0的数。<br />//第二个参数是用在进程之间的数据共享标志,如果仅仅使用在当前进程中,设置为0。如果要在多个进程之间使用该信号,设置为非零。但是在Linux线程中,暂时还不支持进程之间的信号共享,所以第二个参数说了半天等于白说,必须设置为0.否则将返回ENOSYS错误。<br /> sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically<br /> decreases the semaphore count.<br />//当信号的计数为零的时候,sem_wait将休眠挂起当前调用线程,直到信号量计数不为零。在sem_wait返回后信号量计数将自动减1.<br /> sem_trywait is a non-blocking variant of sem_wait. If the semaphore pointed to by sem has non-zero count, the count is<br /> atomically decreased and sem_trywait immediately returns 0. If the semaphore count is zero, sem_trywait immediately<br /> returns with error EAGAIN.<br />//sem_trywait是一个立即返回函数,不会因为任何事情阻塞。根据其返回值得到不同的信息。如果返回值为0,说明信号量在该函数调用之前大于0,但是调用之后会被该函数自动减1.至于调用之后是否为零则不得而知了。如果返回值为EAGAIN说明信号量计数为0。<br /> sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be<br /> used in asynchronous signal handlers.<br />//解除信号量等待限制。让信号量计数加1.该函数会立即返回不等待。<br /> sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem.<br />//获得当前信号量计数的值。<br /> sem_destroy destroys a semaphore object, freeing the resources it might hold. No threads should be waiting on the<br /> semaphore at the time sem_destroy is called. In the LinuxThreads implementation, no resources are associated with<br /> semaphore objects, thus sem_destroy actually does nothing except checking that no thread is waiting on the semaphore.<br />// 销毁信号量对象,释放信号量内部资源。然而在linux的线程中,其实是没有任何资源关联到信号量对象需要释放的,因此在linux中,销毁信号量对象的 作用仅仅是测试是否有线程因为该信号量在等待。如果函数返回0说明没有,正常注销信号量,如果返回EBUSY,说明还有线程正在等待该信号量的信号。
CANCELLATION
sem_wait is a cancellation point.
ASYNC-SIGNAL SAFETY
On processors supporting atomic compare-and-swap (Intel 486, Pentium and later, Alpha, PowerPC, MIPS II, Motorola 68k),
the sem_post function is async-signal safe and can therefore be called from signal handlers. This is the only thread syn-
chronization function provided by POSIX threads that is async-signal safe.
On the Intel 386 and the Sparc, the current LinuxThreads implementation of sem_post is not async-signal safe by lack of
the required atomic operations.
//现在sem_post被POSIX所规范,当它改变信号量计数器值的时候是线程安全的。
RETURN VALUE
The sem_wait and sem_getvalue functions always return 0. All other semaphore functions return 0 on success and -1 on
error, in addition to writing an error code in errno.
//通常返回值往往都为0,表示成功,如果有误将返回-1,并且将错误的代码号赋值给errno。
ERRORS
The sem_init function sets errno to the following codes on error:
//sem_init失败时,常见错误有:
EINVAL value exceeds the maximal counter value SEM_VALUE_MAX
//第三个参数value值超过了系统能够承受的最大值SEM_VALUE_MAX.
ENOSYS pshared is not zero
//你将第二参数设置为了非零,如果是linux系统,请将第二个参数设置为零
The sem_trywait function sets errno to the following error code on error:
EAGAIN the semaphore count is currently 0
The sem_post function sets errno to the following error code on error:
ERANGE after incrementation, the semaphore value would exceed SEM_VALUE_MAX (the semaphore count is left unchanged
in this case)
//信号量的计数超过了系统能够承受的最大值SEM_VALUE_MAX。
The sem_destroy function sets errno to the following error code on error:
EBUSY some threads are currently blocked waiting on the semaphore.
//某些线程正在使用该信号量等待。
其实线程临界区可以使用信号量来实现,将信号量的信号初始化为1,然后在临界区使用完毕后再置信号量为1我们就可以轻松实现mutex了。
具体实现,自己慢慢琢磨一下吧。
首先你得知道什么叫信号量,什么时候要用信号量。
这个嘛,主要就是用来保护共享资源的,也就是说如果你想限制某个(些)资源在同一时刻只能有一(多)个线程拥有,就可以使用信号量。当然也可以用作让一个线程等待另一个线程完成某项工作。
用下面一组函数(系统调用)来实现。
int sem_init(sem_t sem,int pshared,unsigned int value);
int sem_destroy(sem_t sem);
int sem_wait(sem_t sem);
int sem_trywait(sem_t sem);
int sem_post(sem_t sem);
int sem_getvalue(sem_t sem);
具体要Include什么头文件,在你的系统上man sem_init吧。
这组函数是POSIX标准的无名信号量函数,另外还有具名信号亮,这个嘛,等下回再说。
看一个例子,比如有两个线程都要往打印机上打东西,但是同一时刻只能打一个。
那么首先用sem_init初始化一个信号量,注意pshared表示允许几个进程共享该信号量,一般设0用于进程内的多线程共享,要看是否支持进程共享,请查看下你的系统的man手册。
第三个参数value表示可用的资源的数目,即信号灯的数目,咱们这儿只有1个打印机所以设成1。
然后线程调用sem_wait取获取这个信号灯,第一个线程一看,有1个,他就拿到了,然后可以继续后继操作,此时信号灯自动减1,变成0个。那么第二个线程调用sem_wait时就会阻塞在这儿了。
第一个线程完成打印后,调用sem_post释放信号灯,信号灯数目变成1,将会唤醒等待的第二个线程,然后第二个线程接着打印。
最后当所有任务完成后,主线程调用sem_destroy释放这个信号量。
OK?
另外几个函数sem_trywait ,顾名思义,望文生义,就是测试一下看看现在有没有可用的信号灯,而不会阻塞。
sem_getvalue嘛就是读取当前的信号灯的数目。
EPC中usrTimerLoop::usrTimerLoop() 使用的
usrTimerOutProcessThread::usrTimerOutProcessThread
{
m_mtx_sem = new SYS::Semaphore(1);
}
就是这种使用情况。