Name

sem_init
- initialize an unnamed semaphore

Library

libpthread.lib

Synopsis

  #include <semaphore.h>
  int sem_init (sem_t *sem, int pshared, unsigned int value);

Return values

Upon successful completion, the sem_init function shall initialize the semaphore in sem. Otherwise, it shall return -1 and set errno to indicate the error.

Detailed description

The sem_init function initializes the unnamed semaphore pointed to by sem to have the value value. A non-zero value for pshared specifies a shared semaphore that can be used by multiple processes, which this implementation is not capable of.

Following a successful call to sem_init, sem can be used as an argument in subsequent calls to sem_wait, sem_trywait, sem_post, and sem_destroy. The sem argument is no longer valid after a successful call to sem_destroy.


Examples

sem_t psem;   
if (sem_init(&psem, 0, 1) < 0) {
        perror("sem_init");
        return -1;
   }


Limitation

Semaphore can be used only within the process, semaphore across the processes is not supported


Errors

The sem_init function will fail if:
[ENOSYS]
  the value of the pshared is not 0.
[EINVAL]
  The value argument exceeds SEM_VALUE_MAX.
[ENOSPC]
  Memory allocation error.

See also

sem_destroy, sem_getvalue, sem_post, sem_trywait in sem_wait, sem_wait


Feedback

For additional information or queries on this page send feedback

© 2008 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.

Top