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

相关推荐

  • 华为云国际站代理商注册:服务器托管和租用区别

    华为云国际站代理商注册:服务器托管和租用区别 在当今快速发展的信息技术时代,企业对云计算服务的需求日益增加。尤其是对于希望在国际市场上拓展业务的公司,选择合适的云服务器产品显得尤为重要。华为云作为全球领先的云服务提供商,为各类企业提供了强大的云计算解决方案。对于华为云国际站的代理商来说,了解并区分服务器托管与服务器租用的区别是开展业务的关键。 什么是服务器托…

    2024年11月26日
    57100
  • 华为云国际站代理商充值:cdn服务器存放静态图片

    华为云国际站代理商充值:CDN服务器存放静态图片 在互联网日益发展和数字化转型加速的今天,网站加载速度和内容传输的效率成为了用户体验的关键因素。尤其是对于图片、视频等静态资源的加载,往往成为网站访问速度的瓶颈。因此,越来越多的企业和开发者开始寻求更加高效的解决方案,以提高网站的访问速度和整体性能。 华为云作为全球领先的云计算服务提供商,凭借其强大的技术优势和…

    2025年3月20日
    45000
  • 华为云国际站:监控运维工作小结

    华为云国际站:监控运维工作小结 引言 随着云计算技术的快速发展,企业对于云服务的监控和运维需求日益增长。华为云国际站作为全球领先的云服务提供商,凭借其强大的技术实力和丰富的实践经验,为企业提供了高效、稳定的监控运维解决方案。本文将围绕华为云国际站在监控运维方面的优势,从多个维度进行总结和分析。 华为云监控运维的核心优势 1. 全面的监控能力 华为云国际站提供…

    2025年7月6日
    49000
  • 华为云国际站代理商注册:cdn服务商提供什么

    华为云国际站代理商注册:CDN服务商提供什么 在数字化浪潮的推动下,企业对高效、稳定且高性能的内容分发网络(CDN)服务需求不断增加。CDN技术使得内容可以通过分布在全球的节点快速传输,从而大幅提升用户访问速度和体验。作为全球领先的云服务提供商,华为云为企业提供了强大的CDN服务,帮助它们在全球范围内实现快速、稳定的内容分发。本文将围绕华为云国际站代理商的注…

    2024年12月10日
    52300

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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