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

相关推荐

  • 华为云国际站代理商:华为打款认证

    华为云国际站代理商:华为打款认证全解析 一、华为云国际站代理商体系概述 华为云国际站(HUAWEI CLOUD International)通过全球化代理商网络拓展业务,为海外企业提供本地化云服务支持。成为认证代理商需通过严格的资质审核,其中”华为打款认证”是验证合作伙伴资金往来安全性的核心环节。 该认证要求代理商: 具备合法注册的商…

    2026年1月5日
    18600
  • 华为云国际站代理商充值:cdn域名怎么查询

    华为云国际站代理商充值:CDN域名查询操作指南 随着全球互联网业务的不断扩展,企业对于内容分发网络(CDN)的需求愈加迫切。CDN技术通过在全球范围内部署节点,将用户请求的数据缓存至最近的服务器,从而提高网站和应用的访问速度和稳定性。华为云作为领先的云计算服务提供商,提供了高效、安全的CDN服务,而在使用华为云CDN时,代理商充值和域名查询是非常重要的操作环…

    2024年12月7日
    47100
  • 华为云代理商:cdn视频缓存

    华为云代理商:CDN视频缓存解决方案 引言 随着互联网视频应用的普及,尤其是短视频、直播、高清视频等内容的广泛传播,传统的视频内容传输和分发模式已经无法满足用户对于低延迟、高清画质和稳定服务的需求。作为全球领先的ICT解决方案提供商,华为云凭借其强大的技术积累和领先的云计算服务,推出了多种云服务,特别是在内容分发网络(CDN)领域,华为云CDN视频缓存服务能…

    2024年11月28日
    42800
  • 华为云代理商:cdn智能调度功能

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

    2024年12月5日
    45400
  • 华为云代理商:服务器主机密码忘记了

    华为云代理商:服务器主机密码忘记了的解决方案 在云计算时代,越来越多的企业选择将自己的业务部署到云端,华为云作为全球领先的云服务提供商,凭借其强大的技术实力和服务优势,成为了众多企业的首选。然而,在使用华为云服务的过程中,难免会遇到一些技术性问题,其中,服务器主机密码忘记是一个常见的困扰。本文将详细介绍在遇到华为云服务器主机密码忘记的情况下,如何通过华为云的…

    2025年3月26日
    37300

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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