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

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

相关推荐
夏婵语冰1 小时前
实用R语言机器学习指南:从数据预处理到模型实战(附配套学习资源)
开发语言·学习·r语言
牛角上的男孩4 小时前
apt update Ign and 404 Not Found
开发语言·数据库
yzzzzzzzzzzzzzzzzz5 小时前
JavaScript 操作 DOM
开发语言·javascript·ecmascript
海绵宝宝汉堡包6 小时前
c# 项目 文件夹
开发语言·c#
时间之里6 小时前
c++:MFC中sqlite3的使用(附实际案例)
c++·mfc·sqlite3
小白要加油努力6 小时前
C++设计模式--策略模式与观察者模式
开发语言·c++·设计模式
小马学嵌入式~7 小时前
数据结构:队列 二叉树
c语言·开发语言·数据结构·算法
Slaughter信仰8 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第二章知识点问答(21题)
java·开发语言·jvm
John_ToDebug8 小时前
Chrome 内置扩展 vs WebUI:浏览器内核开发中的选择与实践
前端·c++·chrome
焊锡与代码齐飞8 小时前
嵌入式第三十五课!!Linux下的网络编程
linux·运维·服务器·开发语言·网络·学习·算法