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

相关推荐

  • 华为云国际站:金融等保三级

    华为云国际站:金融等保三级合规解决方案 一、金融行业安全合规的重要性 在数字化经济快速发展的今天,金融行业作为国民经济的重要支柱,其数据安全与系统稳定性关乎国家经济安全和社会稳定。《网络安全法》《数据安全法》以及《金融行业信息系统安全等级保护基本要求》明确要求金融机构需通过等保三级认证。等保三级作为非银行金融机构的最高认证等级,对云服务商的技术能力与合规体系…

    2025年9月18日
    36200
  • 华为云国际站代理商:cdn回源其它域名

    华为云国际站代理商:CDN回源其它域名 随着互联网技术的不断发展,CDN(内容分发网络)在提高网站加载速度和用户体验方面起到了至关重要的作用。特别是对于全球化运营的企业,使用CDN服务已经成为一种趋势。而对于华为云国际站代理商来说,提供优化的CDN回源服务,能够帮助客户提升跨地域网站访问速度和稳定性,增加用户粘性。本篇文章将围绕华为云CDN回源其它域名的应用…

    2025年3月19日
    45100
  • 华为云国际站代理商:cdn架设视频教程

    华为云国际站代理商:CDN架设视频教程 随着互联网的发展,用户对网站访问速度和稳定性的要求越来越高。尤其是在全球化的背景下,如何提升跨国网站的访问体验,成为了许多企业亟待解决的问题。华为云CDN(内容分发网络)作为一种有效的加速工具,通过将内容分发到离用户更近的节点,能够显著提升用户访问速度,降低延迟,保证网站的高可用性和高稳定性。 本文将为您提供一份关于如…

    2024年11月30日
    38800
  • 佛山华为云代理商:安卓通知栏短信图标去不了

    佛山华为云代理商:安卓通知栏短信图标去不了 背景介绍 安卓手机是目前世界上最流行的手机操作系统之一,而通知栏短信图标的显示问题是很多安卓用户都会遇到的一个问题。有用户反映说,收到短信后通知栏上没有显示短信图标,导致无法及时查看新消息。 问题原因分析 造成安卓通知栏短信图标无法显示的原因可能有多种,比如系统设置问题、应用权限限制等。需要仔细检查各项设置,找出问…

    2024年3月14日
    59900
  • 华为云国际站代理商充值:策划书网站

    以下是符合您要求的HTML格式文章: 华为云国际站代理商充值:策划书网站 随着云计算产业的不断发展,越来越多的企业和个人开始选择云服务来支持其业务的扩展与创新。作为全球领先的信息技术企业,华为云凭借其卓越的技术、强大的生态系统和安全可靠的服务体系,在云计算市场中占据了重要地位。本文将围绕“华为云国际站代理商充值”这一主题展开,通过分析华为云的优势,并介绍如何…

    2025年3月24日
    37500

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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