白骑士的C语言教学高级篇 3.3 并发与多线程

系列目录

上一篇:白骑士的C语言教学高级篇 3.2 高级数据结构

        在现代编程中,并发和多线程是提高程序性能和响应速度的重要手段。并发编程允许程序同时处理多个任务,充分利用多核处理器的优势。本节将介绍并发编程的基础、线程的创建与管理以及线程同步与通信的相关内容。

并发编程基础

        并发编程是指在同一时间段内运行多个任务。并发编程的实现方式有多种,其中多线程编程是最常见的方法之一。多线程可以在一个进程内并发地执行多个线程,每个线程可以独立执行代码,彼此之间共享进程的内存空间。

        并发编程的主要优点:

  1. 提高性能:通过并行执行任务,可以减少程序的运行时间;
  2. 提高响应速度:在用户界面程序中,使用多线程可以避免界面冻结,提升用户体验;
  3. 更好地利用系统资源:多线程可以充分利用多核处理器的优势。

线程的创建与管理

        在C语言中,可以使用POSIX线程(Pthreads)库来创建和管理线程。以下是一个简单的线程创建和管理的例子:

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


void* printMessage(void* arg) {
    char* message = (char*)arg;

    printf("%s\n", message);

    return NULL;
}


int main() {
    pthread_t thread1, thread2;

    const char* message1 = "Thread 1";
    const char* message2 = "Thread 2";

    // 创建线程
    pthread_create(&thread1, NULL, printMessage, (void*)message1);
    pthread_create(&thread2, NULL, printMessage, (void*)message2);

    // 等待线程结束
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    return 0;
}

        在上述代码中,‘pthread_create‘ 函数用于创建新线程,‘pthread_join‘ 函数用于等待线程结束。每个线程执行 ‘printMessage‘ 函数,并打印不同的消息。

线程同步与通信

        线程同步是确保多个线程安全访问共享资源的重要机制。在多线程环境中,如果多个线程同时访问和修改共享数据,可能会导致数据不一致或程序崩溃。为了解决这个问题,可以使用互斥量(Mutex)和条件变量(Condition Variable)等同步机制。

互斥量

        用于防止多个线程同时访问共享资源,例如:

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


pthread_mutex_t mutex;

int counter = 0;


void* incrementCounter(void* arg) {
    for (int i = 0; i < 100000; i++) {
        pthread_mutex_lock(&mutex);

        counter++;

        pthread_mutex_unlock(&mutex);
    }

    return NULL;
}


int main() {
    pthread_t thread1, thread2;

    pthread_mutex_init(&mutex, NULL);

    pthread_create(&thread1, NULL, incrementCounter, NULL);
    pthread_create(&thread2, NULL, incrementCounter, NULL);

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

    pthread_mutex_destroy(&mutex);

    printf("Counter: %d\n", counter);

    return 0;
}

        在上述代码中,‘pthread_mutex_lock‘ 和 ‘pthread_mutex_unlock‘ 函数用于加锁和解锁互斥量,确保同一时刻只有一个线程可以访问和修改 ‘counter‘ 变量。

条件变量

        用于线程之间的通信和同步,例如:

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


pthread_mutex_t mutex;
pthread_cond_t cond;

int ready = 0;


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

    while (!ready) {
        pthread_cond_wait(&cond, &mutex);
    }

    printf("Thread received signal\n");

    pthread_mutex_unlock(&mutex);

    return NULL;
}


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

    ready = 1;

    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);

    return NULL;
}


int main() {
    pthread_t thread1, thread2;

    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);

    pthread_create(&thread1, NULL, waitForSignal, NULL);
    pthread_create(&thread2, NULL, sendSignal, NULL);

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

    pthread_mutex_destroy(&mutex);

    pthread_cond_destroy(&cond);

    return 0;
}

        在上述代码中,‘pthread_cond_wait‘ 函数使线程等待条件变量的信号,‘pthread_cond_signal‘函数发送信号,通知等待的线程继续执行。

总结

        并发与多线程编程是现代程序设计中的重要内容。通过学习并发编程的基础知识、线程的创建与管理以及线程同步与通信机制,将能够编写高效的多线程程序,充分利用多核处理器的优势,提高程序的性能和响应速度。在实际编程中,合理使用多线程和同步机制,可以有效地解决复杂的并发问题,提升程序的可靠性和稳定性。

下一篇:白骑士的C语言教学高级篇 3.4 C语言中的算法​​​​​​​

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白骑士所长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值