栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

线程和线程间通信(C语言)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程和线程间通信(C语言)

        大家好,我是练习编程时长两年半的个人练习生昆工第一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 
​
   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 
​
   void pthread_exit(void *retval);
   
   参数:
    retval:线程结束信息,由pthread_join等待接收,如果不想返回信息,则填NULL

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

3、线程间通信

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

(1)同步

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

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

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

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

②P操作(申请资源) -- sem_wait()
  
 #include 
​
   int sem_wait(sem_t *sem);

if(是否有资源)

{

执行后续代码;

信号量-1;

}

else

{

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

}

③V操作(释放资源) -- sem_post()
  
 #include 
​
   int sem_post(sem_t *sem);

信号量+1;

if(有等待资源的程序)

{

将其唤醒;

}

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

#include 
#include 
#include 
#include 
#include 

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 
#include 
#include 
#include 

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 = %dn", a, b);
        }
        pthread_mutex_unlock(&mutex);

    }

}

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号