[英]pthread_mutex_timedlock use from Linux assembly language
我試圖在我的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()
所使用的時鍾clock_gettime()
來獲取當前時間。毫秒,然后將結果傳遞給pthread_mutex_timedlock()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.