Linux线程——线程同步
生活随笔
收集整理的這篇文章主要介紹了
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ā)中已經做過分析。我們這里直接進行修改。
運行結果最后輸出都是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 <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线程——线程同步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux线程——线程创建和基本使用(多
- 下一篇: Linux基本信号的使用