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

相关推荐

  • 华为云国际站代理商:服务器在线监控

    华为云国际站代理商:服务器在线监控 引言 随着数字化转型的不断推进,企业对IT基础设施的依赖日益加深。华为云作为全球领先的云计算服务提供商,凭借其强大的技术实力和丰富的产品线,成为了众多企业的首选。本文将探讨华为云在服务器在线监控方面的优势,帮助企业更好地管理和维护其云环境。 华为云的技术优势 华为云凭借其强大的技术背景和研发能力,提供了一系列先进的云服务,…

    2024年11月3日
    33900
  • 华为云代理商:佛山外贸网站建设机构

    华为云代理商:佛山外贸网站建设机构 引言 随着全球经济的快速发展,外贸行业在近些年得到了迅速增长。在这个背景下,越来越多的企业开始重视线上业务的发展,而网站建设则成为了外贸企业迈向全球市场的重要一步。而作为一家专业的外贸网站建设机构,佛山地区的华为云代理商为企业提供了强有力的技术支持和解决方案,让企业在激烈的竞争中脱颖而出。 华为云的优势 华为云以其卓越的技…

    2024年9月27日
    39800
  • 华为云国际站代理商:成都企业网站的建立

    华为云国际站代理商:成都企业网站的建立 华为云服务器产品优势 华为云作为全球领先的云计算服务提供商,拥有强大的技术实力和丰富的产品线,为企业提供稳定、高效、安全的云服务。 华为云服务器产品特点 华为云服务器产品具有以下特点: 弹性扩展:可以根据业务需求随时调整配置,保证系统的稳定运行。 高性能:采用最新的硬件设备,保证服务器的性能和稳定性。 安全可靠:华为云…

    2024年4月23日
    50000
  • 华为云代理商:centos7配置ntp本地服务器配置

    配置 NTP 服务器非常重要,这是因为它可以确保你的服务器的时间和世界标准时间同步。以下是在 CentOS 7 上配置 NTP 本地服务器的步骤: 在开始之前,你需要确保你的系统是最新的。你可以使用下面的命令来更新你的系统: sudo yum update -y 安装 NTP NTP 软件包在默认的 CentOS 7 仓库中可用。你可以使用 yum 命令来安…

    2024年3月29日
    47100
  • 华为云国际站代理商:机器人电话软件

    华为云国际站代理商:机器人电话软件解决方案 引言 随着人工智能技术的快速发展,机器人电话软件正逐渐成为企业客户服务、市场营销等领域的重要工具。作为华为云国际站代理商,我们深刻理解企业在数字化转型过程中对高效、智能通信解决方案的需求。本文将详细介绍华为云在机器人电话软件领域的优势,并结合华为云服务器产品,为企业提供全方位的技术支持。 华为云机器人电话软件的核心…

    2026年1月7日
    1800

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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