linux高级编程(8)线程

线程清理函数:

设置分离属性函数:

线程控制:

互斥与同步

概念:

互斥===》在多线程中对临界资源的排他性访问。

互斥机制===》互斥锁===》保证临界资源的访问控制。

pthread mutex_t    mutex;

互斥锁类型             互斥锁变量   内核对象

框架:

定义互斥锁==》初始化锁==》加锁==》解锁==》销毁

 

例子:

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

int a = 0;
pthread_mutex_t mutex;
void* th(void* arg)
{
    // pthread_mutex_lock(&mutex);
    int i =5000;
    while(i--)
    {
        pthread_mutex_lock(&mutex);
        int tmp = a;
        printf("a is %d\n",tmp+1);
        a = tmp+1;
        pthread_mutex_unlock(&mutex); 
    }
    // pthread_mutex_unlock(&mutex); 
    return NULL;
}

int main(int argc, char *argv[])
{
    
    pthread_t tid1,tid2;
    pthread_mutex_init(&mutex,NULL);
    pthread_create(&tid1,NULL,th,NULL);
    pthread_create(&tid2,NULL,th,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}

结果:

两个线程,为了让它保证一定的顺序,用这个加锁和解锁的函数,控制数字出现的顺序。

 

练习:十个人去银行办业务,一共三个窗口,走一个进一个走一个进一个

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
pthread_mutex_t mutex;
int WIN = 3;
void* th(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if(WIN>0)
        {
            WIN--;
            pthread_mutex_unlock(&mutex);
            printf("get win\n");
            sleep(rand()%5);

            printf("relese win\n");
            pthread_mutex_lock(&mutex);
            WIN++;
            pthread_mutex_unlock(&mutex);

            break;
        }
        else 
        {
            pthread_mutex_unlock(&mutex);

        }
    }
    return NULL;
}

int main(int argc, char *argv[])
{
    
    pthread_t tid[10];
    int i = 0 ;
    pthread_mutex_init(&mutex,NULL);
    for(i = 0 ;i<10;i++)
    {
        pthread_create(&tid[i],NULL,th,NULL);
    }
    for(i = 0 ;i<10;i++)
    {
        pthread_join(tid[i],NULL);
    }
    pthread_mutex_destroy(&mutex);
    return 0;
}

结果为:

 注意:

线程的同步===》同步

===》有一定先后顺序的对资源的排他性访问。

原因:互斥锁可以控制排他访问但没有次序

linux下的线程同步===》信号量机制===》semaphore.h      posix

sem_open();

信号量的分类:

1.无名信号量==》线程间通信

2.有名信号量==》进程间通信

框架:

信号量的定义===》信号量的初始化==》信号量的PV操作===》信号量的销毁。

semaphore

1.信号量的定义:

sem_t                   sem;

信号量的类型        信号量的变量

2.信号量的初始化:

 

例子:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
sem_t sem_H,sem_W;
void* th1(void* arg)
{
    int i =10;
    while(i--)
    {
        sem_wait(&sem_H);
        printf("hello");
        fflush(stdout);
        sem_post(&sem_W);
    }
    return NULL;
}
void* th2(void* arg)
{
    int i =10;
    while(i--)
    {
        sem_wait(&sem_W);
        printf(",world\n");
        sleep(1);
        sem_post(&sem_H);
    }
    return NULL;
}

int main(int argc, char *argv[])
{

    pthread_t tid1,tid2;
    sem_init(&sem_H,0,1);
    sem_init(&sem_W,0,0);
    pthread_create(&tid1,NULL,th1,NULL);
    pthread_create(&tid2,NULL,th2,NULL);
    pthread_join(tid1,NULL);
    pthread_join(tid2,NULL);

    sem_destroy(&sem_H);
    sem_destroy(&sem_W);
    return 0;
}

结果为:

 

 

  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值