日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【转】pthread mutex 基本用法

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转】pthread mutex 基本用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉自:pthread mutex 基本用法 | feng 言 feng 語

鎖是程序中經常需要用到的機制,尤其是多線程的程序中,如果沒有鎖的幫助,線程間的同
步就會非常麻煩甚至不可能。pthread中提供了mutex互斥量這種鎖,在 linux 下經常
用到,以下是pthread_mutex_t的相關函數介紹及簡單用法。

相關函數

1 2 3 4 5 6 7 8 9 10 #include <pthread.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr); int pthread_mutex_destroy(pthread_mutex_t *mutex);int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex);

使用方法

使用mutex的基本步驟就是:

定義muutex?-> 初始化mutex?-> 使用mutex(lock, unlock, trylock) -> 銷毀mutex。

函數名也已經把它自己的功能描述的非常清楚了。只是有一些細節需要注意:

pthread_mutex_t的初始化有兩種方法,一種是使用函數pthread_mutex_init,使用結
束需要調用函數pthread_mutex_destroy進行銷毀,調用時mutex必須未上鎖。

It shall be safe to destroy an initialized mutex that is unlocked. Attempting
to destroy a locked mutex or a mutex that is referenced (for example, while
being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another
thread results in undefined behavior.

– man pthread_thread_destroy

大意是如果mutex是上鎖狀態,或者被pthread_cond_timedwait()或pthread_cond_wait()
函數引用,此時對其調用pthread_mutex_destroy()結果未定義。

第二種方法是使用PTHREAD_MUTEX_INITIALIZER。根據[1]中的描述,似乎對使用這種方法
初始化的mutex調用pthread_mutex_destroy()會產生錯誤,對未上鎖的mutex調用
pthread_mutex_unlock也會產生錯誤。

Mutex initialization using the PTHREAD_MUTEX_INITIALIZER does not immediately
initialize the mutex. Instead, on first use, pthread_mutex_lock() or
pthread_mutex_trylock() branches into a slow path and causes the
initialization of the mutex. Because a mutex is not just a simple memory
object and requires that some resources be allocated by the system, an attempt
to call pthread_mutex_destroy() or pthread_mutex_unlock() on a mutex that has
statically initialized using PTHREAD_MUTEX_INITIALER and was not yet locked
causes an EINVAL error.

但并沒有找到太多佐證,也不知道這個系統與 Linux 上的實現是否相同。

例子程序

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <stdio.h> #include <pthread.h>/* pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; */ pthread_mutex_t mutex;int count;void * thread_run(void *arg) {int i;pthread_mutex_lock(&mutex);for (i = 0; i < 3; i++) {printf("[%#lx]value of count: %d\n", pthread_self(), ++count);}pthread_mutex_unlock(&mutex);return 0; }int main(int argc, char *argv[]) {pthread_t thread1, thread2;pthread_mutex_init(&mutex, 0);pthread_create(&thread1, NULL, thread_run, 0);pthread_create(&thread2, NULL, thread_run, 0);pthread_join(thread1, 0);pthread_join(thread2, 0);pthread_mutex_destroy(&mutex);return 0; }

如果使用了第 4 行的初始化方法,可以刪除 23 和 28 行。

Reference

[1]?pthread_mutex_destroy()–Destroy Mutex - IBM Knowledge Center

總結

以上是生活随笔為你收集整理的【转】pthread mutex 基本用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。