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

相关推荐

  • 华为云国际站充值:禁止get方法调用

    华为云国际站充值安全升级:全面禁止GET方法调用 一、支付安全的重大变革 在华为云国际站最新安全升级中,充值接口全面禁止使用HTTP GET方法调用。这一变革源于GET方法的固有安全缺陷:所有参数直接暴露在URL中,可能被浏览器历史记录、服务器日志或网络监控截获。尤其对于涉及账户充值这类敏感操作,使用GET方法如同将银行卡密码写在明信片上邮寄,极易造成用户支…

    2025年6月2日
    9800
  • 华为云代理商:cdn加速的域名需要备案

    华为云代理商:CDN加速的域名需要备案 在数字化转型的浪潮中,越来越多的企业选择通过云计算服务来提升其网站和应用的性能和可访问性。华为云作为全球领先的云计算服务提供商,凭借其强大的技术实力和产品优势,已成为众多企业的首选云服务平台。特别是在内容分发网络(CDN)加速服务中,域名备案问题是一个常常被忽视但至关重要的环节。本文将探讨CDN加速的域名需要备案的原因…

    2025年3月19日
    11700
  • 华为云国际站代理商:cdn视频接入

    华为云国际站代理商:CDN视频接入 随着互联网视频内容的快速增长,用户对视频观看体验的要求也越来越高。无论是企业的直播活动、点播视频服务,还是在线教育、娱乐行业的高清视频流媒体传输,都对网络传输的稳定性和速度提出了更高要求。为了满足这些需求,华为云CDN(内容分发网络)技术应运而生。作为华为云国际站的代理商,帮助企业完成CDN视频接入,不仅能优化视频传输效果…

    2025年3月26日
    9800
  • 华为云国际站代理商充值:FreeBSD 虚拟主机版本

    华为云国际站代理商充值:FreeBSD 虚拟主机版本 随着云计算和虚拟化技术的迅速发展,越来越多的企业开始选择通过云服务来部署和管理自己的信息技术资源。华为云作为全球领先的云服务提供商,凭借其先进的技术、全球化布局和优质的服务,赢得了众多用户的信任。在众多的云服务产品中,FreeBSD虚拟主机作为一种高效、稳定、安全的解决方案,受到了许多企业的青睐。本文将围…

    2024年12月9日
    15300
  • 华为云国际站代理商:服务器定期镜像

    华为云国际站代理商:服务器定期镜像的重要性 一、什么是服务器定期镜像 服务器定期镜像是指将服务器上的所有数据、配置、应用程序等信息完整地复制一份,形成镜像文件。这一过程可以帮助用户在各种情况下快速恢复服务器数据,确保业务连续性。 二、华为云服务器产品优势 1. 高效稳定的性能 华为云服务器基于华为自主研发的硬件和软件技术,提供高效的计算能力和稳定的性能,满足…

    2024年7月10日
    21800

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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