about how to use the Pthread_cond_timedwait () timeout interface

Source: Internet
Author: User

Before we discussed that pthread_cond_timedwait () interface can not be used, if you need to return the function timeout must write a timer to achieve. But the operation is too expensive, in fact, you can use this interface directly to implement the timeout wait function, the following describes my understanding of the interface and usage, For reference only.

1. POSIX provides a variety of clock types, including the following two types:
Clock_realtime:systemwide Realtime CLOCK. System-wide real-time clock, is a software clock, you can modify the system time by means of a command.
Clock_monotonic:represents monotonic time. cannot be set. Represents a monotonous time, which can not be set up and modified for the current time of the system.

Pthread_cond_timedwait () When you do not set a condition variable property, the default is to use the Clock_realtime software time, so in extreme cases the actual wait time differs from the set timeout time.
Here's how to use the Pthread_cond_timedwait () interface correctly

2. Correct use of the pthread_cond_timedwait () interface to change the measurement time reference to Clock_monotonic, which requires setting the properties of the condition variable at initialization time, and then setting the timeout. Examples are as follows:
ret = Pthread_condattr_init (& (pthread_info.condattr));
if (ret!= 0) {
Exit (1);
}
ret = Pthread_condattr_setclock (& (Pthread_info.condattr), clock_monotonic);

ret = Pthread_cond_init (& (Pthread_info.cleanup_queue_wait), & (Pthread_info.condattr));
...
struct TIMESPEC TV;
Clock_gettime (Clock_monotonic, &TV);
pthread_task_dbg (Logs_debug, "Now time:%d\n", tv.tv_sec);
Tv.tv_sec + + 30;- here set 30 seconds to confiscate the event timeout return
ret = pthread_cond_timedwait (& (Pthread_info.cleanup_queue_wait), & (Pthread_info.cleanup_queue_lock), & TV);
...

Run as follows:


You can see from the above that the system run time will not be revised regardless of the system time set to any value, and that the thread will not be triggered back, still the original 30 seconds timeout.

3. In fact, the use of clock_realtime as a measurement time reference is also feasible, but in extreme cases (such as after the current system time to get the system time is revised immediately), this timeout is not correct, but this is a small probability event.
Examples are as follows:
struct TIMESPEC TV;
Tv.tv_sec = time (NULL);- in extreme cases system times are revised immediately after obtaining the current system time
pthread_task_dbg (Logs_debug, "Now time:%d\n", tv.tv_sec);
Tv.tv_sec + + 30;- here set 30 seconds to confiscate the event timeout return
ret = pthread_cond_timedwait (& (Pthread_info.cleanup_queue_wait), & (Pthread_info.cleanup_queue_lock), & TV);

Run as follows:


As you can see from the above, when the thread enters a timeout wait, time (NULL) returns the current setting, regardless of whether the system time is changed to the previous time, but the threads are not triggered back.

4. Reference materials:
Http://man.chinaunix.net/unix/susv3/functions/pthread_condattr_setclock.html
Http://man.chinaunix.net/unix/susv3/functions/pthread_cond_timedwait.html

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.