目錄
互斥鎖的概念和使用?
線程通信-互斥
?互斥鎖的創(chuàng)建和銷毀
?申請鎖-pthread_mutex_lock
?釋放鎖-pthread_mutex_unlock
讀寫鎖的概念和使用
死鎖的避免
互斥鎖的概念和使用?
線程通信-互斥
臨界資源
- 一次只允許一個任務(wù)(進程、線程)訪問的共享資源
概念:
????????不能同時訪問的資源,比如寫文件,只能由一個線程寫,同時寫會寫亂。
????????比如外設(shè)打印機,打印的時候只能由一個程序使用。
????????外設(shè)基本上都是不能共享的資源。
????????生活中比如衛(wèi)生間,同一時間只能由一個人使用。
臨界區(qū)
- 訪問臨界資源的代碼
互斥機制
- mutex互斥鎖,任務(wù)訪問臨界資源前申請鎖,訪問完后釋放鎖
?互斥鎖的創(chuàng)建和銷毀
兩種方法創(chuàng)建互斥鎖,靜態(tài)方式和動態(tài)方式:?
動態(tài)方式:?
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr);
- 成功時返回0,失敗時返回錯誤碼
- mutex指向要初始化的互斥鎖對象
- attr互斥鎖屬性,NULL表示缺省屬性
- man函數(shù)出現(xiàn)No manual entry for pthread_mutex_xxx解決方法:apt-get install manpages-posix-dev
?靜態(tài)方式:
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
?鎖的銷毀:
int pthread_mutex_destory(pthread_mutex_t *mutex)
在linux中,互斥鎖并不占用任何資源,因此LinuxThreads中的pthread_mutex_destory()除了檢查鎖狀態(tài)以外(鎖定狀態(tài)則返回EBUSY)沒有其他動作。
?申請鎖-pthread_mutex_lock
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
- 成功時返回0,失敗時返回錯誤碼
- mutex指向要初始化的互斥鎖對象
- pthread_mutex_lock如果無法獲得鎖,任務(wù)阻塞
- pthread_mutex_trylock如果無法獲得鎖,返回EBUSY而不是掛起等待
?釋放鎖-pthread_mutex_unlock
#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
- 成功時返回0,失敗時返回錯誤碼
- mutex指向要初始化的互斥鎖對象
?示例代碼:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
FILE *fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //多個文件需要多個鎖
void *func1(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread1\n");
char str[] = "I write func1 line\n";
char c;
int i = 0;
//pthread_mutex_t mutex1;
while (1)
{
pthread_mutex_lock(&mutex);
while(i < strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
pthread_mutex_unlock(&mutex);
i = 0;
usleep(1);
}
pthread_exit("func1 exit");
}
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread2\n");
char str[] = "You read func1 thread\n";
char c;
int i = 0;
//pthread_mutex_t mutex2;
while (1)
{
pthread_mutex_lock(&mutex);
while(i < strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
pthread_mutex_unlock(&mutex);
i = 0;
usleep(1);
}
pthread_exit("func2 exit");
}
int main()
{
pthread_t tid1,tid2;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid1,NULL,func1,NULL);
pthread_create(&tid2,NULL,func2,NULL);
while(1)
{
sleep(1);
}
}
運行結(jié)果:
讀寫鎖的概念和使用
?必要性:提高線程執(zhí)行效率
特性:
- 寫者:寫者使用寫鎖,如果當(dāng)前沒有讀者,也沒有其他寫者,寫者立即獲得寫鎖;否則寫者將等待,直到?jīng)]有讀者和寫者。
- 讀者:讀者使用讀鎖,如果當(dāng)前沒有寫者,讀者立即獲得讀鎖;否則讀者等待,直到?jīng)]有寫者。
?注意:
- 同一時刻只有一個線程可以獲得寫鎖,同一時刻可以有多個線程獲得讀鎖。
- 讀寫鎖出于寫鎖狀態(tài)時,所有試圖對讀寫鎖加鎖的線程,不管是讀者試圖加讀鎖,還是寫者試圖加寫鎖,都會被阻塞。
- 讀寫鎖處于讀鎖狀態(tài)時,有寫者試圖加寫鎖時,之后的其他線程的讀鎖請求會被阻塞,以避免寫者長時間的不寫鎖。
- ?初始化一個讀寫鎖? ? ? ? pthread_rwlock_init
- 讀鎖定讀寫鎖? ? ? ? ? ? ? ? pthread_rwlock_rdlock
- 非阻塞讀鎖定? ? ? ? ? ? ? ? pthread_rwlock_tryrdlock
- 寫鎖定讀寫鎖? ? ? ? ? ? ? ? pthread_rwlock_wrlock
- 非阻塞寫鎖定? ? ? ? ? ? ? ? pthread_rwlock_trywrlock
- 解鎖讀寫鎖? ? ? ? ? ? ? ? ? ? pthread_rwlock_unlock
- 釋放讀寫鎖? ? ? ? ? ? ? ? ? ? pthread_rwlock_destroy
?示例代碼:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
pthread_rwlock_t rwlock;
FILE *fp;
void * read_func(void *arg){
pthread_detach(pthread_self());
printf("read thread\n");
char buf[32]={0};
while(1){
//rewind(fp);
pthread_rwlock_rdlock(&rwlock);
while(fgets(buf,32,fp)!=NULL){
printf("%d,rd=%s\n",(int)arg,buf);
usleep(1000);
}
pthread_rwlock_unlock(&rwlock);
sleep(1);
}
}
void *func2(void *arg){
pthread_detach(pthread_self());
printf("This func2 thread\n");
char str[]="I write func2 line\n";
char c;
int i=0;
while(1){
pthread_rwlock_wrlock(&rwlock);
while(i<strlen(str))
{
c = str[i];
fputc(c,fp);
usleep(1);
i++;
}
pthread_rwlock_unlock(&rwlock);
i=0;
usleep(1);
}
pthread_exit("func2 exit");
}
void *func(void *arg){
pthread_detach(pthread_self());
printf("This is func1 thread\n");
char str[]="You read func1 thread\n";
char c;
int i=0;
while(1){
pthread_rwlock_wrlock(&rwlock);
while(i<strlen(str))
{
c = str[i];
fputc(c,fp);
i++;
usleep(1);
}
pthread_rwlock_unlock(&rwlock);
i=0;
usleep(1);
}
pthread_exit("func1 exit");
}
int main(){
pthread_t tid1,tid2,tid3,tid4;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp==NULL){
perror("fopen");
return 0;
}
pthread_rwlock_init(&rwlock,NULL);
pthread_create(&tid1,NULL,read_func,1);
pthread_create(&tid2,NULL,read_func,2);
pthread_create(&tid3,NULL,func,NULL);
pthread_create(&tid4,NULL,func2,NULL);
while(1){
sleep(1);
}
}
死鎖的避免
- 鎖越少越好,最好使用一把鎖
- 調(diào)整好鎖的順序
- 使鎖進行錯位
示例代碼:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
FILE *fp;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; //多個文件需要多個鎖
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER; //多個文件需要多個鎖
void *func1(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread1\n");
char str[] = "I write func1 line\n";
char c;
int i = 0;
//pthread_mutex_t mutex1;
while (1)
{
pthread_mutex_lock(&mutex2);
printf("%d,I get lock2\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex);
printf("%d,I get 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex2);
sleep(10);
}
pthread_exit("func1 exit");
}
void *func2(void *arg)
{
pthread_detach(pthread_self());
printf("This is child thread2\n");
char str[] = "You read func1 thread\n";
char c;
int i = 0;
//pthread_mutex_t mutex2;
while (1)
{
pthread_mutex_lock(&mutex);
printf("%d,I get lock1\n",(int)arg);
sleep(1);
pthread_mutex_lock(&mutex2);
printf("%d,I get 2 locks\n",(int)arg);
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex);
usleep(10);
}
pthread_exit("func2 exit");
}
int main()
{
pthread_t tid1,tid2;
void *retv;
int i;
fp = fopen("1.txt","a+");
if(fp == NULL)
{
perror("fopen");
return 0;
}
pthread_create(&tid1,NULL,func1,1);
sleep(5);
pthread_create(&tid2,NULL,func2,2);
while(1)
{
sleep(1);
}
}
?運行結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-822424.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-822424.html
到了這里,關(guān)于線程的同步和互斥學(xué)習(xí)筆記的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!