我试图通过cppyy (一个C++-python绑定包)使用来自future的C++ STL。
例如,我可以在C++中运行以下代码(该代码来自于这个answer)
#include <future>
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
using namespace chrono_literals;
int main () {
promise<int> p;
future<int> f = p.get_future();
thread t([&p]() {
this_thread::sleep_for(10s);
p.set_value(2);
});
auto status = f.wait_for(10ms);
if (status == future_status::ready) {
cout << "task is read" << endl;
} else {
cout << "task is running" << endl;
}
t.join();
return 0;
}在Python中,上面的类似实现是
import cppyy
cppyy.cppdef(r'''
#include <future>
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
int test () {
promise<int> p;
future<int> f = p.get_future();
thread t([&p]() {
this_thread::sleep_for(10s);
p.set_value(2);
});
auto status = f.wait_for(10ms);
if (status == future_status::ready) {
cout << "task is read" << endl;
} else {
cout << "task is running" << endl;
}
t.join();
return 0;
}
''')
cppyy.gbl.test()而上面的代码生成
IncrementalExecutor::executeFunction: symbol '__emutls_v._ZSt15__once_callable' unresolved while linking symbol '__cf_4'!
IncrementalExecutor::executeFunction: symbol '__emutls_v._ZSt11__once_call' unresolved while linking symbol '__cf_4'!它看起来是由在cppyy中使用future引起的。有什么解决办法吗?
发布于 2022-08-20 15:29:54
Clang9 9的JIT不像现代g++实现它那样支持线程本地存储,它将在(正在进行的)升级到Clang13时再次检查,这可能解决了这个问题。
否则,cppyy可以与线程代码混合(例如,上面的示例在MacOS上运行很好,Clang是系统编译器)。只是任何TLS使用都需要放在编译好的库代码中,而JIT有这个限制。
https://stackoverflow.com/questions/73424050
复制相似问题