Application of conditional lock pthread_cond_t

Source: Internet
Author: User

Name
Pthread_cond_init, pthread_cond_destroy, pthread_cond_signal,
Pthread_cond_broadcast, pthread_cond_wait, pthread_cond_timedwait-status operation.
  
Outline
# Include
Pthread_cond_t cond = pthread_cond_initializer;
Int pthread_cond_init (pthread_cond_t * cond, pthread_condattr_t * cond_attr );
Int pthread_cond_signal (pthread_cond_t * Cond );
Int pthread_cond_broadcast (pthread_cond_t * Cond );
Int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex );
Int pthread_cond_timedwait (pthread_cond_t * cond, pthread_mutex_t * mutex, const struct
Timespec * abstime );
Int pthread_cond_destroy (pthread_cond_t * Cond );
  
Description
A state variable is a synchronization device that allows threads to suspend and discard CPU time to wait for some shared variables to meet the state. Basic operation of the operation status variable: send a signal when the status is satisfied, wait for the status to meet, and suspend the thread before other threads send the status signal.
State variables are usually associated with mutex. To avoid competition, a thread must set the State signal before waiting for a state variable.
  
Pthread_cond_init initializes the state variable Cond and uses the state attribute defined in cond_attr. If cond_attr is null, the default attribute is used. Linuxthreads supports cond_attr without any attributes. Therefore, cond_attr is ignored.
Variables of the pthread_cond_t type can also use pthread_cond_initializer. Constant for static initialization.
  
The pthread_cond_signal function restarts a thread waiting for the cond variable. If no thread is waiting for the cond variable, no operation is performed. If multiple threads are waiting, a matching thread will be restarted, but not necessarily.
  
The pthread_cond_broadcast function restarts all threads waiting for the cond variable. If no thread is waiting for the cond variable, no operation is performed.
  
The pthread_cond_wait function unlocks the mutex (like pthread_unlock_mutex) and waits for the status signal. The thread is suspended and does not consume CPU time until the sending status signal is sent. The mutex must be locked by the caller. The mutex lock is owned by pthread_cond_wait before the call thread is returned.
  
Releasing mutex and hanging on status variables are automatically performed. Therefore, if all threads require mutex before the status signal, this will ensure that the status variable will not trigger the signal during the time when the thread locks the mutex on the status variable.
  
The pthread_cond_timedwait function automatically releases the mutex and waits for the cond state, just like what pthread_cond_wait does, but it limits the maximum wait time. If the cond is not marked with a time not specified in abstime within a certain period of time, the mutual exclusion will be re-obtained and the error code etimeout will be returned. The abstime parameter specifies the absolute time, which is the same as the time (2) and gettimeofday (2): Start from GMT on January 1, January 1, 1970.
  
The pthread_cond_destroy function destroys the state variable and releases the resources it may hold. No thread must wait for the status variable here. In linuxthreads implementation, state variables are not related to any resources, so this interface does not do anything except check whether there is a waiting thread on the state variables.
  
Cancel
Pthread_cond_wait and pthread_cond_timedwait are both canceling points. If a thread is suspended in a function and canceled, the thread starts execution again and then locks the mutex. (Not complete !)
  
Synchronous signal Security
Status functions are not synchronous secure and should not appear in signal processing functions. Specifically, calling pthread_cond_signal or pthread_cond_broadcast from the signal processing function will lead to a deadlock.
  
Return Value
All status variable functions return 0 upon success, and an error code upon error.
  
Error Code
Pthread_cond_init, pthread_cond_signal, pthread_cond_broadcast, and pthread_cond_wait never return error codes.
  
The pthread_cond_timedwait function returns the following error code when an error occurs:
Etimeout: The status variable does not activate the signal within the abstime time limit.
Eintr: pthread_cond_timedwait is interrupted by the signal.
  
The pthread_cond_destroy function returns the following error code when an error occurs:
Ebusy: A thread is waiting on the cond.
  
Author
Xavier Leroy
  
See
Pthread_condattr_init (3), pthread_mutex_lock (3), pthread_mutex_unlock (3)
Gettimeofday (2), nanosleep (2 ).
  
Example
Two shared variables X and Y are considered to be protected by muts. A state variable cond is used to stimulate signals when X is greater than Y.
Int X, Y;
Pthread_mutex_t mut = pthread_mutex_initializer;
Pthread_cond_t cond = pthread_cond_initializer;
  
Wait until X is greater than Y:
  
Pthread_mutex_lock (& MUT );
While (x <= y ){
Pthread_cond_wait (& cond, & MUT );
}
/* Operate on x and y */
Pthread_mutex_unlock (& MUT );
  
Modifying X and Y may make X greater than Y. If necessary, the following signal should be sent:
  
Pthread_mutex_lock (& MUT );
/* Modify x and y */
If (x> Y) pthread_cond_broadcast (& Cond );
Pthread_mutex_unlock (& MUT );
  
If it can be proved that at most one waiting thread needs to wake up (for example, there are only two threads, connected by X and Y), pthread_cond_signal can be used as an optional more efficient lightweight pthread_cond_broadcast .. If not, use the pthread_cond_broadcast function.
  
Wait for X to be greater than Y in 5 seconds, as shown below:
Struct timeval now;
Struct timespec timeout;
Int retcode;
  
Pthread_mutex_lock (& MUT );
Gettimeofday (& now );
Timeout. TV _sec = now. TV _sec + 5;
Timeout. TV _nsec = now. TV _usec * 1000;
Retcode = 0;
  
While (x <= Y & retcode! = Etimedout ){
Retcode = pthread_cond_timedwait (& cond, & Mut, & timeout );
}
  
If (retcode = etimedout ){
/* Timeout occurred */
} Else {
/* Operate on x and y */
}
  
Pthread_mutex_unlock (& MUT );

This post from: http://blog.tianya.cn/blogger/post_read.asp? Blogid = 1285060 & postid = 11795033

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.