华为云国际站代理商充值: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年4月19日
    37300
  • 华为云代理商:cdn设置备用dns

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

    2024年11月29日
    28800
  • 华为云国际站代理商充值:服务器 配置要求

    华为云国际站代理商充值:服务器配置要求 随着云计算技术的飞速发展,越来越多的企业和开发者选择云服务器来支撑自己的业务需求。华为云作为全球领先的云服务提供商,凭借其强大的技术实力和可靠的服务,在全球范围内赢得了大量客户的信赖。本文将详细探讨华为云国际站代理商充值的服务器配置要求,结合华为云的优势,分析如何选择合适的云服务器配置,以满足不同用户的需求。 一、华为…

    2024年11月27日
    30500
  • 华为云国际站:华为云服务学生

    华为云国际站:华为云服务赋能全球学生创新未来 引言:数字化时代的学生新机遇 在全球数字化转型浪潮中,云计算技术已成为学生群体探索科技、实践创新的重要工具。华为云国际站面向全球学生推出的专项服务计划,通过领先的云基础设施、AI开发平台及多样化学习资源,为不同国家/地区的学子提供零门槛接触前沿技术的机会。 一、华为云助力学生发展的核心优势 1.1 全球化基础设施…

    2025年8月19日
    21000
  • 襄阳华为云代理商:安卓应用开发安卓开发

    襄阳华为云代理商:安卓应用开发 华为云的优势 作为襄阳地区的华为云代理商,我们深知华为云在云计算领域的优势。华为云拥有强大的技术团队和丰富的经验,为开发者提供了一整套云计算解决方案,包括存储、计算、网络等各种服务。在安卓应用开发领域,华为云更是提供了丰富的开发工具和资源,帮助开发者快速构建高质量的安卓应用。 安卓应用开发 安卓应用开发是当前移动应用开发领域最…

    2024年3月14日
    38100

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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