华为云国际站代理商充值: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文件服务器的角度出…

    2024年9月19日
    1.1K160
  • 华为云国际站代理商:反射速度 网站

    华为云国际站代理商:反射速度网站 华为云的优势 华为云作为全球领先的云计算服务提供商,拥有着技术和服务方面的诸多优势。其中,反射速度网站是其产品中的一大亮点。 高性能服务器 华为云采用业界领先的服务器硬件设备,保障了其服务器的高性能和稳定性。这样的硬件设备可以有效提高网站的反射速度,使用户访问网站时能够获得更好的体验。 全球节点覆盖 华为云在全球范围内部署了…

    2024年10月15日
    1.6K00
  • 华为云国际站代理商注册:成都企业网站seo技术

    华为云国际站代理商注册:成都企业网站 SEO 技术 华为云的优势 作为全球领先的云计算服务提供商之一,华为云凭借其强大的技术实力和丰富的行业经验,为企业用户提供了一系列优质的云计算解决方案。其中,华为云国际站代理商注册为成都企业网站 SEO 优化带来了诸多优势。 稳定可靠的云计算基础设施 华为云拥有全球领先的云计算基础设施,包括遍布全球的数据中心和丰富的计算…

    2024年5月20日
    51800
  • 华为云国际站代理商充值:访问mysql数据库需要配置什么意思

    华为云国际站代理商充值:访问MySQL数据库的配置指南 华为云作为领先的云服务提供商,在全球范围内拥有强大的云计算资源和技术支持。对于华为云国际站代理商,通过华为云服务器访问和管理MySQL数据库是一个非常重要的操作,确保正确的配置不仅能提升业务效率,还能保障数据安全。本文将详细介绍如何进行MySQL数据库的配置,并阐述华为云在这一过程中的优势。 一、华为云…

    2024年10月29日
    56900
  • 华为云代理商:华为云 宿主机

    华为云代理商:华为云宿主机 赋能企业数字化转型 一、华为云宿主机概述 华为云宿主机(HUAWEI CLOUD Bare Metal Server,简称BMS)是华为云提供的一种高性能计算服务,它基于物理服务器而非虚拟化技术,为用户提供独享的物理资源。相较于传统虚拟化云服务器,华为云宿主机具备更强的计算性能、更低的延迟以及更高的安全隔离性,特别适合对计算性能要…

    2025年11月26日
    19900

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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