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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux线程——线程同步

發(fā)布時間:2024/4/17 linux 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux线程——线程同步 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

線程同步指的是當一個線程在對某個臨界資源進行操作時,其他線程都不可以對這個資源進行操作,直到該線程完成操作,其他線程才能操作,也就是協(xié)同步調,讓線程按預定的先后次序進行運行。線程同步的方法有四種:互斥鎖、信號量、條件變量、讀寫鎖。

1.互斥鎖

頭文件及函數聲明:

#include <pthread.h> /* mutex是鎖, attr是鎖的屬性,一般用不上,傳個NULL默認屬性就可以 */ int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);int pthread_mutex_lock(pthread_mutex_t *mutex);int pthread_mutex_unlock(pthread_mutex_t *mutex);int pthread_mutex_destroy(pthread_mutex_t *mutex);

示例代碼
以下代碼在多線程并發(fā)中已經做過分析。我們這里直接進行修改。

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<assert.h> #include<pthread.h>pthread_mutex_t mutex;//定義鎖 int g = 0;void* pthread_fun(void* arg) {for(int i = 0; i < 1000; i++){pthread_mutex_lock(&mutex);//上鎖g++;printf("g = %d\n",g);pthread_mutex_unlock(&mutex);//解鎖}pthread_exit(NULL); }int main() {pthread_mutex_init(&mutex, NULL);//初始化鎖pthread_t id[5];for(int i = 0; i < 5; i++){pthread_create(&id[i],NULL,pthread_fun,NULL);}for(int j = 0; j < 5; j++){char* s = NULL;pthread_join(id[j],(void**)&s);}pthread_mutex_destroy(&mutex);//銷毀鎖exit(0); }

運行結果最后輸出都是5000,不會再出現低于5000的情況。

2 信號量

頭文件及函數聲明:

#include <semaphore.h> /* sem:信號對象。 pshared:指明信號量的類型。不為0時此信號量在進程間共享,為0時只能為當前進程的所有線程共享。 value:指定信號量值的大小。 */ int sem_init(sem_t *sem, int pshared, unsigned int value);int sem_wait(sem_t *sem);//原子操作,P操作,將信號值減1int sem_post(sem_t *sem);//原子操作,V操作,將信號值加1int sem_destroy(sem_t *sem);//銷毀信號量

示例代碼:函數線程完成將用戶輸入的數據存儲到文件中

#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<assert.h> #include<pthread.h> #include<semaphore.h> #include<fcntl.h>sem_t sem1; sem_t sem2; char buff[128] = {0};void* pthread_fun(void* arg) {int fd = open("sem.txt",O_WRONLY|O_CREAT,0664);assert(fd != -1);while(1){sem_wait(&sem2);if(strncmp(buff,"end",3) == 0){break;}write(fd,buff,strlen(buff));memset(buff,0,128);sem_post(&sem1);}sem_destroy(&sem1);sem_destroy(&sem2);pthread_exit(NULL); }int main() {sem_init(&sem1,0,1);sem_init(&sem2,0,0);pthread_t id;int res = pthread_create(&id,NULL,pthread_fun,NULL);assert(res == 0);while(1){printf("please input data:\n");sem_wait(&sem1);fgets(buff,128,stdin);buff[strlen(buff)-1] = '\0';sem_post(&sem2);if(strncmp(buff, "end", 3) == 0){break;}}char* s = NULL;pthread_join(id,(void**)s);exit(0); }

3.條件變量

條件變量提供了一種線程間的通知機制:當某個共享數據達到某個值的時候,喚醒等待這個共享數據的線程。
頭文件及函數聲明:

#include <pthread.h> /* cond: attr: */ int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);int pthread_cond_signal(pthread_cond_t *cond); //喚醒單個線程int pthread_cond_broadcast(pthread_cond_t *cond); //喚醒所有等待的線程int pthread_cond_destroy(pthread_cond_t *cond);

示例代碼:

#include <stdio.h> #include <stdlib.h> #include <assert.h> #include <unistd.h> #include <string.h> #include <pthread.h> #include <semaphore.h>pthread_mutex_t mutex; pthread_cond_t cond; void * fun1( void * arg) {char* s = ( char*)arg;while( 1 ){//阻塞,被喚醒pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);pthread_mutex_unlock(&mutex);printf("fun1 read:%s\n",s);if (strncmp(s,"end",3) == 0 ){break;}} }void * fun2( void * arg) {char* s = ( char*)arg;while( 1 ){//阻塞,被喚醒pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);pthread_mutex_unlock(&mutex);printf("fun2 read:%s\n",s);if ( strncmp(s,"end",3) == 0 ){break;}} }int main() {pthread_t id[2];char buff[128] = {0};pthread_cond_init(&cond,NULL);pthread_mutex_init(&mutex,NULL);pthread_create(&id[0],NULL,fun1,( void*)buff);pthread_create(&id[1],NULL,fun2,( void*)buff);while( 1 ){fgets(buff,128,stdin);if ( strncmp(buff,"end",3) == 0 ){pthread_mutex_lock(&mutex);pthread_cond_broadcast(&cond);pthread_mutex_unlock(&mutex);break;}else{pthread_mutex_lock(&mutex);pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);}}pthread_join(id[0],NULL);pthread_join(id[1],NULL);exit(0); }

4.讀寫鎖

頭文件及函數聲明:

#include <pthread.h> /* rwlock:指向讀寫鎖的指針 attr:讀寫鎖屬性,一般傳入NULL,使用默認屬性 每一個函數執(zhí)行成功就返回0,否則就返回一個錯誤碼 */ int pthread_rwlock_init(pthread_rwlock_t *rwlock, pthread_rwlockattr_t *attr);int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);//都鎖定int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);//寫鎖定int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);//解鎖讀寫鎖int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);//釋放讀寫鎖

總結

以上是生活随笔為你收集整理的Linux线程——线程同步的全部內容,希望文章能夠幫你解決所遇到的問題。

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