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

相关推荐

  • 华为云代理商:ftp 服务器 日志

    华为云代理商:FTP 服务器日志的管理与应用 引言 随着信息技术的快速发展,数据交换的需求越来越强烈,FTP(文件传输协议)作为一种常见的文件传输协议,在企业日常运营中发挥着重要作用。FTP 服务器的日志管理,尤其是对于代理商和云服务提供商来说,至关重要。本文将深入探讨华为云在管理 FTP 服务器日志方面的优势,结合华为云的服务器产品,分析其如何帮助企业提升…

    2025年3月24日
    33400
  • 宁波华为云代理商:安装好的虚拟机系统

    宁波华为云代理商:安装好的虚拟机系统 在现今科技迅猛发展的时代,云计算成为了企业日常运营不可或缺的一部分。作为全球领先的ICT解决方案供应商,华为倾力打造的华为云为用户带来高效、可靠的服务,而在这其中,宁波华为云代理商扮演着重要的角色,不仅提供专业的云服务代理,还又负责为客户安装好的虚拟机系统。 华为云的优势 首先,华为云借助华为庞大的全球网络资源,提供了超…

    2024年3月15日
    51400
  • 华为云国际站代理商充值:服务器远程管理工具

    华为云国际站代理商充值:服务器远程管理工具 华为云优势 华为云作为全球领先的云计算服务提供商,拥有强大的技术实力和丰富的经验,为用户提供稳定可靠的云服务。 服务器远程管理工具 服务器远程管理工具是一种能够帮助用户远程管理服务器的工具,包括监控、配置、维护等功能。华为云提供了一系列高效便捷的远程管理工具,帮助用户轻松管理服务器。 华为云国际站代理商充值 作为华…

    2024年4月20日
    46800
  • 华为云国际站充值:借助开源系统快速建实用网站

    华为云国际站充值:借助开源系统快速建实用网站 1. 引言:为什么选择华为云搭建网站? 在数字化时代,拥有一个高效、稳定的网站对企业或个人至关重要。华为云作为全球领先的云服务提供商,凭借其高性能的云服务器、稳定的网络环境以及丰富的生态支持,成为搭建网站的理想选择。对于国际站用户来说,华为云还提供便捷的充值服务,支持多币种支付,满足全球用户的需求。 2. 开源系…

    2025年7月14日
    41900
  • 华为云国际站代理商注册:服务器允许访问

    华为云国际站代理商注册:服务器允许访问 随着云计算技术的快速发展,云服务已经成为全球企业 IT 基础设施的重要组成部分。华为云作为领先的全球云服务提供商之一,其强大的云计算技术和完善的服务体系吸引了越来越多的企业和开发者的关注。本文将详细介绍华为云国际站代理商注册过程中的一些关键概念,尤其是服务器允许访问的相关内容,同时结合华为云服务器产品的优势进行解析,帮…

    2025年3月20日
    32900

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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