【C语言教程】【常用类库】(十一)线程库 - <pthread.h>

11. 线程库 - <pthread.h>

POSIX线程库(pthread)提供了一种在C语言中进行多线程编程的方法。通过多线程,可以使程序并行处理多项任务,从而提高性能和响应速度。

11.1. 线程创建与管理

多线程编程的重要组成部分是如何有效创建和管理线程。

11.1.1. 线程的创建:pthread_create

pthread_create 用于创建一个新线程,并使其开始运行。

  • 使用函数pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
  • 参数说明
    • pthread_t *thread: 指向创建的线程标识符的指针。
    • const pthread_attr_t *attr: 线程属性,可设为 NULL 以使用默认属性。
    • void *(*start_routine)(void *): 线程运行的函数地址。
    • void *arg: 传递给线程函数的参数。
  • 返回值:成功返回0,失败返回错误码。
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void *thread_function(void *arg) {
    printf("Hello from thread!\n");
    return NULL;
}

int main() {
    pthread_t thread;
    if (pthread_create(&thread, NULL, thread_function, NULL)) {
        fprintf(stderr, "Error creating thread\n");
        return 1;
    }

    pthread_join(thread, NULL);
    return 0;
}
11.1.2. 线程的终止与等待:pthread_exit, pthread_join
  • pthread_exit:线程可以通过调用这个函数自行终止,也可以不显式调用而结束。

  • pthread_join:用于等待一个线程的结束。类似于进程中的 wait,它可以获取线程的退出状态。

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

void *thread_function(void *arg) {
    printf("Thread is running\n");
    pthread_exit(NULL);  // 线程终止
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_function, NULL);

    pthread_join(thread, NULL);  // 主线程等待线程结束
    printf("Thread finished\n");
    return 0;
}
11.2. 线程同步

在多线程环境中,确保线程安全至关重要。可以使用 mutex 和条件变量来实现线程同步。

11.2.1. 互斥锁:pthread_mutex_t

互斥锁(mutex)用于防止多个线程同时访问共享资源。

  • 初始化pthread_mutex_init(&mutex, NULL);
  • 加锁和解锁
    • 加锁:pthread_mutex_lock(&mutex);
    • 解锁:pthread_mutex_unlock(&mutex);
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t mutex;

void *thread_function(void *arg) {
    pthread_mutex_lock(&mutex);
    // 临界区,多个线程不能同时执行此部分
    printf("Thread is running\n");
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main() {
    pthread_mutex_init(&mutex, NULL);
    pthread_t thread;
    pthread_create(&thread, NULL, thread_function, NULL);
    pthread_join(thread, NULL);
    pthread_mutex_destroy(&mutex);
    return 0;
}
11.2.2. 条件变量:pthread_cond_t

条件变量用于线程间的复杂同步机制,使线程能够睡眠并等待某些条件满足。

  • 初始化pthread_cond_init(&cond, NULL);
  • 等待和唤醒
    • 等待:pthread_cond_wait(&cond, &mutex);
    • 唤醒:pthread_cond_signal(&cond);pthread_cond_broadcast(&cond);
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t mutex;
pthread_cond_t cond;

void *thread_function(void *arg) {
    pthread_mutex_lock(&mutex);
    pthread_cond_wait(&cond, &mutex);  // 等待条件变量
    printf("Condition met, thread proceeding\n");
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main() {
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);
    pthread_t thread;
    pthread_create(&thread, NULL, thread_function, NULL);

    // 主线程睡眠一段时间,然后发信号唤醒
    sleep(1);
    pthread_mutex_lock(&mutex);
    pthread_cond_signal(&cond);  // 唤醒等待线程
    pthread_mutex_unlock(&mutex);

    pthread_join(thread, NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    return 0;
}
11.3. 读写锁与屏障

读写锁和屏障是高级线程同步工具。

11.3.1. 读写锁:pthread_rwlock_t

读写锁允许多线程同时进行“读”操作,而“写”操作是独占的。

  • 初始化pthread_rwlock_init(&rwlock, NULL);
  • 加锁和解锁
    • 读加锁:pthread_rwlock_rdlock(&rwlock);
    • 写加锁:pthread_rwlock_wrlock(&rwlock);
    • 解锁:pthread_rwlock_unlock(&rwlock);
11.3.2. 屏障:pthread_barrier_t

屏障用于同步多个线程,使它们在某一点必须全部到达时才继续执行后续操作。

  • 初始化pthread_barrier_init(&barrier, NULL, count);
  • 等待pthread_barrier_wait(&barrier);
#include <pthread.h>
#include <stdio.h>

pthread_barrier_t barrier;

void *thread_function(void *arg) {
    printf("Thread reached barrier\n");
    pthread_barrier_wait(&barrier);  // 等待其他线程到达此屏障
    printf("Thread passed barrier\n");
    return NULL;
}

int main() {
    const int num_threads = 3;
    pthread_barrier_init(&barrier, NULL, num_threads);
    pthread_t threads[num_threads];

    for (int i = 0; i < num_threads; i++)
        pthread_create(&threads[i], NULL, thread_function, NULL);

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

    pthread_barrier_destroy(&barrier);
    return 0;
}

通过使用上述线程库功能,您可以在多线程应用程序中实现高效及复杂的同步机制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值