华为云国际站代理商充值: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

相关推荐

  • 华为云代理商:分布式数据库教程

    华为云代理商:分布式数据库教程 华为云分布式数据库简介 作为华为云代理商,了解并掌握分布式数据库是非常重要的。华为云的分布式数据库是基于最新的云计算技术和大数据处理技术而研发的,可以满足企业在处理海量数据时的需求。 分布式数据库特点 华为云的分布式数据库具有以下特点: 高可靠性:数据备份和恢复功能完善,保证数据不丢失 高扩展性:支持按需扩展,可以灵活应对业务…

    2024年5月22日
    54800
  • 华为云国际站代理商充值:cdn加速刷新缓存

    华为云国际站代理商充值:CDN加速刷新缓存 引言 在当今数字化时代,企业对于网站性能的需求愈加迫切,尤其是在全球范围内提供服务时,如何确保网站的快速响应与稳定访问成为了关键问题。华为云作为全球领先的云服务提供商,凭借其强大的云计算基础设施与丰富的产品生态,提供了包括内容分发网络(CDN)加速、缓存刷新等技术,帮助企业提升网站加载速度,优化用户体验。 本文将详…

    2024年11月30日
    1.9K00
  • 华为云国际站代理商充值:cdn玩游戏怎么样

    华为云国际站代理商充值:CDN玩游戏怎么样 引言 随着互联网技术的不断发展和网络用户需求的增长,全球范围内的网络游戏玩家数量激增。网络游戏的流畅体验对延迟、带宽和稳定性要求非常高,因此,选择一个可靠的云服务平台来支持游戏内容的传输显得尤为重要。华为云作为全球领先的云服务提供商,其强大的内容分发网络(CDN)能力为游戏玩家提供了高效、低延迟的体验,尤其适合需要…

    2025年3月20日
    40900
  • 华为云国际站:简述服务器主机设计与选择

    华为云国际站:服务器主机设计与选择策略指南 一、服务器主机设计的关键要素 在数字化转型时代,服务器主机的科学设计需综合考量以下核心维度: 计算性能架构:CPU核心数/线程数、主频及指令集优化,匹配业务负载类型 内存层级设计:容量配置与带宽优化,保障数据高速存取 存储子系统:SSD/NVMe介质选型,RAID策略与IOPS吞吐平衡 网络拓扑:网卡冗余、带宽分配…

    2025年6月16日
    42600
  • 华为云国际站代理商:胶南网站建设公司

    华为云国际站代理商:胶南网站建设公司的优选合作伙伴 在数字化时代,企业网站已成为品牌展示和业务拓展的重要窗口。作为华为云国际站代理商,胶南网站建设公司致力于为企业提供高效、稳定、安全的网站建设服务,结合华为云的强大技术优势,助力企业实现数字化转型。 华为云的优势 1. 全球领先的云计算技术 华为云凭借多年的技术积累和创新,在全球范围内构建了高效、稳定的云计算…

    2025年12月30日
    21200

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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