华为云国际站代理商充值: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使用的费用。在本文…

    2025年3月26日
    33400
  • 华为云国际站代理商注册:cdn之前传统的分发网络

    华为云国际站代理商注册:CDN之前传统的分发网络 随着互联网技术的发展,内容分发网络(CDN)逐渐成为了保障网络加速和提升用户体验的重要技术。然而,在CDN出现之前,传统的分发网络已经为全球用户提供了服务,但其局限性和缺陷也逐渐暴露。本文将以华为云国际站代理商注册为背景,探讨CDN技术的发展历程,并对比传统分发网络与CDN的优势,结合华为云服务器产品,分析其…

    2024年11月10日
    38100
  • 华为云国际站代理商注册:cf 域名

    华为云国际站代理商注册涉及到一系列步骤和要求。关于使用 CF 域名注册代理商账户,以下是一个大致的流程: 准备工作: 确保你有一个有效的 CF 域名。 准备好你的个人或公司信息,包括身份证明、联系方式等。 访问华为云国际站: 打开华为云国际站的官方网站。 注册账户: 在华为云国际站官网首页,找到并点击“注册”按钮。 按照提示填写注册信息,使用你的 CF 域名…

    2024年7月21日
    1.7K00
  • 华为云国际站代理商注册:服务器获取json

    华为云国际站代理商注册:获取服务器JSON数据的优势分析 引言 随着全球数字化进程的加速,云计算已经成为企业IT基础设施的重要组成部分。在这个过程中,选择合适的云服务平台至关重要。华为云国际站凭借其强大的技术支持和广泛的服务覆盖,成为了众多企业的首选之一。本文将详细探讨华为云国际站代理商注册过程中,如何获取服务器JSON数据,并分析华为云的优势。 华为云国际…

    2024年8月27日
    49600
  • 华为云代理商:华为云汇款账号

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

    2025年5月6日
    30200

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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