4

I am using javacpp https://github.com/bytedeco/javacpp for JNI C++ to Java

For example, I have a C++ class using std::future

class cppDemo {
    public:
    Demo();

    int testInt(int number) {
        return number;
    }

    std::future<int> futureInC()
    {
        // future from a promise
        std::promise<int> p;

        std::future<int> f2 = p.get_future();
        std::thread( [](std::promise<int> p){ sleep(2); p.set_value_at_thread_exit(8); },
                     std::move(p) ).detach();

        return f2;
    }
}

I want use CompletableFuture to get the result from C function. I guest some ways to do this but not work

native @StdFuture CompletableFuture<Integer> futureInC(){
    cppDemo.futureInc();
}

or

CompletableFuture<Integer> futureInC(){
    @StdFuture f = @cppDemo.futureInC();
    CompletableFuture<Integer> future = new CompletableFuture<>();
    f.then(int value -> future.complete(value));
    return future;
}

How can I use it from Java? Thanks!

0 Answers0