標籤:
多線程中,在互斥量和 讀寫鎖的 屬性中,都有一個叫 進程共用屬性 。
對於互斥量,查詢和設定這個屬性的方法為:
pthread_mutexattr_getpshared
pthread_mutexattr_setpshared
我一開始不理解什麼是 進程共用屬性。 看了man中的說明如下
The pthread_mutexattr_getpshared() function shall obtain the value of the process-shared attribute from the attributes object referenced by attr. The pthread_mutexattr_setpshared() function shall set the process-shared attribute in an initialized attributes object referenced by attr.
The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be operated upon by any thread that has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes. If the process-shared attribute is PTHREAD_PROCESS_PRIVATE, the mutex shall only be operated upon by threads created within the same process as the thread that initialized the mutex; if threads of differing processes attempt to operate on such a mutex, the behavior is undefined. The default value of the attribute shall be PTHREAD_PROCESS_PRIVATE.
意思是:
這個屬性有兩個值
PTHREAD_PROCESS_SHARED
PTHREAD_PROCESS_PRIVATE, 顧名思義,是進程共用,和進程不共用。 也即是 當你的進程初始化一個互斥量後, 是只有自己的進程 可以使用,還是 其他進程也可以使用這個互斥量。
預設情況是 PTHREAD_PROCESS_PRIVATE, 只有本進程可以 對這個互斥量加鎖等操作。
這是,別的進程,對私人的 互斥量 操作的行為,沒有定義。
Linux線程互斥量--進程共用屬性