首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在cppyy中使用未来/异步

如何在cppyy中使用未来/异步
EN

Stack Overflow用户
提问于 2022-08-20 04:18:02
回答 1查看 67关注 0票数 1

我试图通过cppyy (一个C++-python绑定包)使用来自future的C++ STL。

例如,我可以在C++中运行以下代码(该代码来自于这个answer)

代码语言:javascript
复制
#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中,上面的类似实现是

代码语言:javascript
复制
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()

而上面的代码生成

代码语言:javascript
复制
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引起的。有什么解决办法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-20 15:29:54

Clang9 9的JIT不像现代g++实现它那样支持线程本地存储,它将在(正在进行的)升级到Clang13时再次检查,这可能解决了这个问题。

否则,cppyy可以与线程代码混合(例如,上面的示例在MacOS上运行很好,Clang是系统编译器)。只是任何TLS使用都需要放在编译好的库代码中,而JIT有这个限制。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73424050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档