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

相关推荐

  • 华为云代理商:存储和CDN有什么关系

    华为云代理商:存储和CDN有什么关系 在现代互联网环境中,数据传输、存储和内容分发网络(CDN)是保证应用快速、稳定运行的核心要素。作为全球领先的云服务提供商,华为云以其强大的技术积累和创新能力,为企业客户提供了一整套完善的云服务解决方案。存储和CDN作为华为云服务中的重要组成部分,它们之间具有密切的联系和协同作用。本文将深入探讨华为云存储与CDN之间的关系…

    2024年12月4日
    42300
  • 菏泽华为云代理商:asp sql汽车租赁网站

    菏泽华为云代理商:ASP SQL汽车租赁网站 华为云的优势 华为云作为全球领先的云计算服务提供商,拥有雄厚的技术实力和优质的服务保障,为企业和个人用户提供了高效、安全、稳定的云计算解决方案。 华为云的优势主要体现在以下几个方面: 强大的安全防护能力:华为云拥有国际一流的安全技术团队,为用户数据提供强大的防护和安全保障。 高性能的云服务器:华为云服务器采用最新…

    2024年3月14日
    52300
  • 华为云代理商:cdn设置备用dns

    华为云代理商:CDN设置备用DNS 随着互联网技术的迅速发展,全球用户对于内容传输的需求愈发增加。特别是在大规模数据传输和高并发访问场景下,传统的网络架构已经逐渐无法满足企业对网络性能、稳定性和用户体验的要求。作为全球领先的云计算服务提供商,华为云以其强大的基础设施、丰富的产品体系和深厚的技术积累,提供了多种创新型解决方案,其中包括CDN(内容分发网络)服务…

    2024年11月29日
    44500
  • 华为云代理商:服务器配置参数1000并发

    华为云代理商提供的服务器配置参数,特别是针对能够支持1000并发的服务器,主要是针对高性能和可扩展性的需要进行设计。一般来说,当我们谈论到服务器的并发数时,我们是指这台服务器可以同时处理的任务数量,例如Web请求、数据库查询等。对于1000并发需求的服务器,一些关键的配置参数可能包括: CPU(处理器):服务器的处理能力通常和CPU的性能强弱直接相关。高并发…

    2024年4月24日
    55600
  • 华为云国际站代理商充值:crt服务器连接配置

    华为云国际站代理商充值:CRT服务器连接配置 在如今的云计算时代,越来越多的企业选择将其业务迁移到云平台,以实现高效的计算、存储和网络管理。华为云作为全球领先的云服务提供商之一,凭借其卓越的技术、全球化的布局以及强大的硬件设施,吸引了大量企业客户。作为华为云的代理商,代理商需要熟悉并掌握华为云服务器的配置与管理,尤其是CRT(Certificate)服务器的…

    2024年12月12日
    44800

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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