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

相关推荐

  • 华为云国际站代理商充值:楚雄网站制作

    华为云国际站代理商充值:楚雄网站制作 华为云的全球化布局与市场影响 华为云作为全球领先的云服务提供商之一,不仅在国内市场占据重要地位,更在国际市场上展现出强大的竞争力与影响力。其国际站代理商充值服务,为全球企业提供了便捷的云计算解决方案。 楚雄网站制作的专业优势与需求分析 在当今数字化时代,企业网站不仅是展示企业形象的窗口,更是实现业务发展与客户互动的关键平…

    2025年3月22日
    41700
  • 华为云代理商:cdn中dns设计

    华为云代理商:CDN中DNS设计的最佳实践 引言 随着互联网应用的飞速发展,企业在面对全球用户访问时,如何提供快速、稳定的服务成为了不可忽视的问题。内容分发网络(CDN)作为解决这一问题的关键技术,其重要性愈发突出。在CDN中,DNS设计扮演着至关重要的角色,它直接影响到用户请求的响应速度和系统的整体性能。本文将结合华为云的优势,探讨在华为云环境下进行CDN…

    2024年11月22日
    43200
  • 华为云国际站代理商充值:产品条形码如何申请

    以华为云国际站代理商充值:产品条形码如何申请 华为云的优势 华为云作为全球领先的云计算服务提供商之一,拥有强大的技术实力和丰富的经验,为客户提供高性能、可靠稳定的云服务器产品。其优势主要体现在以下几个方面: 全球领先技术:华为云采用业界领先的硬件设备和软件技术,确保用户享有最佳的云计算体验。 丰富的产品线:华为云提供多种类型的云服务器产品,满足不同用户的需求…

    2024年5月21日
    54100
  • 华为云代理商:js 绝对值的函数

    华为云代理商:JavaScript 绝对值的函数实践与华为云优势解析 一、JavaScript中的绝对值函数基础 JavaScript作为前端开发的核心语言之一,其内置的数学计算功能非常强大。其中,绝对值函数是数学运算中最基础但实用的功能之一。通过Math.abs()方法,开发者可以快速获取数值的绝对值: let num = -5.3; console.lo…

    2025年9月18日
    34200
  • 临沂华为云代理商:api身份验证

    临沂华为云代理商:API身份验证 华为云的优势 作为临沂地区的华为云代理商,我们深知华为云在云计算领域的领先地位和优势。华为云致力于为客户提供安全、可靠、高效的云计算服务,不断推动数字化转型,帮助企业实现更高效的运营和创新。 强大的技术支持 华为云拥有丰富的技术积累和强大的研发团队,能够提供全方位的技术支持和解决方案。作为代理商,我们能够为客户提供专业的咨询…

    2024年3月29日
    49100

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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