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

相关推荐

  • 滁州华为云代理商:app 服务器一般怎么配置文件

    滁州华为云代理商:app 服务器一般怎么配置文件 华为云的优势 稳定性:华为云提供可靠的服务器架构,具备高可用性和容灾能力。 弹性伸缩:根据业务需求,可以快速调整服务器配置,实现弹性伸缩。 安全性:华为云具备多层次的安全防护机制,确保应用程序和数据的安全。 高性能:华为云服务器采用先进的硬件设备和网络架构,提供卓越的性能。 灵活性:华为云支持各种操作系统和开…

    2024年4月2日
    31900
  • 华为云国际站代理商:服务配额

    华为云国际站代理商:服务配额 华为云的优势 华为云作为领先的云计算服务提供商,拥有众多优势,其中之一就是服务配额的灵活性和可定制性。 灵活性 华为云提供了多种不同规格的服务器实例,用户可以根据自己的需求选择适合的配置,无需为不需要的资源付费。 可定制性 华为云还支持用户根据自己的业务需求,定制特定的服务配额,包括网络带宽、存储空间、备份频率等,确保用户能够获…

    2024年5月23日
    33800
  • 华为云国际站代理商:cdn加速是什么意思

    华为云CDN加速:优化网络体验的利器 引言 随着全球互联网用户数量的快速增长,用户对网络速度和服务质量的要求也越来越高。作为华为云的国际站代理商,CDN加速技术成为了优化网络体验的重要工具。本文将深入探讨CDN加速的概念、华为云在此领域的优势,以及其对用户和业务的重要意义。 什么是CDN加速? CDN(内容分发网络)是一种通过在全球多个节点部署服务器,将用户…

    2024年8月13日
    37500
  • 华为云代理商:cdn强制刷新没改变

    华为云代理商:CDN强制刷新没改变 在现代互联网的快速发展中,内容分发网络(CDN)扮演着至关重要的角色。它不仅加速了网站和应用的访问速度,还提高了内容的可靠性和稳定性。作为华为云代理商,许多企业和开发者在使用华为云的CDN服务时,可能会遇到“CDN强制刷新没改变”的问题。本文将探讨该问题的原因,分析华为云CDN的优势,并提供解决方案,帮助用户更好地理解和解…

    2025年3月27日
    22200
  • 华为云代理商:CKEditor编辑器

    华为云代理商:CKEditor编辑器 引言 随着互联网技术的不断发展,网站建设已成为企业宣传推广和服务提供的重要手段。而CKEditor作为一款轻量级并且功能强大的富文本编辑器,能够满足网站建设过程中对于内容编辑、格式排版等方面的需求,成为众多网站开发者和内容编辑人员的首选编辑器。本文将介绍CKEditor编辑器在华为云代理商平台上的优势及华为云服务器产品的…

    2024年6月11日
    37300

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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