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

相关推荐

  • 华为云国际站代理商:ftp上传服务器搭建

    华为云国际站代理商:FTP上传服务器搭建全解析 一、为什么选择华为云搭建FTP服务器? 作为全球领先的云服务提供商,华为云凭借其高性能基础设施、全球化覆盖能力以及企业级安全保障,成为搭建FTP服务器的理想选择。通过华为云国际站代理商服务,用户可快速部署符合业务需求的文件传输解决方案,尤其适用于跨国文件分发、多地域协作等场景。 二、华为云搭建FTP服务器的核心…

    2025年4月19日
    42500
  • 华为云国际站代理商注册:cdn劫持减少运营成本

    华为云国际站代理商注册:CDN劫持减少运营成本 随着互联网的发展,全球范围内的数据流量急剧增长,尤其是对于依赖内容分发网络(CDN)的企业来说,如何保障数据的安全性、提高访问速度、降低运营成本,已经成为了重要的挑战之一。华为云作为全球领先的云计算服务提供商,其国际站代理商注册服务不仅提供了强大的CDN解决方案,还能有效地减少CDN劫持问题,从而降低企业的整体…

    2024年12月6日
    43800
  • 华为云国际站代理商:发送短信的服务器

    华为云国际站代理商:发送短信的服务器解决方案 在当今的数字化时代,短信服务仍然是许多企业与客户沟通的重要方式,尤其是在需要即时通知或确认信息的场景中。对于华为云国际站代理商来说,选择一个稳定、安全且高效的服务器解决方案至关重要。本文将从华为云服务器的优势入手,探讨如何借助华为云的强大基础设施构建稳定的短信发送系统。 1. 华为云服务器的卓越性能 华为云提供的…

    2024年9月13日
    48000
  • 华为云代理商:华为云汇款账号

    华为云代理商与华为云汇款账号:合作优势与流程解析 一、华为云的核心优势 1.1 技术领先的全栈云服务能力 华为云基于30年ICT技术积累,提供覆盖IaaS、PaaS、SaaS的全栈云服务,支持人工智能、大数据、物联网等前沿技术,其自研的鲲鹏芯片和昇腾AI处理器构建了高性能算力底座。 1.2 全球化布局与本地化服务 华为云在全球27个地理区域运营65个可用区,…

    2025年5月4日
    39900
  • 华为云国际站代理商:cdn缓存nginx

    华为云国际站代理商:CDN缓存与Nginx优化技术解析 随着互联网技术的飞速发展,全球用户对于网站访问速度和数据加载效率的需求日益增长。为了提高用户体验,很多企业选择使用内容分发网络(CDN)来加速其网站或应用程序的内容传输。而Nginx作为一种高效的Web服务器和反向代理服务器,其在CDN缓存优化中的作用也越来越重要。本文将从华为云的优势出发,结合华为云的…

    2025年3月20日
    42600

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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