大家好,我是练习编程时长两年半的个人练习生昆工第一ikun,我们昨天说了进程,但是在进行进程切换时,需要不断刷新cache缓存,比较消耗资源为了减少cache刷新时的资源消耗,所以我们今天分享轻量级进程 -- 线程。


目录

1、线程特点:

2、线程相关接口函数

(1)创建线程

(2)结束线程

(3)等待线程

3、线程间通信

(1)同步

①信号量的初始化 -- sem_init ()

②P操作(申请资源) -- sem_wait()

③V操作(释放资源) -- sem_post()

(2)互斥

①互斥锁的初始化 -- pthread_mutex_init()

②申请锁 -- pthread_mutex_lock(pthread_mutex_t *mutex)

③释放锁 -- pthread_mutex_unlock(pthread_mutex_t *mutex)


1、线程特点:

同一个进程创建的多个线程,共用同一个进程的地址空间,进程创建线程后,我们把原本进程也称为线程,称为主线程。线程称为CPU最小任务调度单位

2、线程相关接口函数

pthread_create -- 创建线程

pthread_exit -- 结束线程

pthread_join -- 等待线程

(1)创建线程

   #include <pthread.h>
​int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);参数:thread:线程对象,一个线程对应一个线程对象attr:线程属性,填NULL表示使用默认属性start_routine:线程处理函数arg:给线程处理函数start_routine传参,如果线程处理函数没有参数,则填NULL;成功返回0,失败返回错误编号在编译跟线程操作相关的程序时,需要链接线程库   线程库的库名 -- pthread                   

(2)结束线程

   #include <pthread.h>
​void pthread_exit(void *retval);参数:retval:线程结束信息,由pthread_join等待接收,如果不想返回信息,则填NULL

(3)等待线程

   #include <pthread.h>
​int pthread_join(pthread_t thread, void **retval);等待线程一般在主线程中调用

3、线程间通信

线程间的通信只需要利用全局变量就可以实现。在一个线程使用全局变量时,有可能其他线程也在访问该数据,那么某一线程使用的数据就可能遭到破坏,通过线程的同步和互斥,能够达到数据保护的效果

(1)同步

 同步:多个线程之间按照事先约定好的顺序有先后地完成某个事件

信号量:是系统中一种资源, 本质是一个非负整数,信号量的值等于资源的个数

操作信号量只能由通过特定函数接口才能访问:

①信号量的初始化 -- sem_init ()

  #include <semaphore.h>
​int sem_init(sem_t *sem, int pshared, unsigned int value);参数:sem:信号量对象pshared:填0表示用于线程间同步value:信号量的初始值返回值:成功返回0,失败返回-1
 

②P操作(申请资源) -- sem_wait()

  
 #include <semaphore.h>
​int sem_wait(sem_t *sem);

if(是否有资源)

{

执行后续代码;

信号量-1;

}

else

{

阻塞等待,直到有资源唤醒为止;

}

③V操作(释放资源) -- sem_post()

  
 #include <semaphore.h>
​int sem_post(sem_t *sem);

信号量+1;

if(有等待资源的程序)

{

将其唤醒;

}

实例:创建两个线程,一个线程从键盘获取数据,另一个线程打印输出

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>void *func(void *);
void *func1(void *);
char str[64];
sem_t sem, sem1;
int main(int argc, char *argv[])
{sem_init(&sem, 0, 0);sem_init(&sem1, 0, 1);pthread_t thread1, thread2;int ret1 = pthread_create(&thread1, NULL, func, NULL);if(ret1 < 0){perror("pthread_create");exit(-1);}int ret2 = pthread_create(&thread2, NULL, func1, NULL);if(ret2 < 0){perror("pthread_create");exit(-1);}pthread_join(thread1, NULL);pthread_join(thread2, NULL);return 0;
} void *func(void *arg)
{while(1){sem_wait(&sem1);printf("请输入数据:\n");fgets(str, 64, stdin);sem_post(&sem);}
}
void *func1(void *arg)
{while(1){sem_wait(&sem);printf("%s", str);sem_post(&sem1);}
}

(2)互斥

当一个线程使用公共数据时,其他线程都不能访问该公共数据

临界资源:多个线程能够共同访问的数据

临界区:涉及到临界资源的代码模块

互斥是使用互斥锁保护临界区

互斥锁的相关操作接口函数:

①互斥锁的初始化 -- pthread_mutex_init()

int pthread_mutex_init( pthread_mutex_t *mutex, pthread_mutexattr_t *attr);参数:mutex:互斥锁对象attr:互斥锁使用,填NULL使用缺省属性返回值:成功返回0,失败返回-1

②申请锁 -- pthread_mutex_lock(pthread_mutex_t *mutex)

参数:

        mutex:互斥锁对象

③释放锁 -- pthread_mutex_unlock(pthread_mutex_t *mutex)

参数:

        mutex:互斥锁对象

实例:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>int a = 0;
int b = 0;
int count = 0;void *func();
pthread_mutex_t mutex;
int main(int argc, char *argv[])
{pthread_mutex_init(&mutex, NULL);pthread_t thread;int ret = pthread_create(&thread, NULL, func, NULL);if(ret < 0){perror("create");exit(-1);}while(1){pthread_mutex_lock(&mutex);a = count;b = count;count++;pthread_mutex_unlock(&mutex);}return 0;
} void *func()
{while(1){pthread_mutex_lock(&mutex);if(a != b){printf("a = %d  b = %d\n", a, b);}pthread_mutex_unlock(&mutex);}}

线程和线程间通信(C语言)相关推荐

  1. c语言线程通信方式,线程间通信及同步方法介绍

    线程间如何通信/同步?此前小编给大家介绍了进程间通信的方法,于是一些伙伴又好奇线程间的通信及同步方法,没关系,下面小编就继续给大家科普下线程间通信及同步的方法. 线程间通信及同步方法介绍: 一.线程间 ...

  2. 线程的创建与线程间通信(C语言)

    摘要:线程是如何创建的,线程之间的通信是如何做到的,线程之间通信需要注意什么,线程的同步与互斥是如何使用临界资源的,今天,又是我们一起努力学习的一天,一起来看看. 什么是线程,昨天我们学习了进程,说到 ...

  3. C语言实现简易网络进程及线程间通信

    1.单进程通信客户端代码 #include<stdio.h> #include<arpa/inet.h> #include<sys/socket.h> #inclu ...

  4. Java基础学习——多线程(线程间通信-生产者消费者代码示例)

    JDK 1.5提供了多线程升级方案 将同步synchronized替换成了显示的Lock操作.可以实现唤醒.冻结指定的线程. Lock接口 Lock 实现提供了比使用 synchronized 方法和 ...

  5. Java 如何线程间通信,面试被问哭。。。

    Java 如何线程间通信,曾经小编面试被问哭的一道题.. 正常情况下,每个子线程完成各自的任务就可以结束了.不过有的时候,我们希望多个线程协同工作来完成某个任务,这时就涉及到了线程间通信了. 本文涉及 ...

  6. android线程间通信的几种方法_Android 技能图谱学习路线

    Java基础 Java Object类方法 HashMap原理,Hash冲突,并发集合,线程安全集合及实现原理 HashMap 和 HashTable 区别 HashCode 作用,如何重载hashC ...

  7. Java多线程:线程间通信之Lock

    Java 5 之后,Java在内置关键字sychronized的基础上又增加了一个新的处理锁的方式,Lock类. 由于在Java线程间通信:volatile与sychronized中,我们已经详细的了 ...

  8. 线程间通信 GET POST

    线程间通信有三种方法:NSThread   GCD  NSOperation    进程:操作系统里面每一个app就是一个进程. 一个进程里面可以包含多个线程,并且我们每一个app里面有且仅有一条主线 ...

  9. 通过管道进行线程间通信

    在Java语言中提供了各种各样的输入/输出流Stream,使我们能够方便地对数据进行操作,其中管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据.一个发送数据到输出管道,另一个 ...

最新文章

  1. 企业信息化中常见决策点应对
  2. MySpaces性能提高的过程(转)
  3. mysql注入式攻击_SQL的注入式攻击方式和避免方法
  4. poj 1935(搜索+回溯)
  5. html 标签内背景图片自适应 div 大小
  6. 阿里云安全肖力:从RSA2019看安全技术发展的十个机遇
  7. CVPR2021-RSTNet:自适应Attention的“看图说话”模型
  8. 梯度下降法参数更新公式的推导
  9. ##API(一)————枚举
  10. 【钢铁缺陷检测算法】数据探索
  11. UltraEdit编辑器+注册机(windows版)
  12. 三维地理信息系统空间的可视化分析
  13. csm redfish usb
  14. C# 创建 Word 并另存为PDF格式
  15. iphone win7无法识别_小编操作win7系统电脑不能识别iphone苹果设备的设置教程
  16. vue 调用虚拟键盘
  17. peoplesoft 调用Java_利用 XML Publisher 创建 PeopleSoft 报表
  18. OpenVINO工具套件高级课程第一课:如何充分使用OpenVINO工具套件?
  19. 西门子200SMART(七)交叉引用
  20. 南信大电脑开机自动连接校园网

热门文章

  1. 开发微信小程序都需要哪些资质?
  2. 项目经理如何才能做好沟通?
  3. 登录注册页面,JS判断用户手机号码是否已经存在,或者格式不正确
  4. UNITY笔技--DOTS/ECS
  5. 配置文档的访问权限 配置LDAP家目录漫游
  6. 点对点传输现状,镭速高速点对点传输解决方案
  7. Knight On the Chessboard
  8. 学生宿舍管理数据库设计(上)
  9. UserAgent和获取手机内安装的所有app列表
  10. 基于matab GUI的图形处理火焰检测系统