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

linux线程互斥与同步

2017-03-17 19:06 113 查看

线程互斥,解决多个线程访问同一资源时的竞态问题:

/*************************************************************************
> File Name: mutex.c
> Author: XXDK
> Email: v.manstein@qq.com
> Created Time: Wed 15 Mar 2017 11:18:10 PM PDT
************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<string.h>

typedef struct message {
char buf[10];
pthread_mutex_t lock;
}mesg_t;

void *print_func(void *args)
{
mesg_t* mesgp = (mesg_t*)args;

while(1) {
pthread_mutex_lock(&mesgp->lock);
printf("%s\n", mesgp->buf);
pthread_mutex_unlock(&mesgp->lock);
}
}

void *revrs_func(void *args)
{
int len;
char temp;

mesg_t* mesgp = (mesg_t*)args;
len = strlen(mesgp->buf);
while(1) {
pthread_mutex_lock(&mesgp->lock); // 临界区
for(int i = 0; i <= len/2; i++) {
temp = mesgp->buf[i];
mesgp->buf[i] = mesgp->buf[len - i - 1];
mesgp->buf[len - i - 1] = temp;
}
printf("reverse ok\n");
pthread_mutex_unlock(&mesgp->lock);
}
}

int main()
{
pthread_t tid[2];
mesg_t mesg = {"ABCDEFGHI", 0};

pthread_mutex_init(&mesg.lock, NULL);

pthread_create(&tid[0], NULL, print_func, &mesg);
pthread_create(&tid[1], NULL, revrs_func, &mesg);

pause();

exit(1);
}


线程同步-条件变量,解决多线程按照需求的顺序协同运行的问题:
/*************************************************************************
> File Name: cond.c
> Author: XXDK
> Email: v.manstein@qq.com
> Created Time: Thu 16 Mar 2017 12:41:42 AM PDT
************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>

volatile int flag = 0;

pthread_mutex_t mlock;
pthread_cond_t cond;

void *hello(void *arg)
{
while(1) {
pthread_mutex_lock(&mlock);
while(flag != 0) {
// 1. 自动释放mutex,线程阻塞条件变量
// 2. 睡眠等待条件满足,由条件广播函数唤醒
pthread_cond_wait(&cond, &mlock);
}
flag = 1;
printf("hello\n");

pthread_mutex_unlock(&mlock);
pthread_cond_broadcast(&cond);
}
}
void *the(void *arg)
{
while(1) {
pthread_mutex_lock(&mlock);
while(flag != 1) {
pthread_cond_wait(&cond, &mlock);
}
flag = 2;
printf("the\n");
pthread_mutex_unlock(&mlock);
pthread_cond_broadcast(&cond);
}
}
void *world(void *arg)
{
while(1) {
pthread_mutex_lock(&mlock);
while(flag != 2) {
pthread_cond_wait(&cond, &mlock);
}
flag = 0;
printf("world\n");
pthread_mutex_unlock(&mlock);
pthread_cond_broadcast(&cond);
}
}

int main()
{
pthread_t tid[3];

pthread_mutex_init(&mlock, NULL);
pthread_cond_init(&cond, NULL);

pthread_create(&tid[0], NULL, hello, NULL);
pthread_create(&tid[1], NULL, the , NULL);
pthread_create(&tid[2], NULL, world, NULL);

pause();

exit(1);
}

线程同步-信号量,解决多线程按照需求的顺序协同运行的问题:
/*************************************************************************
> File Name: sema.c
> Author: XXDK
> Email: v.manstein@qq.com
> Created Time: Thu 16 Mar 2017 08:00:00 PM PDT
************************************************************************/

#include<stdio.h>
#include<semaphore.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>

sem_t sem;
void* first(void*args)
{
sem_wait(&sem); // 等待信号量可用
printf("12345\n");
}

void* second(void*args)
{
printf("67890\n");
sem_post(&sem); // 信号量+1
}
int main()
{
pthread_t tid[2];

sem_init(&sem, 0, 0); // 设定信号量初值为0
pthread_create(&tid[0], NULL, first, NULL);
pthread_create(&tid[1], NULL, second, NULL);

sleep(1);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线程 互斥同步