C语言中互斥锁、信号量和条件变量的所有相关函数、参数、返回值和说明

1. 互斥锁(Mutex)的使用

互斥锁用于保护共享数据,防止多个线程同时修改它。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

// 定义一个全局共享变量
int shared_counter = 0;
// 定义一个互斥锁
pthread_mutex_t mutex;

void* increment_counter(void* arg) {
    for (int i = 0; i < 100000; ++i) {
        // 锁定互斥锁
        pthread_mutex_lock(&mutex);
        
        // 修改共享变量
        shared_counter++;
        
        // 解锁互斥锁
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}

int main() {
    // 初始化互斥锁
    if (pthread_mutex_init(&mutex, NULL) != 0) {
        printf("Mutex init failed\n");
        return 1;
    }

    pthread_t thread1, thread2;

    // 创建两个线程同时运行
    pthread_create(&thread1, NULL, increment_counter, NULL);
    pthread_create(&thread2, NULL, increment_counter, NULL);

    // 等待两个线程完成
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    // 打印计数结果
    printf("Final counter value: %d\n", shared_counter);

    // 销毁互斥锁
    pthread_mutex_destroy(&mutex);
    return 0;
}

2. 信号量(Semaphore)的使用

信号量通过计数器控制资源访问,允许多个线程访问有限的资源。

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define NUM_RESOURCES 2

// 定义信号量
sem_t semaphore;

void* access_resource(void* arg) {
    int thread_num = *(int*)arg;

    // 等待信号量
    sem_wait(&semaphore);
    
    // 模拟资源访问
    printf("Thread %d accessing resource\n", thread_num);
    sleep(1);  // 模拟工作
    printf("Thread %d finished\n", thread_num);

    // 增加信号量, 释放资源
    sem_post(&semaphore);
    return NULL;
}

int main() {
    // 初始化信号量
    sem_init(&semaphore, 0, NUM_RESOURCES);

    pthread_t threads[5];

    for (int i = 0; i < 5; ++i) {
        int* thread_num = malloc(sizeof(int));
        *thread_num = i + 1;        
        pthread_create(&threads[i], NULL, access_resource, thread_num);
    }

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

    // 销毁信号量
    sem_destroy(&semaphore);
    return 0;
}

3. 条件变量(Condition Variable)的使用

条件变量用于线程间的事件通知,一个线程等待条件变化时释放锁,直到其他线程发出信号。

#include <pthread.h>
#include <stdio.h>

pthread_mutex_t mutex;
pthread_cond_t cond_var;
int ready = 0;  // 状态标记

void* wait_for_event(void* arg) {
    pthread_mutex_lock(&mutex);

    // 等待 ready 被设置为1
    while (!ready) {
        printf("Thread waiting\n");
        pthread_cond_wait(&cond_var, &mutex);
    }

    printf("Thread proceeded after condition met\n");
    pthread_mutex_unlock(&mutex);
    return NULL;
}

void* signal_event(void* arg) {
    pthread_mutex_lock(&mutex);
    ready = 1;  // 改变条件
    pthread_cond_signal(&cond_var);  // 发送信号

    printf("Thread signaled\n");
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main() {
    pthread_t thread1, thread2;

    // 初始化互斥锁和条件变量
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond_var, NULL);

    pthread_create(&thread1, NULL, wait_for_event, NULL);
    sleep(1);  // 确保第一个线程先运行
    pthread_create(&thread2, NULL, signal_event, NULL);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    // 销毁互斥锁和条件变量
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond_var);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值