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

相关推荐

  • 华为云国际站代理商充值:FreeBSD镜像安全

    华为云国际站代理商充值:FreeBSD镜像安全 一、华为云简介及其在国际市场的优势 华为云(Huawei Cloud)是华为公司推出的云计算平台,旨在为全球用户提供领先的云技术和服务。作为全球领先的ICT(信息与通信技术)解决方案提供商,华为云借助华为在电信设备、智能设备以及大数据领域的多年经验,为企业用户提供高效、灵活、安全的云服务。随着华为云的国际化步伐…

    2024年12月3日
    40200
  • 华为云代理商:cdn服务商网站

    华为云代理商:CDN服务商网站的优势与选择 随着互联网应用的日益普及,企业和个人对网站访问速度和用户体验的要求也不断提高。尤其是在全球化和多地域分布的环境下,如何提升网站内容的传输效率,成为了企业面临的重要课题。内容分发网络(CDN,Content Delivery Network)作为一种优化网站访问速度和提高用户体验的技术,逐渐成为各大企业的必备工具。作…

    2024年11月11日
    45800
  • 华为云国际站代理商注册:cdn防御内容分发

    华为云国际站代理商注册:CDN防御内容分发 引言 随着互联网的高速发展和全球数据流量的爆炸性增长,如何有效管理和加速内容的分发已经成为各大企业面临的重要问题。内容分发网络(CDN)作为一种优化互联网内容传输的解决方案,正逐步成为全球互联网基础设施的重要组成部分。华为云作为领先的云计算服务提供商,通过其强大的云计算平台,为企业提供高效、可靠、安全的CDN服务。…

    2024年12月6日
    37300
  • 华为云代理商:服务器 客户端 同步计时

    华为云代理商:服务器客户端同步计时 在如今的数字化时代,时间是各类业务系统和应用程序正常运行的核心要素之一。无论是金融、医疗、电商还是其他领域,精确的时间同步都对系统的稳定性和数据一致性起着至关重要的作用。对于企业来说,时间同步不仅关系到业务操作的准确性,也直接影响到系统的安全性。本文将围绕“华为云代理商:服务器客户端同步计时”的主题,探讨华为云在时间同步方…

    2025年3月20日
    39300
  • 华为云国际站代理商注册:佛山营销网站建设服务

    华为云国际站代理商注册:佛山营销网站建设服务 一、华为云的优势 在当今互联网高速发展的时代,选择一个可靠的云服务平台至关重要。华为云作为行业领先的云服务提供商,具备诸多优势,以下将详细介绍其核心竞争力。 1. 强大的技术支持 华为云依托华为多年的技术积累和研发能力,为用户提供稳定、高效的云服务。其数据中心遍布全球,确保了低延迟和高可用性,满足各类业务需求。 …

    2024年7月23日
    1.1K160

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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