gpt4 book ai didi

c - pthread_mutex_timedlock 使用来自 Linux 汇编语言

转载 作者:太空狗 更新时间:2023-10-29 11:41:51 26 4
gpt4 key购买 nike

设置:

我正在尝试在我的 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中设置的值 结构。

问题:

我做错了什么?

最佳答案

pthread_mutex_timedlock() 的超时是绝对 超时,而不是相对超时 - 它会立即返回,因为您的超时值表示的绝对时间早已过去。

如果您想要“从现在起 N 毫秒后超时”(相对超时),您需要使用 clock_gettime() 获取当前时间(指定 CLOCK_REALTIME时钟,因为这是 pthread_mutex_timedlock()) 使用的时钟,然后将其偏移 N 毫秒并将结果传递给 pthread_mutex_timedlock()

关于c - pthread_mutex_timedlock 使用来自 Linux 汇编语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42055363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com