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

相关推荐

  • 华为云国际站代理商:html编程例子

    华为云国际站代理商:HTML编程实例与华为云优势解析 一、华为云的核心优势 作为全球领先的云服务提供商,华为云凭借以下优势成为企业数字化转型的首选合作伙伴: 全球基础设施布局:覆盖亚太、欧洲、拉美等地区的30+可用区,提供低延迟网络 全栈技术能力:从芯片到云平台的全栈自研技术体系 安全合规认证:通过GDPR、ISO 27001等50+项国际安全认证 弹性扩展…

    2026年1月3日
    23100
  • 华为云国际站代理商充值:cdn市场行业占比

    华为云国际站代理商充值:CDN市场行业占比分析与华为云优势 随着全球互联网的发展与技术进步,云计算行业持续扩张,各种云服务的应用场景不断丰富。特别是在内容分发网络(CDN)市场,企业对高效、安全的网络传输需求日益增加。作为全球领先的ICT(信息与通信技术)解决方案供应商,华为云凭借其强大的技术优势、丰富的产品体系以及广泛的市场布局,正逐步占领CDN市场的主导…

    2024年12月5日
    39300
  • 华为云国际站代理商充值:福州婚庆网站建设哪个公司比较专业

    福州婚庆网站建设:选择专业代理商与华为云的优势 随着互联网的发展,婚庆行业也越来越多地依赖于网站建设来吸引客户与展示服务。对于福州的婚庆公司来说,建设一个专业且具备吸引力的网站至关重要。而在选择网站建设方案时,选择一个可靠的代理商以及云服务提供商显得尤为重要。华为云作为全球领先的云计算服务平台,凭借其强大的技术实力和多元化的服务,成为许多企业网站建设的首选。…

    2024年12月7日
    44100
  • 济宁华为云代理商:arcsin定义域

    济宁华为云代理商:arcsin定义域 1. 什么是arcsin? arcsin是反正弦函数,表示一个数的正弦值等于该数。其中,函数的定义域需要注意。 2. 正弦函数的定义域 正弦函数的定义域是整个实数集,即(-∞, +∞)。 3. 反正弦函数的定义域 反正弦函数的定义域是[-1, 1],即其取值范围是[-π/2, π/2]。 4. 济宁华为云代理商介绍 济宁…

    2024年1月12日
    54000
  • 华为云国际站代理商:junit 测试用例

    华为云国际站代理商:JUnit测试用例的最佳实践与优势解析 引言 在软件开发过程中,单元测试是保证代码质量的关键环节之一。作为华为云国际站的代理商,我们深知华为云在云计算领域的强大实力和丰富资源。本文将结合华为云的优势,探讨如何高效编写和管理JUnit测试用例,帮助开发团队提升测试效率和代码质量。 1. 华为云的优势与JUnit测试的结合 华为云为开发者提供…

    2026年1月4日
    13100

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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