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

相关推荐

  • 华为云国际站代理商充值:cdn企业排名

    华为云国际站代理商充值:CDN企业排名与华为云优势 在现代互联网环境中,企业对于数据传输速度和网站访问稳定性有着极高的要求。随着全球数字化进程的加快,云计算服务成为了企业建设和运营的基础设施。作为全球领先的云计算服务提供商,华为云不仅提供强大的计算和存储能力,其全球范围内的内容分发网络(CDN)服务也在市场上占有一席之地。本文将结合华为云CDN在国际站代理商…

    2025年3月26日
    36700
  • 华为云代理商:服务器负载均衡配置

    华为云代理商:服务器负载均衡配置 引言 在当今信息化时代,云计算技术已经成为企业IT架构的重要组成部分。随着云计算的普及和技术的发展,越来越多的企业选择使用云服务来提高计算、存储和网络资源的利用率。作为国内领先的云计算服务商,华为云凭借其强大的技术实力和丰富的产品组合,为各类企业提供了稳定可靠的云计算服务。服务器负载均衡(SLB,Server Load Ba…

    2024年12月16日
    2.4K00
  • 常德华为云代理商:aix系统ftp连接服务器

    常德华为云代理商:aix系统ftp连接服务器 华为云的优势 华为云作为全球领先的云计算服务提供商,具有以下优势: 安全可靠:华为云采用国际标准的数据加密和隔离技术,确保用户数据的安全性。 高性能:华为云拥有强大的计算和存储能力,能够满足不同规模企业的需求。 灵活扩展:华为云提供多种灵活的云服务,用户可以根据自身需求进行扩展和调整。 智能管理:华为云具有智能化…

    2024年3月19日
    56900
  • 华为云国际站代理商充值:服务器 3t

    华为云国际站代理商充值:服务器 3T 随着云计算的快速发展,越来越多的企业和开发者选择将他们的应用和数据迁移到云端。华为云作为全球领先的云服务提供商,凭借其强大的技术实力和全面的云服务生态,已经成为许多企业的首选云平台。对于代理商而言,华为云国际站的充值服务为其提供了更高效、更便捷的管理和服务方式,尤其是针对大型云服务器的应用如3T服务器。在本文中,我们将深…

    2024年11月8日
    45000
  • 华为云国际站充值:js提取中文正则

    华为云国际站充值:JS提取中文正则表达式实践指南 一、华为云国际站充值的核心优势 华为云国际站为全球用户提供高效稳定的云服务,其充值流程支持多语言多币种支付,通过JavaScript正则表达式可快速提取中文关键信息,提升操作效率: 全球化覆盖 – 支持中英文等多语言界面切换 金融级安全 – 采用AES-256加密传输支付数据 智能解析…

    2025年9月29日
    1.5K00

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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