C++线程库

1. 基本概念

1.1 线程(Thread)

线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一个进程可以并发多个线程,每条线程并行执行不同的任务。

1.2 并发与并行

  • 并发(Concurrency):指多个任务在同一时间段内交替执行。
  • 并行(Parallelism):指多个任务在同一时刻同时执行,通常需要多核处理器。

1.3 同步与互斥

  • 同步(Synchronization):协调多个线程的执行顺序,确保共享资源的安全访问。
  • 互斥(Mutex):确保同一时间只有一个线程可以访问共享资源。

2. 常用函数及其用法

-thread函数

2.1构造函数

用于创建一个新线程,并指定要执行的函数及其参数。

cpp 复制代码
#include <iostream>
#include <thread>
using namespace std;
void func(int x, string str) {
    cout << "Thread running with x = " << x << " and str = " << str << endl;
}

int main() {
    int a = 5;
    string s = "Hello";
    thread t(func, a, s); // 创建线程,执行func函数
    t.join(); // 等待线程完成,主线程会阻塞直到子线程结束。
    return 0;
}

detach() 函数

将线程分离,使其在后台运行,主线程不再等待子线程。

cpp 复制代码
t.detach();//一旦线程被分离,就无法再与主线程同步

lock() 和unlock()

锁定互斥量,如果互斥量已被锁定,则阻塞直到解锁。

cpp 复制代码
std::mutex mtx;
mtx.lock();
// 临界区
mtx.unlock();//解锁互斥量。

lock_guard用法

自动管理互斥量的生命周期,在构造时锁定互斥量,在析构时解锁。

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mtx;

void printThread(int id) {
    lock_guard<mutex> lock(mtx); // 自动锁定
    cout << "Thread " << id << " is running." << endl;
    // 自动解锁
}

int main() {
    thread t1(printThread, 1);
    thread t2(printThread, 2);
    
    t1.join();
    t2.join();
    
    return 0;
}

unique_lock用法

提供更灵活的锁管理,可以手动锁定和解锁。

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex mtx;

void func() {
    unique_lock<mutex> lock(mtx);
    // 执行操作
    lock.unlock(); // 手动解锁
    // 执行其他操作
    lock.lock(); // 重新锁定
    // 继续执行
}

int main() {
    thread t(func);
    t.join();
    return 0;
}

wait() 用法

使线程等待,直到被通知。

cpp 复制代码
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
mutex mtx;
condition_variable cv;
bool ready = false;

void worker() {
    unique_lock<mutex> lock(mtx);
    cv.wait(lock, []{ return ready; }); // 等待通知
    cout << "Worker thread is running." << endl;
}

int main() {
    thread t(worker);
    
    {
        lock_guard<std::mutex> lock(mtx);
        ready = true;
    }
    cv.notify_one(); // 唤醒一个等待的线程。同理也有notify_all唤醒所有等待的线程
    
    t.join();
    
    return 0;
}

atomic

提供原子操作,确保对变量的操作是原子的,避免数据竞争。

cpp 复制代码
#include <iostream>
#include <thread>
#include <atomic>
using namespace std;
atomic<int> counter(0);

void increment(int n) {
    for(int i = 0; i < n; ++i) {
        counter++;
    }
}

int main() {
    thread t1(increment, 1000);
    thread t2(increment, 1000);
    
    t1.join();
    t2.join();
    
    cout << "Counter value: " << counter << endl;
    
    return 0;
}

3. 注意事项

3.1 避免数据竞争

确保对共享资源的访问是互斥的,使用互斥量或原子操作来防止数据竞争。

3.2 死锁预防

避免多个线程互相等待对方释放锁的情况。使用锁管理类如std::lock_guardstd::unique_lock可以帮助管理锁的生命周期。

3.3 线程安全

设计线程安全的类和方法,确保在多线程环境下正确处理共享资源。

3.4 性能考虑

过多的线程切换会带来性能开销,合理配置线程数量,避免过度创建线程。

3.5 异常处理

在线程函数中捕获并处理异常,避免未捕获的异常导致程序终止。

相关推荐
swan4162 分钟前
2025GDCPC广东省赛游记(附赛时代码)
c++·算法·acm·icpc·算法竞赛
jmlinux8 分钟前
C 语言开发中常见的开发环境
c语言·开发语言·stm32·单片机
not coder21 分钟前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
百锦再25 分钟前
Python窗体编程技术详解
开发语言·python·qt·tkinter·simple·desk·side
临溟夜空的繁星26 分钟前
C语言——深入理解指针(1)
c语言·开发语言
十五年专注C++开发33 分钟前
CMake指令:string(字符串操作)
linux·开发语言·c++·cmake·跨平台编译
代码老y38 分钟前
Python基础入门:开启编程之旅
开发语言·python
0zxm43 分钟前
202403-02-相似度计算 csp认证
开发语言·数据结构·c++
寒山李白1 小时前
Java 中 Redis 过期策略深度解析(含拓展-redis内存淘汰策略列举)
java·开发语言·redis
草莓熊Lotso1 小时前
【C语言预处理详解(下)】--#和##运算符,命名约定,命令行定义 ,#undef,条件编译,头文件的包含,嵌套文件包含,其他预处理指令
c语言·开发语言·经验分享·笔记·其他