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

相关推荐

  • 太原华为云代理商:案列网站

    华为云是一家领先的云计算服务提供商,为全球客户提供稳定、安全、高效的云计算解决方案。在中国市场中,华为云拥有众多代理商,其中太原华为云代理商便是其中之一。本文将从太原华为云代理商的角度出发,介绍华为云的优势,并结合华为云服务器产品进行详细阐述。 华为云的优势 1. 安全保障 华为云拥有专业的安全工程师团队,实时监控云平台的安全状况,以确保客户数据的安全性。华…

    2024年3月15日
    48000
  • 华为云国际站代理商充值:成都 网站原创

    华为云国际站代理商充值:成都 网站原创 引言 随着云计算技术的迅猛发展,越来越多的企业和开发者选择使用云服务来提升自身的业务能力和技术优势。华为云作为全球领先的云计算服务提供商,其云服务产品涵盖了计算、存储、网络、安全、大数据、人工智能等多个领域,深受国内外用户的青睐。尤其是在华为云国际站(Huawei Cloud Global)上,代理商充值的服务越来越受…

    2025年3月22日
    33400
  • 华为云国际站代理商注册:服务器网站部署

    华为云国际站代理商注册:服务器网站部署 什么是华为云国际站代理商? 华为云国际站代理商是指与华为云合作的企业或个人,通过注册成为代理商后,可以销售华为云的各类云服务,并为客户提供专业的技术支持和解决方案。在互联网的快速发展时代,越来越多的企业开始向云计算转型,选择合适的云服务提供商显得尤为重要。华为云凭借其强大的技术实力和丰富的产品线,成为了诸多企业的首选。…

    2024年10月12日
    36900
  • 华为云代理商:常用linux ftp服务器

    华为云代理商:常用Linux FTP服务器 引言 随着云计算的迅猛发展,企业越来越倾向于将数据和应用程序迁移到云端。作为华为云的代理商,了解和掌握常用的Linux FTP服务器对于帮助客户有效管理和传输数据至关重要。本文将介绍几种常用的Linux FTP服务器,并结合华为云的优势探讨如何选择适合的服务器。 常用的Linux FTP服务器 1. vsftpd …

    2024年8月6日
    43800
  • 金华华为云代理商:安装iis没有ftp服务器

    金华华为云代理商:安装IIS没有FTP服务器 问题描述 在使用Windows Server操作系统安装IIS(Internet Information Services)时,有用户反馈安装完成后却没有找到FTP服务器的功能。这个问题困扰了很多用户,因为FTP服务器对于一些网站运营和文件传输非常重要。 解决方案 首先,要确认安装IIS的版本是否包含FTP服务器…

    2024年3月21日
    51300

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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