【Linux C编程】(一)POSIX多线程及读写优先算法

pthread库

多线程


  这是Linux C的多线程库,需要添加头文件

#include<pthread.h>

  同时编译时需要链接库

gcc -lpthread xxx.c -o xxx.o

(或者在Cmake中使用target_link_libraries)

pthread_create()线程创建函数
//函数原型
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)
(void *), void *arg);

参数1:指向pthread_t的指针或者地址,pthread_t是pthread库定义的数据类型,用来引用线程
参数2:线程属性(指向线程属性数据类型的指针),通常设置为NULL
参数3:线程函数,需要定义为void *类型,返回指向空的指针.并且在传入时要转换成void * 类型
参数4:传递的参数,直接传入指针。多个参数传入结构指针

pthread_exit()线程退出函数

  写在线程函数中,当遇到该函数便退出。可以选择传递参数,不返回参数就NULL

pthread_exit(NULL);
pthread_join()线程等待函数

  调用这一直等待所指定的线程结束.可以选择接受参数,不接受参数就NULL

pthread_join(writernum[0],NULL);

示例1

//示例:文件名为readfirst.c
/***********<头文件>*****************/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<wait.h>
#include<string.h>
#include<pthread.h>
#include<semaphore.h>
typedef struct thread_args{
    int a;
    int b;
}thread_args,*p_thargs;
/***********<写函数>*****************/
void *writer(p_thargs p1){
    printf("ok");
    printf("the num is\n%d\n%d\n",p1->a,p1->b);
    pthread_exit(NULL);
}
/***********<主程序>*****************/
int *main(){
    //创建传递线程参数的结构指针
    int i=3;
    int j=5;
    p_thargs p1;
    p1=(p_thargs)malloc(sizeof(thread_args));//给结构分配空间,别忘记!
    p1->a=i;
    p1->b=j;
    
    pthread_t writernum[10],readernum[10];
    pthread_create(&writernum[0],NULL,(void *)writer,p1);//创建线程,传递结构数组
    pthread_join(writernum[0],NULL);//线程等待
    return 0;
}

terminal执行

gcc -lpthread readfirst.c -o readfirst.o
./readfirst.o 

结果如下
在这里插入图片描述

  
  
  

信号量Semaphore


#include<semaphore.h>

函数以sem_开头

sem_init()信号量创建及初始化
//函数原型
int sem_init(sem_t *sem, int pshared, unsigned int value);

成功返回0

参数描述
sem信号引用:指向信号量数据类型的指针或者地址
pshared0进程内线程间的同步,1进程间线程的同步
value信号量初始值
sem_wait()信号阻塞P操作
//函数原型
int sem_wait(sem_t *sem);

等到获取一个信号量,若sem>0,则立即 -1 并立即返回;若sem=0,则一直睡眠等到sem>0;
成功返回0

sem_trywait()信号非阻塞P操作
int sem_trywait(sem_t *sem);

P操作,但是不阻塞,如果sem=0,不等待直接返回错误-1;成功则返回0;

sem_post()信号释放V操作
int sem_post(sem_t *sem);

给信号量sem的值 +1 ,以唤醒正在等待该信号量的线程
成功返回0

sem_destroy()信号量删除操作
int sem_destroy(sem_t *sem);

删除信号量,前提时该信号量没用被占用
成功返回0,失败返回-1

  
  
  

互斥量MUTEX


互斥量与信号量的区别:互斥量需要自己使用并释放信号量

pthread_mutex_init()创建互斥量
//函数原型
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t * attr)

可以通过第二个参数设置属性,一般使用NULL

pthread_mutex_lock()互斥上锁
//函数原型
int pthread_mutex_lock(pthread_mutex_t *mutex)

如果互斥锁已经上锁(已经被别的线程占用),则一直等待到被解锁
无等待的上锁表示如下,如果锁已经上锁,则返回错误

//无等待的上锁,函数原型
int pthread_mutex_trylock(pthread_mutex_t *mutex);
pthread_mutex_unlock()互斥解锁
//函数原型
int pthread_mutex_unlock(pthread_mutex_t *mutex)
pthread_mutex_destroy()删除互斥锁

前提时该锁当前没有被占用(即没用被锁住)

//函数原型
int pthread_mutex_destroy(pthread_mutex_t *mutex)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值