The use of the signal volume SEM

Source: Internet
Author: User

#include <semaphore.h>

sem_t sem;

Sem_init (&sem, 0, 0);

Sem_post (&sem);

Sem_wait (&sem);

Sem_destroy (&sem);

The data type of the semaphore is the structure sem_t, which is essentially a number of long integers. The function Sem_init () is used to initialize a semaphore. Its prototype is:

extern int Sem_init __p ((sem_t *__sem, int __pshared, unsigned int __value));

SEM is a pointer to the structure of the semaphore;

Pshared not 0 O'Clock this semaphore is shared among processes, otherwise it can be shared only for all threads of the current process;

Value gives the initial value of the semaphore.

The function sem_post (sem_t *sem) is used to increase the value of the semaphore.

When a thread is blocked on this semaphore, calling this function causes one of the threads to not block, and the selection mechanism is also determined by the thread's scheduling policy.

The function sem_wait (sem_t *sem) is used to block the current thread until the value of the semaphore SEM is greater than 0, and the value of the SEM is reduced by one after unblocking, indicating that the public resources are decreased after use. The function sem_trywait (sem_t *sem) is a non-blocking version of the function sem_wait (), which directly minimizes the value of the semaphore SEM.

 

The function Sem_destroy (sem_t *sem) is used to release the signal volume of the SEM.

---------------------------------------------------------------

The instances that have been seen are used on the main thread of blocking, and Sem_post (SEM) is used when the program runs the end resource release. To end the blocking.

Ext.: http://blog.sina.com.cn/s/blog_af9acfc6010191bj.html

The use of the signal volume SEM

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.