pthread_cond_wait执行失败
来源:互联网 发布:淘宝设计美工招聘 编辑:程序博客网 时间:2024/06/16 00:35
Printf("execute result:%d",pthread_cond_wait(&reply_cond, &reply_mutex));
Description
The pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. They shall be called withmutex locked by the calling thread or undefined behavior results.
These functions atomically release mutex and cause the calling thread to block on the condition variablecond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call topthread_cond_broadcast() orpthread_cond_signal() in that thread shall behave as if it were issued after the about-to-block thread has blocked.
Upon successful return, the mutex shall have been locked and shall be owned by the calling thread.
When using condition variables there is always a Boolean predicate involving shared variables associated with each condition wait that is true if the thread should proceed. Spurious wakeups from thepthread_cond_timedwait() orpthread_cond_wait() functions may occur. Since the return frompthread_cond_timedwait() orpthread_cond_wait() does not imply anything about the value of this predicate, the predicate should be re-evaluated upon such return.
The effect of using more than one mutex for concurrent pthread_cond_timedwait() orpthread_cond_wait() operations on the same condition variable is undefined; that is, a condition variable becomes bound to a unique mutex when a thread waits on the condition variable, and this (dynamic) binding shall end when the wait returns.
A condition wait (whether timed or not) is a cancellation point. When the cancelability enable state of a thread is set to PTHREAD_CANCEL_DEFERRED, a side effect of acting upon a cancellation request while in a condition wait is that the mutex is (in effect) re-acquired before calling the first cancellation cleanup handler. The effect is as if the thread were unblocked, allowed to execute up to the point of returning from the call topthread_cond_timedwait() orpthread_cond_wait(), but at that point notices the cancellation request and instead of returning to the caller ofpthread_cond_timedwait() orpthread_cond_wait(), starts the thread cancellation activities, which includes calling cancellation cleanup handlers.
A thread that has been unblocked because it has been canceled while blocked in a call topthread_cond_timedwait() orpthread_cond_wait() shall not consume any condition signal that may be directed concurrently at the condition variable if there are other threads blocked on the condition variable.
The pthread_cond_timedwait() function shall be equivalent to pthread_cond_wait(), except that an error is returned if the absolute time specified byabstime passes (that is, system time equals or exceedsabstime) before the conditioncond is signaled or broadcasted, or if the absolute time specified byabstime has already been passed at the time of the call.
If the Clock Selection option is supported, the condition variable shall have a clock attribute which specifies the clock that shall be used to measure the time specified by theabstime argument. When such timeouts occur,pthread_cond_timedwait() shall nonetheless release and re-acquire the mutex referenced bymutex. Thepthread_cond_timedwait() function is also a cancellation point.
If a signal is delivered to a thread waiting for a condition variable, upon return from the signal handler the thread resumes waiting for the condition variable as if it was not interrupted, or it shall return zero due to spurious wakeup.
Return Value
Except in the case of [ETIMEDOUT], all these error checks shall act as if they were performed immediately at the beginning of processing for the function and shall cause an error return, in effect, prior to modifying the state of the mutex specified bymutex or the condition variable specified by cond.
Upon successful completion, a value of zero shall be returned; otherwise, an error number shall be returned to indicate the error.
Errors
The pthread_cond_timedwait() function shall fail if:
- ETIMEDOUT
- The time specified by abstime to pthread_cond_timedwait() has passed.
The pthread_cond_timedwait() and pthread_cond_wait() functions may fail if:
- EINVAL
- The value specified by cond, mutex, or abstime is invalid.
- EINVAL
- Different mutexes were supplied for concurrent pthread_cond_timedwait() orpthread_cond_wait() operations on the same condition variable.
- EPERM
- The mutex was not owned by the current thread at the time of the call.
These functions shall not return an error code of [EINTR].
查询源码:
pthread.h
int pthread_cond_broadcast(pthread_cond_t*) __nonnull((1));int pthread_cond_destroy(pthread_cond_t*) __nonnull((1));int pthread_cond_init(pthread_cond_t*, const pthread_condattr_t*) __nonnull((1));int pthread_cond_signal(pthread_cond_t*) __nonnull((1));int pthread_cond_timedwait(pthread_cond_t*, pthread_mutex_t*, const struct timespec*) __nonnull((1, 2, 3));int pthread_cond_wait(pthread_cond_t*, pthread_mutex_t*) __nonnull((1, 2));
int pthread_cond_broadcast(pthread_cond_t* cond) { return __pthread_cond_pulse(cond, INT_MAX);}int pthread_cond_signal(pthread_cond_t* cond) { return __pthread_cond_pulse(cond, 1);}int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) { return __pthread_cond_timedwait(cond, mutex, NULL, COND_GET_CLOCK(cond->value));}int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) { return __pthread_cond_timedwait(cond, mutex, abstime, COND_GET_CLOCK(cond->value));}
__LIBC_HIDDEN__int __pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime, clockid_t clock) { timespec ts; timespec* tsp; if (abstime != NULL) { if (__timespec_from_absolute(&ts, abstime, clock) < 0) {//注释1 return ETIMEDOUT; } tsp = &ts; } else { tsp = NULL; } return __pthread_cond_timedwait_relative(cond, mutex, tsp);}
__LIBC_HIDDEN__int __pthread_cond_timedwait_relative(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) { int old_value = cond->value; pthread_mutex_unlock(mutex); int status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond->value), old_value, reltime); pthread_mutex_lock(mutex); if (status == -ETIMEDOUT) { return ETIMEDOUT; } return 0;}
static inline int __futex_wake_ex(volatile void* ftx, bool shared, int count) { return __futex(ftx, shared ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, count, NULL);}
static inline __always_inline int __futex(volatile void* ftx, int op, int value, const struct timespec* timeout) { // Our generated syscall assembler sets errno, but our callers (pthread functions) don't want to. int saved_errno = errno; int result = syscall(__NR_futex, ftx, op, value, timeout); if (__predict_false(result == -1)) {//注释2 result = -errno; errno = saved_errno; } return result;}
#ifndef _SYS_SYSCALL_H_#define _SYS_SYSCALL_H_#include <errno.h>#include <sys/cdefs.h>#include <sys/types.h>#include <asm/unistd.h>#include <sys/glibc-syscalls.h> /* glibc-compatible SYS_* aliases for our __NR_* names. */__BEGIN_DECLSlong syscall(long number, ...);__END_DECLS#endif
查询linux doc:
NAME top
syscall - indirect system call
SYNOPSIS top
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <unistd.h> #include <sys/syscall.h> /* For SYS_xxx definitions */ long syscall(long number, ...);
DESCRIPTION top
syscall() is a small library function that invokes the system call whose assembly language interface has the specified number with the specified arguments. Employing syscall() is useful, for example, when invoking a system call that has no wrapper function in the C library. syscall() saves CPU registers before making the system call, restores the registers upon return from the system call, and stores any error code returned by the system call in errno(3) if an error occurs. Symbolic constants for system call numbers can be found in the header file <sys/syscall.h>.
RETURN VALUE top
The return value is defined by the system call being invoked. In general, a 0 return value indicates success. A -1 return value indicates an error, and an error code is stored in errno.syscall() 执行一个系统调用,根据指定的参数number和所有系统调用的汇编语言接口来确定调用哪个系统调用。
系统调用所使用的符号常量可以在头文件<sys/syscall.h>里面找到。
#define SYS_futex __NR_futex
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */#define __NR_futex 202#define __NR_sched_setaffinity 203#define __NR_sched_getaffinity 204#define __NR_set_thread_area 205
注释1:__timespec_from_absolute在pthread_internals.cpp定义
// Initialize 'ts' with the difference between 'abstime' and the current time// according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.int __timespec_from_absolute(timespec* ts, const timespec* abstime, clockid_t clock) { clock_gettime(clock, ts); ts->tv_sec = abstime->tv_sec - ts->tv_sec; ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec; if (ts->tv_nsec < 0) { ts->tv_sec--; ts->tv_nsec += 1000000000; } if ((ts->tv_nsec < 0) || (ts->tv_sec < 0)) { return -1; } return 0;}
注释2:__predict_false在cdefs.h定义
#if __GNUC_PREREQ(2, 96)#define __predict_true(exp) __builtin_expect((exp) != 0, 1)#define __predict_false(exp) __builtin_expect((exp) != 0, 0)#else#define __predict_true(exp) (exp)#define __predict_false(exp) (exp)#endif
- pthread_cond_wait执行失败
- pthread_cond_wait()
- pthread_cond_wait
- pthread_cond_wait
- pthread_cond_wait
- pthread_cond_wait()
- pthread_cond_wait()
- pthread_cond_wait
- pthread_cond_wait
- pthread_cond_wait()
- pthread_cond_wait()
- pthread_cond_wait()
- pthread_cond_wait()
- pthread_cond_wait
- pthread_cond_wait()
- pthread_cond_wait
- pthread_cond_wait
- pthread_cond_wait
- centos6.8下安装搭建apache环境是出现 make: *** No targets specified and no makefile found. Stop.的解决方案
- JavaSE基础数组排序
- CSS中奇数选择器与偶数选择器,一句代码怎么实现?
- #include <string.h> 、<cstring>、<string> 的区别
- Java JDK动态代理实现自己的事务管理器
- pthread_cond_wait执行失败
- Linux下后台执行的程序出现stopped的情况
- 初学Qt:链表迭代器
- 删除无序链表中值重复出现的节点
- 网狐荣耀版棋牌游戏输赢控制设置
- 微信清理内置浏览器缓存
- Android Camera对预览进行黑白滤镜处理
- iOS上线提审问题
- MongoDB查询