How to use future / async in cppyy

Question:

I’m trying to use future from C++ STL via cppyy (a C++-python binding packet).

For example, I could run this following code in C++ (which is adapted from this 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;
}

A similar implementation of the above in Python is

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()

And the above code yields

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'!

It looks like it’s caused by using future in cppyy.
Any solutions to this?

Asked By: Max Wong

||

Answers:

Clang9’s JIT does not support thread local storage the way the modern g++ implements it, will check again when the (on-going) upgrade to Clang13 is finished, which may resolve this issue.

Otherwise, cppyy mixes fine with threaded code (e.g. the above example runs fine on MacOS, with Clang the system compiler). Just that any TLS use needs to sit in compiled library code while the JIT has this limitation.

Answered By: Wim Lavrijsen
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.