惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cisco Blogs
罗磊的独立博客
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
V
V2EX
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
InfoQ
S
Securelist
K
Kaspersky official blog
博客园 - 司徒正美
爱范儿
爱范儿
Scott Helme
Scott Helme
B
Blog RSS Feed
H
Help Net Security
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
AI
AI
Blog — PlanetScale
Blog — PlanetScale
Webroot Blog
Webroot Blog
P
Proofpoint News Feed
V
Visual Studio Blog
Cyberwarzone
Cyberwarzone
P
Privacy International News Feed
M
MIT News - Artificial intelligence
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
IT之家
IT之家
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
S
Security Affairs
T
Threat Research - Cisco Blogs
S
Security @ Cisco Blogs
The Register - Security
The Register - Security
F
Full Disclosure
A
Arctic Wolf
C
Check Point Blog
Recent Announcements
Recent Announcements
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
T
Tor Project blog
Latest news
Latest news
Schneier on Security
Schneier on Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 邗影

关于音频PA结构腔体 球面渲染 关于linux和shi脚本常用的通配符 关于fread读不满一块和读不满count的返回值 如何去掉merge UBI的覆盖写和擦除 DTSI 多个模块共用同一个 GPIO 引脚冲突 问题汇总 debug比release程序启动的快 关于CPU占用优先级的调整 关于蜂鸣器发声 麦序与声源定位 关于squashfs压缩挂载 软连接生成 linux文件查找 C语言弱函数 数据传输报错port unreachable D3D11绘制三角形 关于延迟测试 v4l2检测 关于数据库的性能 流媒体小记包含一些面试问题 v4l2视频采集 流媒体ZLM配置vscode远程开发 关于SPS中的帧率问题 Linux编译问题 SSl冲突问题 D3D初始化窗口显示 关于线程池
线程退出未定义行为
邗影 · 2025-10-24 · via 博客园 - 邗影
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <pthread.h>

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

void worker_thread() {
    std::unique_lock<std::mutex> lock(mtx);
    
    // 如果在此处被pthread_cancel,会导致问题
    cv.wait(lock, []{ return ready; });
    
    std::cout << "Worker thread completed" << std::endl;
}

int main() {
    std::thread t(worker_thread);
    
    // 模拟一些工作
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    
    // 危险操作:在条件变量等待时取消线程
    pthread_cancel(t.native_handle());
    
    t.join();  // 这里可能会出现问题
    
    return 0;
}

在 C++11 中,当线程在条件变量上等待时被 pthread_cancel 取消,确实会导致未定义行为,通常会使程序调用 std::terminate;最终导致崩溃