semaphore接口介绍

  • 需要包含头文件#include <posix/semaphore.h>;

  • 定义了系统信号量相关的接口

[English]

Header File

Functions

int sem_destroy(sem_t *sem)

Destroy an unnamed semaphore.

备注

Semaphore is destroyed regardless of whether there is any thread currently blocked on this semaphore.

Return values

0 – - upon successful completion

int sem_getvalue(sem_t *sem, int *sval)

Get the value of a semaphore.

备注

If sem is locked, then the object to which sval points is set to zero.

Return values

0 – - Upon successful completion

int sem_init(sem_t *sem, int pshared, unsigned value)

Initialize an unnamed semaphore.

@sideeffect Possible errno values

EINVAL - The value argument exceeds {SEM_VALUE_MAX}.

ENOSPC - A resource required to initialize the semaphore has been exhausted.

备注

pshared is ignored. Semaphores will always be considered “shared”.

Return values
  • 0 – - upon successful completion

  • -1 – - otherwise. System error variable errno is also set in this case.

int sem_post(sem_t *sem)

Unlock a semaphore.

Return values

0 – - upon successful completion

int sem_timedwait(sem_t *sem, const struct timespec *abstime)

Lock a semaphore with timeout.

@sideeffect Possible errno values

EINVAL - parameter specified a nanoseconds field value less than zero or greater than or equal to 1000 million

ETIMEDOUT - The semaphore could not be locked before the specified timeout expired.

备注

Deadlock detection is not implemented.

Return values
  • 0 – - upon successful completion

  • -1 – - otherwise. System error variable errno is also set in this case.

int sem_trywait(sem_t *sem)

Lock a semaphore if available.

@sideeffect Possible errno values

EAGAIN - The semaphore was already locked, so it cannot be immediately locked by the

sem_trywait() operation.

Return values
  • 0 – - upon successful completion

  • -1 – - otherwise. System error variable errno is also set in this case.

int sem_wait(sem_t *sem)

Lock a semaphore.

备注

Deadlock detection is not implemented.

Return values
  • 0 – - upon successful completion

  • -1 – - otherwise. System error variable errno is also set in this case.

Type Definitions

typedef PosixSemType_t sem_t

Semaphore type.