华为云国际站代理商充值: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时常遇到的一个难题。本文将从华为云CDN的优势出发,深入探讨如何通过…

    2024年12月12日
    44100
  • 华为云国际站:Java爬虫入门

    华为云国际站:Java爬虫入门指南 一、什么是网络爬虫? 网络爬虫(Web Crawler)是一种自动化程序,能够按照预设规则遍历互联网并抓取目标数据。在当今大数据时代,爬虫技术广泛应用于搜索引擎、舆情监控、价格比对等场景。 二、为什么选择Java开发爬虫? Java凭借其强大的生态系统和跨平台特性,成为开发企业级爬虫的理想选择: 丰富的库支持:Jsoup、…

    2025年8月10日
    40700
  • 华为云国际站代理商:函数工作流应用扩展

    华为云国际站代理商:函数工作流应用扩展的核心优势与实践 一、引言:函数工作流在云原生时代的价值 随着企业数字化转型加速,Serverless架构与函数工作流(FunctionGraph)成为构建敏捷应用的关键技术。作为华为云国际站代理商,深入理解华为云在函数计算领域的独特优势,能够帮助全球客户实现资源高效利用、业务快速迭代和成本精准控制。 二、华为云函数工作…

    2025年5月15日
    37400
  • 华为云国际站代理商注册:复制网站源码

    华为云国际站代理商注册:复制网站源码 引言 随着云计算的快速发展,华为云作为全球领先的云服务商之一,不断致力于为全球用户提供高质量、高性能的云计算解决方案。作为华为云国际站代理商,您将有机会分享到华为云丰富的资源和稳定的技术支持,同时也可以为更多客户提供专业的云计算服务。 复制网站源码 作为华为云国际站代理商,您需要注册并获取华为云国际站的代理商账号。注册完…

    2024年9月29日
    42100
  • 华为云国际站代理商注册:查看闲置端口

    华为云国际站代理商注册:查看闲置端口 华为云的优势 华为云是一家全球领先的云计算服务提供商,致力于为客户提供安全可靠、灵活高效的云计算服务。在构建华为云的过程中,华为充分利用自己在信息通信技术领域的积累和优势,打造了领先的技术平台和解决方案。华为云在全球拥有多个数据中心,可以为用户提供全球范围内的云计算服务。 代理商注册流程 要成为华为云国际站代理商,首先需…

    2024年10月8日
    46200

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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