Linux???????????sleep???????
???????????? ???????[ 2012/8/29 10:50:00 ] ????????
????????pthread_cond_timedwait???????????£?
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
static pthread_t thread;
static pthread_cond_t cond;
static pthread_mutex_t mutex;
static int flag = 1;
void * thr_fn(void * arg)
{
struct timeval now;
struct timespec outtime;
pthread_mutex_lock(&mutex);
while (flag) {
printf("*****
");
gettimeofday(&now?? NULL);
outtime.tv_sec = now.tv_sec + 5;
outtime.tv_nsec = now.tv_usec * 1000;
pthread_cond_timedwait(&cond?? &mutex?? &outtime);
}
pthread_mutex_unlock(&mutex);
printf("cond thread exit
");
}
int main(void)
{
pthread_mutex_init(&mutex?? NULL);
pthread_cond_init(&cond?? NULL);
if (0 != pthread_create(&thread?? NULL?? thr_fn?? NULL)) {
printf("error when create pthread??%d
"?? errno);
return 1;
}
char c ;
while ((c = getchar()) != 'q');
printf("Now terminate the thread!
");
pthread_mutex_lock(&mutex);
flag = 0;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
printf("Wait for thread to exit
");
pthread_join(thread?? NULL);
printf("Bye
");
return 0;
}
????pthread_cond_timedwait()?????????????ú??????????????cond?????????????????pthread_cond_broadcast() or pthread_cond_signal()????
??????pthread_cond_timedwait()???????????????????????????mutex??????pthread_cond_timedwait()???mutex???С?????????ж?????????????????????????????????ζ??????????????????????????????????In this case?? atomically means with respect to the mutex andthe condition variable and other access by threads to those objectsthrough the pthread condition variable interfaces.??
???????????????????????????????????????????????????????????????mutex???????????mutex??????EPERM?????????ú??????????mutex?????????????????
???????????????abstime???????????????????????????????????ETIMEDOUT?????????????????????????????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????gettimeofday()???timeval???塣
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11