问题描述
我有这个程序,它试图使用 MacOS CoreFoundation
API CFRunLoopRun()
来阻止线程。
#include <iostream>
#include <thread>
#include <CoreFoundation/CoreFoundation.h>
CFRunLoopRef loop = nullptr;
int main() {
std::thread t1([](){char n; std::cin>>n; CFRunLoopStop(loop);});
std::thread t2([](){loop=CFRunLoopGetCurrent(); CFRunLoopRun();});
t1.join();
t2.join();
return 0;
}
我试图理解为什么线程 t2 中的 CFRunLoopRun()
没有阻塞,而当我删除线程 t2 并在主线程中执行相同的命令时,它会阻塞。在 t2 线程中调用 CFRunLoopRun()
的正确方法是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)