关于C++ 多线程和并发编程问题的案例

1. 竞态条件(Race Condition)

问题描述:多个线程同时访问并修改共享资源,导致不可预测的结果。

#include <iostream>
#include <thread>

int counter = 0;

void increment() {
    for (int i = 0; i < 1000000; ++i) {
        counter++; // 竞态条件:多个线程同时修改 counter
    }
}

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

解决方法:使用互斥锁(std::mutex)保护共享资源。

#include <iostream>
#include <thread>
#include <mutex>

int counter = 0;
std::mutex mtx;

void increment() {
    for (int i = 0; i < 1000000; ++i) {
        std::unique_lock<std::mutex> lock(mtx); // 使用互斥锁保护共享资源
        counter++;
    }
}

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

2. 死锁(Deadlock)

问题描述:两个或多个线程互相等待对方释放资源,导致程序无法继续执行。

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx1, mtx2;

void thread1() {
    std::unique_lock<std::mutex> lock1(mtx1);
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::unique_lock<std::mutex> lock2(mtx2); // 线程1等待mtx2

    std::cout << "Thread 1 acquired both locks" << std::endl;
}

void thread2() {
    std::unique_lock<std::mutex> lock2(mtx2);
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::unique_lock<std::mutex> lock1(mtx1); // 线程2等待mtx1

    std::cout << "Thread 2 acquired both locks" << std::endl;
}

int main() {
    std::thread t1(thread1);
    std::thread t2(thread2);
    t1.join();
    t2.join();
    return 0;
}
解决方法:避免循环等待,按照相同的顺序获取互斥锁。

cpp
Copy code
#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx1, mtx2;

void thread1() {
    std::unique_lock<std::mutex> lock1(mtx1);
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::unique_lock<std::mutex> lock2(mtx2, std::defer_lock); // 延迟加锁

    if (lock2.try_lock()) {
        std::cout << "Thread 1 acquired both locks" << std::endl;
    } else {
        std::cout << "Thread 1 failed to acquire lock2" << std::endl;
    }
}

void thread2() {
    std::unique_lock<std::mutex> lock2(mtx2);
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::unique_lock<std::mutex> lock1(mtx1, std::defer_lock); // 延迟加锁

    if (lock1.try_lock()) {
        std::cout << "Thread 2 acquired both locks" << std::endl;
    } else {
        std::cout << "Thread 2 failed to acquire lock1" << std::endl;
    }
}

int main() {
    std::thread t1(thread1);
    std::thread t2(thread2);
    t1.join();
    t2.join();
    return 0;
}
这些案例涉及了竞态条件和死锁等多线程编程中常见的问题,通过合适的同步机制和锁的使用,可以避免或减轻这些问题的发生。





解决方法:避免循环等待,按照相同的顺序获取互斥锁。

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx1, mtx2;

void thread1() {
    std::unique_lock<std::mutex> lock1(mtx1);
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::unique_lock<std::mutex> lock2(mtx2, std::defer_lock); // 延迟加锁

    if (lock2.try_lock()) {
        std::cout << "Thread 1 acquired both locks" << std::endl;
    } else {
        std::cout << "Thread 1 failed to acquire lock2" << std::endl;
    }
}

void thread2() {
    std::unique_lock<std::mutex> lock2(mtx2);
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::unique_lock<std::mutex> lock1(mtx1, std::defer_lock); // 延迟加锁

    if (lock1.try_lock()) {
        std::cout << "Thread 2 acquired both locks" << std::endl;
    } else {
        std::cout << "Thread 2 failed to acquire lock1" << std::endl;
    }
}

int main() {
    std::thread t1(thread1);
    std::thread t2(thread2);
    t1.join();
    t2.join();
    return 0;
}

这些案例涉及了竞态条件和死锁等多线程编程中常见的问题,通过合适的同步机制和锁的使用,可以避免或减轻这些问题的发生。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

知孤云出岫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值