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 异常处理

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

相关推荐
Humbunklung12 分钟前
Rust方法语法:赋予结构体行为的力量
开发语言·后端·rust
萧曵 丶19 分钟前
Rust 内存结构:深入解析
开发语言·后端·rust
算法练习生25 分钟前
Qt核心类QWidget及其派生类详解
开发语言·c++·qt
oioihoii30 分钟前
C++11标准库算法:深入理解std::none_of
java·c++·算法
1024小神41 分钟前
tauri项目在windows上的c盘没有权限写入文件
c语言·开发语言·windows
老虎06271 小时前
数据结构(Java)--位运算
java·开发语言·数据结构
yanjiaweiya1 小时前
云原生-集群管理续
java·开发语言·云原生
Swift社区1 小时前
Swift 解 LeetCode 320:一行单词有多少种缩写可能?用回溯找全解
开发语言·leetcode·swift
写不出来就跑路1 小时前
暑期实习感悟与经验分享:从校园到职场的成长之路
java·开发语言·经验分享·spring boot
No Silver Bullet1 小时前
软件工程功能点估算法常用术语介绍
java·开发语言·软件工程