Talk C chestnuts together (113rd back: C language instance-semaphores for thread synchronization)

Source: Internet
Author: User
Tags overview example

Talk C chestnuts together (113rd back: C language instance-semaphores for thread synchronization)

Hello, everyone. In the previous session, we talked about the thread synchronization overview example. The example here is: LineSemaphores for Process Synchronization. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

Today, we will introduce how to use semaphores for thread synchronization. The core operations of semaphores are P/V operations. We can use related functions to process P/V Operations. Next we will introduce functions related to semaphores.

Sem_init Function

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

This function is used to initialize semaphores. This function has three parameters. Next we will introduce their respective functions.

The first parameter is a pointer of the sem_t type. The Pointer Points to a semaphore, which is the semaphore to be initialized by the function. The second parameter is an int type variable, this variable indicates the use range of semaphores. The third parameter is an int type variable, and the function will use the value of this variable to initialize the semaphores. When the function runs successfully, 0 is returned, otherwise,-1 is returned;

When using this function, the second parameter is usually set to 0, indicating that the semaphore can only be used in the current process and cannot be used between threads of different processes; if it is set to a non-zero value, the semaphore can be used between different processes.

Sem_destroy Function

Int sem_destroy (sem_t * sem );

This function is used to release resources related to semaphores. This function has only one parameter.

The parameter is a pointer of the sem_t type. The Pointer Points to a semaphore, which is the semaphore initialized by the sem_init function. If the function runs successfully, 0 is returned; otherwise,-1 is returned; sem_wait is returned.

Int sem_wait (sem_t * sem );

This function implements the P operation. When the semaphore is greater than 1, 1 is subtracted. If the semaphore is 0, the current thread is suspended;

This function has only one parameter. The parameter is a pointer of the sem_t type. The Pointer Points to a semaphore, which is the semaphore initialized by the sem_init function. If the function runs successfully, 0 is returned; otherwise,-1 is returned.

Int sem_post (sem_t * sem );

This function implements the V operation. When the semaphore is 0, add 1. If a thread is waiting for the semaphore, wake up the thread;

This function has only one parameter. The parameter is a pointer of the sem_t type. The Pointer Points to a semaphore, which is the semaphore initialized by the sem_init function. If the function runs successfully, 0 is returned; otherwise,-1 is returned;

Let's talk about the semaphores for thread synchronization. I want to know what examples will be provided later, and I will try again.

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.