华为云国际站代理商充值:c语言 线程间通信

C语言中线程间通信的方式有多种,以下是几种常用的方法:

  1. 全局变量和互斥锁(Mutex)
    使用全局变量来存储线程之间共享的数据,并使用互斥锁来确保对共享数据的互斥访问。

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int shared_data = 0;
    pthread_mutex_t lock;
    
    void *thread_func(void *arg) {
        pthread_mutex_lock(&lock);
        shared_data++;
        printf("Thread %d: %dn", *(int *)arg, shared_data);
        pthread_mutex_unlock(&lock);
        return NULL;
    }
    
    int main() {
        pthread_t threads[2];
        int thread_ids[2] = {1, 2};
    
        pthread_mutex_init(&lock, NULL);
    
        for (int i = 0; i < 2; i++) {
            pthread_create(&threads[i], NULL, thread_func, &thread_ids[i]);
        }
    
        for (int i = 0; i < 2; i++) {
            pthread_join(threads[i], NULL);
        }
    
        pthread_mutex_destroy(&lock);
        return 0;
    }
  2. 条件变量(Condition Variable)
    条件变量可以用来让一个线程等待另一个线程发出某个信号,以实现线程间的同步。

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int ready = 0;
    pthread_mutex_t lock;
    pthread_cond_t cond;
    
    void *thread_func(void *arg) {
        pthread_mutex_lock(&lock);
        while (!ready) {
            pthread_cond_wait(&cond, &lock);
        }
        printf("Thread %d is runningn", *(int *)arg);
        pthread_mutex_unlock(&lock);
        return NULL;
    }
    
    int main() {
        pthread_t threads[2];
        int thread_ids[2] = {1, 2};
    
        pthread_mutex_init(&lock, NULL);
        pthread_cond_init(&cond, NULL);
    
        for (int i = 0; i < 2; i++) {
            pthread_create(&threads[i], NULL, thread_func, &thread_ids[i]);
        }
    
        sleep(1); // 模拟一些操作
        pthread_mutex_lock(&lock);
        ready = 1;
        pthread_cond_broadcast(&cond);
        pthread_mutex_unlock(&lock);
    
        for (int i = 0; i < 2; i++) {
            pthread_join(threads[i], NULL);
        }
    
        pthread_mutex_destroy(&lock);
        pthread_cond_destroy(&cond);
        return 0;
    }
  3. 信号量(Semaphore)
    信号量可以用来控制对共享资源的访问。

    #include <pthread.h>
    #include <semaphore.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    sem_t sem;
    
    void *thread_func(void *arg) {
        sem_wait(&sem);
        printf("Thread %d is runningn", *(int *)arg);
        sem_post(&sem);
        return NULL;
    }
    
    int main() {
        pthread_t threads[2];
        int thread_ids[2] = {1, 2};
    
        sem_init(&sem, 0, 1);
    
        for (int i = 0; i < 2; i++) {
            pthread_create(&threads[i], NULL, thread_func, &thread_ids[i]);
        }
    
        for (int i = 0; i < 2; i++) {
            pthread_join(threads[i], NULL);
        }
    
        sem_destroy(&sem);
        return 0;
    }

这三种方法是C语言中常用的线程间通信方式,每种方法都有其适用的场景和优缺点,具体选择哪种方法需要根据具体的应用需求来决定。

发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/192679.html

(0)
luotuoemo的头像luotuoemo
上一篇 2024年7月24日 20:45
下一篇 2024年7月24日 20:53

相关推荐

  • 华为云国际站代理商:centos7如何启动ftp服务器

    使用华为云国际站代理商在CentOS 7上启动FTP服务器的指南 随着云计算的快速发展,越来越多的企业选择将其业务迁移到云端。在此背景下,华为云作为全球领先的云服务提供商,以其强大的技术实力和卓越的服务赢得了众多企业的青睐。本文将详细介绍如何在华为云国际站代理商的帮助下,在CentOS 7上启动FTP服务器。 一、为什么选择华为云? 华为云凭借其卓越的性能、…

    2024年9月1日
    46700
  • 华为云国际站代理商充值:cdn视频加速解决方法

    华为云国际站代理商充值:CDN视频加速解决方法 在当今的互联网环境中,视频内容的需求日益增加,如何确保视频的流畅播放成为各大企业的关键问题。华为云CDN(内容分发网络)服务提供了高效、稳定的解决方案,帮助企业在全球范围内提升视频传输效率,提升用户体验。本文将详细介绍如何通过华为云国际站代理商充值以及使用CDN进行视频加速,帮助您更好地了解华为云CDN的优势与…

    2024年10月31日
    48500
  • 华为云国际站代理商:cdn服务分发

    华为云国际站代理商:CDN服务分发 在当今互联网时代,用户对网站访问速度、内容分发的需求越来越高,尤其是跨国、跨区域的业务,CDN(内容分发网络)服务的应用愈加重要。作为全球领先的云服务提供商,华为云在提供高效、安全、稳定的CDN服务方面,凭借其强大的技术优势,成为众多企业和开发者的首选。本文将从华为云CDN的特点、优势,以及如何通过华为云服务器产品增强CD…

    2024年12月1日
    44500
  • 华为云代理商:cdn智能调度功能

    华为云代理商:CDN智能调度功能分析 引言 随着互联网应用的普及,尤其是流媒体、在线教育、云游戏等业务的快速发展,用户对于网站访问速度和稳定性的要求日益提高。为了满足这些需求,内容分发网络(CDN)技术应运而生,成为了提升互联网服务质量的重要工具。作为全球领先的云计算服务提供商,华为云通过其强大的技术实力,推出了多项创新功能。其中,华为云CDN智能调度功能,…

    2024年12月5日
    43900
  • 华为云代理商:cdn加速请求

    华为云CDN加速请求:提升网站性能的利器 1. 介绍华为云CDN加速 华为云CDN加速是一项优秀的内容分发网络服务,旨在提升网站的访问速度和性能。它通过在全球范围内分布的节点,将网站的静态内容缓存并就近分发给用户,从而降低访问延迟,提高访问速度。 2. CDN加速请求的工作原理 当用户访问一个网站时,请求会被发送到最接近用户的CDN节点。如果该节点缓存了请求…

    2024年5月2日
    49100

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
购买阿里云服务器请访问:https://www.4526.cn/