SYNOPSIS
#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_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变为零。
sem_init initializes the semaphore object pointed to by sem. The count associated with the semaphore is set initially to
value. The pshared argument indicates whether the semaphore is local to the current process ( pshared is zero) or is to
be shared between several processes ( pshared is not zero). LinuxThreads currently does not support process-shared
semaphores, thus sem_init always returns with error ENOSYS if pshared is not zero.
//在使用信号量之前,我们必须初始化信号。第三个参数通常设置为零,初始化信号的计数为0,这样第一次使用sem_wait的时候会因为信号计数为0而等待,直到在其他地方信号量post了才返回。除非你明白你在干什么,否则不要将第三个参数设置为大于0的数。
//第二个参数是用在进程之间的数据共享标志,如果仅仅使用在当前进程中,设置为0。如果要在多个进程之间使用该信号,设置为非零。但是在Linux线程中,暂时还不支持进程之间的信号共享,所以第二个参数说了半天等于白说,必须设置为0.否则将返回ENOSYS错误。
sem_wait suspends the calling thread until the semaphore pointed to by sem has non-zero count. It then atomically
decreases the semaphore count.
//当信号的计数为零的时候,sem_wait将休眠挂起当前调用线程,直到信号量计数不为零。在sem_wait返回后信号量计数将自动减1.
sem_trywait is a non-blocking variant of sem_wait. If the semaphore pointed to by sem has non-zero count, the count is
atomically decreased and sem_trywait immediately returns 0. If the semaphore count is zero, sem_trywait immediately
returns with error EAGAIN.
//sem_trywait是一个立即返回函数,不会因为任何事情阻塞。根据其返回值得到不同的信息。如果返回值为0,说明信号量在该函数调用之前大于0,但是调用之后会被该函数自动减1.至于调用之后是否为零则不得而知了。如果返回值为EAGAIN说明信号量计数为0。
sem_post atomically increases the count of the semaphore pointed to by sem. This function never blocks and can safely be
used in asynchronous signal handlers.
//解除信号量等待限制。让信号量计数加1.该函数会立即返回不等待。
sem_getvalue stores in the location pointed to by sval the current count of the semaphore sem.
//获得当前信号量计数的值。
sem_destroy destroys a semaphore object, freeing the resources it might hold. 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.
// 销毁信号量对象,释放信号量内部资源。然而在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. 《semaphore ['seməfɔ:] 》
//通常返回值往往都为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了。
具体实现,自己慢慢琢磨一下吧。
static pthread_t th_download = 0;
if (!th_download) {
sem_init(&down_load_sem,0,0);
sc->timeout=current_time()+300;
pthread_create(&th_download, NULL, (void*)thread_download, (void *)sc);
tr_log(LOG_DEBUG, "@@@@@@@3,th_download:%d",th_download);/*print for download*/
return;
} else {
int ret = 0;
ret = sem_trywait(&down_load_sem);
if (ret == EAGAIN) {
return;
} else if (ret == 0) {
tr_log(LOG_DEBUG, "@@@@@@@1,res_tmp:%d",res_tmp);/*print for download*/
th_download =0;
sem_destroy(&down_load_sem);
res=res_tmp;
} else {
//tr_log(LOG_ERROR, "@@@@@@@@@@the sem is wrong,the ret is %d !", ret);/*print for download*/
return;
}
void thread_download(void *arg)
{
struct sched * sc;
struct task_item *ti;
int fault_code = 0;
int res;
int res_code = 200;
sc=(struct sched*)arg;
ti = (struct task_item *)(sc->pdata);
res = http_recv(& (ti->http), & (ti->conn));
res_tmp=res;
tr_log(LOG_DEBUG, "@@@@@@@2,res_tmp:=%d",res_tmp);/*print for download*/
sem_post(&down_load_sem);
}