Linux Thread Memo

Source: Internet
Author: User
Three thread synchronization mechanisms • Mutex • semaphore • condition variable
pthread_t thread_id;
Main functions Pthread_create (), Pthread_exit (), Pthread_join (), Pthread_cancel ()

pthread_mutex_t Mutex;
The mutex consists mainly of the following basic functions: Mutex initialization: Pthread_mutex_init () Mutex Lock: Pthread_mutex_lock () Mutex Lock: Pthread_mutex_trylock () Mutex unlock: Pthread_mutex_unlock () Eliminate mutex: Pthread_mutex_destroy () The PV atomic operation used in the operating system is widely used for synchronization and mutual exclusion between processes or threads • Essentially a nonnegative integer counter, Used to control access to public resources PV atomic operations: Operation of the integer counter semaphore sem • One-time p operation causes the SEM to subtract one, while a V operation causes the SEM to add a process (or thread) to determine whether access to the public resource is available based on the value of the semaphore-when the value of the SEM is greater than or equal to zero, the A process (or thread) has access to a public resource – when the value of the semaphore SEM is less than zero, it blocks until the value of the semaphore SEM is greater than or equal to 0. PV operations are primarily used for synchronization and mutual exclusion between threads, and several threads only set a semaphore SEM synchronization, which sets multiple semaphores, Scheduling different initial values to implement the semaphore function between them sem_init () creates a semaphore and initializes it with sem_wait () and Sem_trywait (): p operation, minus the value of the semaphore when the semaphore is greater than zero • Difference: If the semaphore is less than zero, sem_wa It () will block the thread, sem_trywait () will immediately return Sem_post (): v operation, the value of the semaphore plus a simultaneous signal to wake up the waiting Thread Sem_getvalue (): Get the value of the Semaphore Sem_destroy (): Delete the signal volume

Linux Thread Memo

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.