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

相关推荐

  • 珠海华为云代理商:安全网关

    珠海华为云代理商:安全网关 在当下科技互联网的时代,数据的安全性与保密性越来越重要。华为云作为业界领先的云服务提供商,在安全性方面始终走在前沿。本文将从珠海华为云代理商的角度,介绍华为云安全网关的优势,并且说明选择华为云服务的理由。 华为云安全网关的主要特点 华为云安全网关是一个设备和应用程序的一体化解决方案,提供了一系列强大的防护措施,可以有效地防止各类网…

    2024年3月15日
    2600
  • 新乡华为云代理商:api审核

    新乡华为云代理商:API审核 华为云的优势 华为云作为全球领先的云服务提供商,拥有强大的技术实力和全面的解决方案,广泛应用于各行各业。其在云计算、人工智能、物联网等领域的技术积累和创新能力备受认可。 API审核的重要性 API是应用程序接口的缩写,是不同软件系统或应用程序之间相互通信的标准。在云计算中,API的设计和使用至关重要,可以帮助用户更高效地管理和操…

    2024年3月23日
    3900
  • 潍坊华为云代理商:api的帮助文档在什么地方

    潍坊华为云代理商:API的帮助文档在什么地方 华为云的优势 华为云作为全球领先的云计算服务提供商,拥有诸多优势: 全球领先的技术创新能力,提供高品质的云服务。 强大的安全性能,保障用户数据的安全。 灵活多样的产品组合,满足不同应用场景的需求。 稳定可靠的服务质量,保障用户业务的稳定运行。 优秀的技术支持团队,为用户提供专业的技术指导和服务。 API的帮助文档…

    2024年4月9日
    3000
  • 衡阳华为云代理商:apache 域名分发

    1. 引言 随着云计算技术的普及和网络应用的广泛推广,Apache域名分发的重要性也越来越被人们所认识。作为一位华为云代理商,我深知Apache域名分发在企业的网络架构中扮演着怎样的重要角色。本文旨在介绍Apache域名分发的基本概念和原理,并结合华为云的优势,深入探讨如何实现高可用、高效率和安全的域名分发。 2. Apache域名分发的概念和原理 Apac…

    2024年3月15日
    1900
  • 华为云代理商:ftp服务器怎么设置不用账户

    在配置FTP服务器时,通常会要求用户提供用户名和密码进行身份验证。但是,如果你希望设置一个无需账户便可访问的FTP服务器,可以通过配置匿名访问来实现。这是在一定程度上降低了安全性,因此应谨慎使用,仅在特定场景下(如文件公开共享)使用匿名FTP访问。以下是如何在常见的FTP服务器软件(如vsftpd)中设置匿名FTP访问的步骤: 在Linux系统中使用vsft…

    华为云 2024年5月16日
    3100

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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