您的位置:首页 > 运维架构 > Linux

Linux线程同步

2015-09-05 17:48 113 查看
linux提供多种方式处理线程同步问题,最常用的是互斥锁、条件变量和信号量.

互斥锁

通过锁机制实现线程同步,只有获得锁的线程才能执行

初始化锁,在linux下线程的互斥数据类型为
pthread_mutex_t
,在使用前需要进行初始化

静态分配:

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


动态分配:

int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);


加锁,在对共享资源访问时,需要对互斥量加锁,如果互斥量已经被加锁,调用线程会阻塞,直到互斥量被解锁

int pthread_mutex_lock(pthread_mutex *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);


解锁,在完成对共享资源的访问后,需要释放锁

int pthread_mutex_unlock(pthread_mutex_t *mutex);


销毁锁,锁在使用完后,需要销毁以释放资源

int pthread_mutex_destroy(pthread_mutex *mutex);


可以用互斥锁解决主线程和子线程的打印问题

#include <iostream>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
using namespace std;

char* xwl = "xiewenlong";
char* zdg = "zhangdaoguang";

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void* print(void* arg){
pthread_mutex_lock(&mutex);
char* name = (char*)arg;
while(*name != '\0'){
printf("%c",*name);
name++;
Sleep(10);
}
pthread_mutex_unlock(&mutex);
return (void*)0;
}

int main(){
pthread_t t;
if(!pthread_create(&t,NULL,print,xwl)){
printf("%s\n","Create thread success!");
}else{
printf("%s\n","Create thread failed!");
}
print(zdg);
pthread_join(t,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}


该互斥锁等价于java中的lock

条件变量

前面介绍的互斥锁只是实现线程的同步,但多数情况下还需要线程的协作. 通常互斥锁和条件变量同时使用,条件变量包括条件和变量两部分,条件是由互斥量保护的,线程在改变条件状态时要先锁住互斥量. 条件变量主要有两个操作:线程等待条件成立而挂起;线程发出条件成立的信号,唤醒等待在该变量上的其他线程.

初始化条件变量,静态初始化和动态初始化

pthread_cond_t cond = PTHREAD_COND_INITIALIER;
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);


等待条件成立,释放互斥锁,阻塞当前线程直到条件成立

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);


唤醒其他等待线程

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 <iostream>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
using namespace std;

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

struct node{
int num;
struct node* next;
}*head=NULL;

void cleanup_handler(void* arg){
printf("Clean up of thread!\n");
free(arg);
pthread_mutex_unlock(&mtx);
}

void* take(void* arg){
struct node* p = NULL;
pthread_cleanup_push(cleanup_handler,p);
for(int i=0; i<10; i++){
pthread_mutex_lock(&mtx);
while(head == NULL){
pthread_cond_wait(&cond,&mtx);
}
p = head;
head = head->next;
printf("get %d\n",p->num);
free(p);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
}
pthread_cleanup_pop(0);
return (void*)0;
}

void* put(void* arg){
struct node* p = (struct node*)arg;
pthread_cleanup_push(cleanup_handler,p);
for(int i=0; i<10; i++){
pthread_mutex_lock(&mtx);
while(head != NULL){
pthread_cond_wait(&cond,&mtx);
}
p = (struct node*)malloc(sizeof(struct node));
p->num = i;
p->next = NULL;
head = p;
printf("put %d\n",i);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx);
}
pthread_cleanup_pop(0);
return (void *)0;
}

int main(){
pthread_t c, p;
pthread_create(&c,NULL,take,NULL);
pthread_create(&p,NULL,put,NULL);
//pthread_cancel(c);
pthread_join(c,NULL);
pthread_join(p,NULL);
pthread_mutex_destroy(&mtx);
pthread_cond_destroy(&cond);
return 0;
}


条件变量等价于java中wait和notify

信号量

前面介绍的互斥锁和条件变量允许单个线程执行互斥的代码,若想若干线程同时执行,可以使用信号量来保证同步.

信号量初始化

int sem_init (sem_t *sem , int pshared, unsigned int value);


等待信号量,阻塞当前线程,直到信号量改变为非零值,并做减法

int sem_wait(sem_t *sem);


释放信号量,给信号量做加法,并通知其他等待线程

int sem_post(sem_t *sem);


销毁信号量

int sem_destroy(sem_t *sem);


利用信号量实现两个线程轮流执行

#include <iostream>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
using namespace std;

sem_t has, em;

void* take(void* arg){
for(int i=0; i<10; i++){
sem_wait(&has);
printf("take element..\n");
sem_post(&em);
}
return (void*)0;
}

void* put(void* arg){
for(int i=0; i<10; i++){
sem_wait(&em);
printf("put element..\n");
sem_post(&has);
}
return (void*)0;
}

int main(){
sem_init(&has,0,0);
sem_init(&em,0,1);
pthread_t c, p;
pthread_create(&c,NULL,take,NULL);
pthread_create(&p,NULL,put,NULL);

pthread_join(c,NULL);
pthread_join(p,NULL);
sem_destroy(&has);
sem_destroy(&em);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: