【发布时间】:2017-02-05 17:56:51
【问题描述】:
设置:
我正在尝试在我的 32 位汇编语言程序中使用来自 pthreads 的 pthread_mutex_timedlock 函数。代码如下所示:
struct timespec
.tv_sec dd ? ; time in seconds
.tv_nsec dd ? ; time is nano seconds
ends
;....
.time timespec ; the timespec structure
;....
; the code where pthread_mutex_timedlock is used
mov eax, [.timeout] ; the timeout in [ms]
mov ecx, 1000
cdq
div ecx ; the timeout in eax [s]
imul edx, 1000000 ; the remainder in edx [ns]
mov [.time.tv_sec], eax
mov [.time.tv_nsec], edx
lea eax, [.time]
cinvoke pthread_mutex_timedlock, [.ptrMutex], eax
test eax, eax
jnz .error
问题在于pthread_mutex_timedlock 函数只有在立即解锁互斥体时才会锁定它。
如果此时互斥锁被锁定,函数pthread_mutex_timedlock 立即返回ETIMEDOUT 错误,无需等待超时,忽略timespec 结构中设置的值。
问题:
我做错了什么?
【问题讨论】:
-
使用调试器和/或 ltrace 查看它是如何被调用的。另外,请发布minimal reproducible example,以便我们也可以尝试它,而无需花时间让您的 sn-p 可运行。