线程同步:信号量
2015-06-11 20:01
363 查看
#include <semaphore.h>
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_timewait(sem_t * sem);
int sem_post(sem_t * sem);
int sem_getvalue(sem_t * sem, int * sval);
int sem_destroy(sem_t * sem);
Description:
http://manpages.ubuntu.com/manpages/dapper/man3/sem_init.3.html
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_timewait(sem_t * sem);
int sem_post(sem_t * sem);
int sem_getvalue(sem_t * sem, int * sval);
int sem_destroy(sem_t * sem);
Description:
Semaphores are counters for resources shared between threads. The basicoperations on semaphores are: increment the counter atomically, and wait until the counter is non-null and decrement it atomically.
/******************************************************************* * sem_init * * Description: * initializes the semaphore object pointed to by |sem| * * <Additional description> * LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with * error ENOSYS if |pshared| is not zero. * * Parameters: * |sem|, semaphore to be initialozed * |pshared|, indicates whether the semaphore is local to the current process * 0: local to the current process * others: shared between several processes * * Returns: * OK on success, * ERROR on failure * *****************************************************************/ int sem_init(sem_t *sem, int pshared,unsigned int value); /******************************************************************* * sem_wait * * Description: * Suspends the calling thread until the semaphore pointed to by |sem| has non-zero count. * It then atomically decreases the semaphore count. * * <Additional description> * 递减即锁定,否则就阻塞直到sem_post()释放这个锁。 * * Parameters: * * Returns: * OK on success, * ERROR on failure * *****************************************************************/ int sem_wait(sem_t * sem); /******************************************************************* * sem_trywait * * Description: * A non-blocking variant of !sem_wait!. * * <Additional description> * 1) If the semaphore pointed to by |sem| has non-zero count, the count is atomically * decreased and sem_trywait immediately returns 0. * 2) If the semaphore count is zero, sem_trywait immediately returns with error EAGAIN. * * Parameters: * * Returns: * *****************************************************************/ int sem_trywait(sem_t * sem); /******************************************************************* * sem_timedwait--lock a semaphore * * Description: * The sem_timedwait() function shall lock the semaphore referenced by sem as in the sem_wait() * function. However, if the semaphore cannot be locked when waiting for another process or * thread to unlock the semaphore by performing a sem_post() function, this wait shall be terminated * when the specified timeout expires. * (注:sem_timedwait() 与 sem_wait() 类似,只不过 abs_timeout 指定一个阻塞的时间上限,如果调用因不 * 能立即执行递减而要阻塞。abs_timeout 参数指向一个指定绝对超时时刻的结构,这个结果由自 Epoch, * 1970-01-01 00:00:00 +0000(UTC) 秒数和纳秒数构成。这个结构定义如下: * struct timespec { * time_t tv_sec; /* 秒 */ * long tv_nsec; /* 纳秒 */ * }; * 如果调用时超时时刻已经到点,并且信号量不能立即锁定,那么 sem_timedwait() 将失败于超时(errno 设置为 ETIMEDOUT)。 * 如果操作能被立即执行,那么 sem_timedwait() 永远不会失败于超时错误,而不管 abs_timeout 的值。进一步说,abs_timeout * 的验证在此时没有进行。) * * <Additional description> * * Parameters: * * Returns: * OK on success, * ERROR on failure * *****************************************************************/ int sem_timewait(sem_t * sem); /******************************************************************* * sem_post * * Description: * Atomically increases the count of the semaphore pointed to by |sem|. This function never * blocks and can safely be used in asynchronous signal handlers. * * <Additional description> * * Parameters: * * Returns: * OK on success, * ERROR on failure * *****************************************************************/ int sem_post(sem_t * sem); /******************************************************************* * sem_getvalue * * Description: * stores in the location pointed to by |sval| the current count of the semaphore |sem|. * * <Additional description> * LinuxThreads currently does not support process-shared semaphores, thus sem_init always returns with * error ENOSYS if |pshared| is not zero. * * Parameters: * |sem|, semaphore to be initialozed * |pshared|, indicates whether the semaphore is local to the current process * 0: local to the current process * others: shared between several processes * * Returns: * OK on success, * ERROR on failure * *****************************************************************/ int sem_getvalue(sem_t * sem, int * sval); /******************************************************************* * sem_destroy * * Description: * destroys a semaphore object, freeing the resources it might hold. * * <Additional description> * No threads should be waiting on the semaphore at the time sem_destroy is called. * In the LinuxThreads implementation, no resources are associated with semaphore * objects, thus sem_destroy actually does nothing except checking that no thread is waiting * on the semaphore. * * Parameters: * * Returns: * * *****************************************************************/ int sem_destroy(sem_t * sem);
http://manpages.ubuntu.com/manpages/dapper/man3/sem_init.3.html
相关文章推荐
- x210v3开发板u-boot-2012.10移植之十---串口摇身一变控制台
- 已下是转载如有冒犯,请见谅
- mongodb.core.query.Criteria 查询非空(ne)和排序(sort)
- 局域网雨云蠕虫病毒的处理
- OOAD 学习笔记 九
- poj 1269 Intersecting Lines
- Laravel控制器调用model的两种方法
- hdu 4296 Buildings(贪婪)
- spring中常用的注解
- spring无法获取jar包中的xml配置
- [LeetCode]Max Points on a Line
- System.exit()方法的作用
- hdu 3631 Shortest Path(Floyd)
- Hibernate学习笔记之三种查询方式
- x210v3开发板u-boot-2012.10移植之九---运行阵地转移,赶紧把BL2扔到内存
- SQL结构化查询语言及Mysql基本操作
- hdu 3631 Shortest Path(Floyd)
- 设计模式之 - 总结
- 汇编语言学习笔记(5)——[bx]和loop
- Karma和Jasmine自动化单元测试