开发实战进阶之多线程/多进程c++

多线程

C++ 中的多线程编程是一项高级特性,用于开发能够并发执行的应用程序。C++11 标准引入了原生的多线程支持,使得在 C++ 中编写多线程代码变得更加简单和安全。以下是关于 C++ 多线程的一些关键概念和常见操作。

1. 创建线程

C++11 提供了 std::thread 类,用于创建和管理线程。你可以通过传递一个可调用对象(如函数指针、lambda 表达式、函数对象等)来启动线程。

#include <iostream>
#include <thread>

void threadFunction() {
    std::cout << "Hello from thread!" << std::endl;
}

int main() {
    std::thread t(threadFunction); // 创建一个线程执行threadFunction
    t.join(); // 等待线程t完成
    return 0;
}
  • std::thread: 用于表示并控制线程的类。
  • join(): 阻塞当前线程,直到线程 t 结束。
  • detach(): 将线程与当前线程分离,允许它独立运行,不能再 join

2. 传递参数

你可以向线程传递参数。C++ 会自动处理参数的复制或移动。

void printNumber(int n) {
    std::cout << "Number: " << n << std::endl;
}

int main() {
    std::thread t(printNumber, 42); // 向线程传递参数42
    t.join();
    return 0;
}

也可以通过引用传递参数,使用 std::ref 包装参数:

void modifyValue(int &n) {
    n *= 2;
}

int main() {
    int value = 10;
    std::thread t(modifyValue, std::ref(value)); // 通过引用传递
    t.join();
    std::cout << "Modified value: " << value << std::endl; // 输出20
    return 0;
}

3. 多线程同步

在多线程环境中,多个线程可能会访问共享资源,导致竞争条件。C++ 提供了多种同步机制来防止数据竞争,如互斥量 (std::mutex) 和条件变量 (std::condition_variable)。

互斥量(Mutex)
#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;

void printThreadID(int id) {
    std::lock_guard<std::mutex> lock(mtx); // 自动管理锁的获取与释放
    std::cout << "Thread ID: " << id << std::endl;
}

int main() {
    std::thread t1(printThreadID, 1);
    std::thread t2(printThreadID, 2);
    
    t1.join();
    t2.join();
    return 0;
}
  • std::mutex: 一个简单的互斥量,用于保护共享资源。
  • std::lock_guard: RAII 风格的锁管理器,自动在作用域结束时释放锁。
条件变量(Condition Variable)
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
bool ready = false;

void printID(int id) {
    std::unique_lock<std::mutex> lock(mtx);
    cv.wait(lock, []{ return ready; }); // 等待 ready 变为 true
    std::cout << "Thread ID: " << id << std::endl;
}

void setReady() {
    std::lock_guard<std::mutex> lock(mtx);
    ready = true;
    cv.notify_all(); // 通知所有等待的线程
}

int main() {
    std::thread t1(printID, 1);
    std::thread t2(printID, 2);

    setReady(); // 设置 ready 并通知线程

    t1.join();
    t2.join();
    return 0;
}
  • std::condition_variable: 用于阻塞线程,直到某个条件成立。
  • wait(): 让线程等待,直到条件变量被通知并且指定条件满足。
  • notify_one()notify_all(): 通知一个或所有等待的线程。

4. 线程池

创建过多的线程可能会导致系统性能下降。为了解决这个问题,可以使用线程池,其中一组线程可以重用来执行多个任务。C++ 标准库没有直接提供线程池,但你可以通过结合线程和任务队列来实现自己的线程池。

5. C++20中的 std::jthread

C++20 引入了 std::jthread,它是 std::thread 的增强版,能够在对象析构时自动 join 线程,避免了资源泄漏。

#include <iostream>
#include <thread>

void threadFunction() {
    std::cout << "Thread running" << std::endl;
}

int main() {
    std::jthread t(threadFunction); // jthread 自动 join
    return 0;
}

pthread开源库

在C++中,尽管C++11及更高版本引入了std::thread类来实现多线程编程,但在某些场合,您可能仍需要使用POSIX线程(pthread)库,尤其是在跨平台开发或者与C语言兼容时。pthread库提供了丰富的多线程操作功能,允许更细粒度的线程管理。以下是如何在C++中使用pthread的一个简要指南。

1. 引入头文件

首先,需要包含pthread库的头文件。

#include <pthread.h>

2. 创建线程

使用pthread_create函数创建新线程。该函数的原型如下:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
  • thread: 用于存储线程ID的指针。
  • attr: 线程属性,一般传递NULL,表示使用默认属性。
  • start_routine: 线程的执行函数。
  • arg: 传递给线程函数的参数。

示例代码

#include <iostream>
#include <pthread.h>

void* threadFunction(void* arg) {
    int* num = static_cast<int*>(arg);
    std::cout << "Hello from thread! Argument: " << *num << std::endl;
    return nullptr;
}

int main() {
    pthread_t thread;
    int arg = 42;
    int result = pthread_create(&thread, nullptr, threadFunction, &arg);

    if (result != 0) {
        std::cerr << "Failed to create thread" << std::endl;
        return 1;
    }

    pthread_join(thread, nullptr);  // 等待线程完成
    return 0;
}

3. 等待线程结束

使用pthread_join函数等待线程执行完毕。这类似于std::thread中的join方法。

int pthread_join(pthread_t thread, void **retval);
  • thread: 要等待的线程ID。
  • retval: 用于接收线程函数返回值的指针(可传递NULL)。

4. 线程同步

pthread库提供了多种同步机制,例如互斥量(mutex)、条件变量(condition variable)等,以防止多个线程同时访问共享数据时产生数据竞争。

互斥量(Mutex)

示例代码

#include <iostream>
#include <pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void* threadFunction(void* arg) {
    pthread_mutex_lock(&mutex);
    std::cout << "Thread " << pthread_self() << " is running" << std::endl;
    pthread_mutex_unlock(&mutex);
    return nullptr;
}

int main() {
    pthread_t thread1, thread2;

    pthread_create(&thread1, nullptr, threadFunction, nullptr);
    pthread_create(&thread2, nullptr, threadFunction, nullptr);

    pthread_join(thread1, nullptr);
    pthread_join(thread2, nullptr);

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

5. 终止线程

pthread_exit可以用于终止当前线程,pthread_cancel用于请求终止另一个线程。

void pthread_exit(void *retval);
int pthread_cancel(pthread_t thread);

6. 编译

在编译时,需要链接pthread库,使用-lpthread选项。

编译命令

g++ -o my_program my_program.cpp -lpthread

总结

使用pthread库进行多线程编程为C和C++开发者提供了强大的控制能力。尽管C++11及更高版本提供了更为现代的std::threadpthread在处理更底层的操作时仍然有其用武之地。通过正确使用线程同步机制,可以有效地避免数据竞争等常见的多线程问题。

  • 15
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值