首页主机资讯使用pthreads如何进行线程通信

使用pthreads如何进行线程通信

时间2025-09-27 11:51:03发布访客分类主机资讯浏览397
导读:pthreads 是一个用于实现多线程的库,它提供了各种同步原语,如互斥锁(mutex)、条件变量(condition variable)和信号量(semaphore),以帮助在线程之间进行通信和同步 互斥锁(Mutex):互斥锁是一种同...

pthreads 是一个用于实现多线程的库,它提供了各种同步原语,如互斥锁(mutex)、条件变量(condition variable)和信号量(semaphore),以帮助在线程之间进行通信和同步

  1. 互斥锁(Mutex):互斥锁是一种同步原语,用于确保多个线程在访问共享资源时不会发生冲突。当一个线程获得锁时,其他线程必须等待直到锁被释放。
#include <
    pthread.h>
    
#include <
    stdio.h>
    

pthread_mutex_t lock;
    
int shared_data = 0;


void* thread_func(void* arg) {
    
    pthread_mutex_lock(&
    lock);
    
    shared_data++;
    
    printf("Shared data: %d\n", shared_data);
    
    pthread_mutex_unlock(&
    lock);
    
    return NULL;

}


int main() {
    
    pthread_t threads[5];
    
    pthread_mutex_init(&
    lock, NULL);
    

    for (int i = 0;
     i <
     5;
 i++) {
    
        pthread_create(&
    threads[i], NULL, thread_func, NULL);

    }
    

    for (int i = 0;
     i <
     5;
 i++) {
    
        pthread_join(threads[i], NULL);

    }
    

    pthread_mutex_destroy(&
    lock);
    
    return 0;

}
    
  1. 条件变量(Condition Variable):条件变量是一种同步原语,允许线程在特定条件满足时等待或通知其他线程。它们通常与互斥锁一起使用。
#include <
    pthread.h>
    
#include <
    stdio.h>
    

pthread_mutex_t lock;
    
pthread_cond_t cond;
    
int shared_data = 0;


void* producer(void* arg) {
    
    pthread_mutex_lock(&
    lock);
    
    shared_data++;
    
    printf("Produced data: %d\n", shared_data);
    
    pthread_cond_signal(&
    cond);
    
    pthread_mutex_unlock(&
    lock);
    
    return NULL;

}


void* consumer(void* arg) {
    
    pthread_mutex_lock(&
    lock);

    while (shared_data == 0) {
    
        pthread_cond_wait(&
    cond, &
    lock);

    }
    
    shared_data--;
    
    printf("Consumed data: %d\n", shared_data);
    
    pthread_mutex_unlock(&
    lock);
    
    return NULL;

}


int main() {
    
    pthread_t producer_thread, consumer_thread;
    
    pthread_mutex_init(&
    lock, NULL);
    
    pthread_cond_init(&
    cond, NULL);
    

    pthread_create(&
    producer_thread, NULL, producer, NULL);
    
    pthread_create(&
    consumer_thread, NULL, consumer, NULL);
    

    pthread_join(producer_thread, NULL);
    
    pthread_join(consumer_thread, NULL);
    

    pthread_mutex_destroy(&
    lock);
    
    pthread_cond_destroy(&
    cond);
    
    return 0;

}
    
  1. 信号量(Semaphore):信号量是一种计数器,用于限制对共享资源的访问。它可以用于实现多种同步策略,如限制并发线程数、实现资源池等。
#include <
    pthread.h>
    
#include <
    stdio.h>
    
#include <
    semaphore.h>
    

sem_t semaphore;
    
int shared_data = 0;


void* producer(void* arg) {
    
    sem_wait(&
    semaphore);
    
    shared_data++;
    
    printf("Produced data: %d\n", shared_data);
    
    sem_post(&
    semaphore);
    
    return NULL;

}


void* consumer(void* arg) {
    
    sem_wait(&
    semaphore);
    
    shared_data--;
    
    printf("Consumed data: %d\n", shared_data);
    
    sem_post(&
    semaphore);
    
    return NULL;

}


int main() {
    
    pthread_t threads[5];
    
    sem_init(&
    semaphore, 0, 1);
     // Initialize semaphore with value 1

    for (int i = 0;
     i <
     5;
 i++) {
    
        pthread_create(&
    threads[i], NULL, producer, NULL);
    
        pthread_create(&
    threads[i + 5], NULL, consumer, NULL);

    }
    

    for (int i = 0;
     i <
     10;
 i++) {
    
        pthread_join(threads[i], NULL);

    }
    

    sem_destroy(&
    semaphore);
    
    return 0;

}
    

这些示例展示了如何使用 pthreads 库中的互斥锁、条件变量和信号量来实现线程通信。在实际应用中,你可能需要根据具体需求选择合适的同步原语和策略。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 使用pthreads如何进行线程通信
本文地址: https://pptw.com/jishu/709917.html
php pthreads能处理的任务类型 php中rmdir的权限要求是什么

游客 回复需填写必要信息