How to connect to one Matlab shared session with multiple threads from C++?

3 Ansichten (letzte 30 Tage)

I created a Matlab shared session using this command in the Matlab Desktop(R2021a):
matlab.engine.shareEngine("mySession");
Then I want to connect it from multi-play threads from C++:
using namespace std::chrono_literals;

void thread(std::u16string const &sessionName)
{
matlab::engine::connectMATLAB(sessionName);
std::this_thread::sleep_for(10s);
}

int main()
{
std::thread _1(thread, std::u16string(u"mySession"));
std::thread _2(thread, std::u16string(u"mySession"));

_1.join();
_2.join();
}
And I got:
terminate called after throwing an instance of 'matlab::engine::EngineException'
what(): MATLAB session cannot be connected.
How could I do that? It's possible?
My Environment:
OS: Fedora 34
Kernel: Linux 5.15.4
Compiler: GCC 11.2.1
Compiler Additional Flags: -std=c++14 -lpthread
Build System Generator: CMake
The CMake Configuration Used:
cmake_minimum_required(VERSION 3.5)

project(KazaeeTest LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

list(APPEND CMAKE_PREFIX_PATH "~/bin/matlab/R2021a")

find_package(Matlab REQUIRED)
include_directories(${Matlab_INCLUDE_DIRS})
include_directories("${Matlab_ROOT_DIR}/extern/include")
list(APPEND LIBRARIES ${Matlab_ROOT_DIR}/extern/bin/glnxa64/libMatlabEngine.so)
list(APPEND LIBRARIES ${Matlab_ROOT_DIR}/extern/bin/glnxa64/libMatlabDataArray.so)
list(APPEND LIBRARIES ${Matlab_ROOT_DIR}/runtime/glnxa64/libMatlabCppSharedLib.so)
find_package(Threads)
list(APPEND PLATFORM_LIBS ${CMAKE_THREAD_LIBS_INIT})

add_executable(KazaeeTest main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE
${LIBRARIES}
${PLATFORM_LIBS}
pthread
)

Antworten (0)

Kategorien

Mehr zu Call C++ from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by