mex multithreading with c++

조회 수: 8 (최근 30일)
Matt.
Matt. 2019년 10월 15일
답변: Matt. 2019년 10월 15일
Hi !
I was trying to improve the speed of part of my code with a mex function. It decreased sensibly but I wante to improve it even more by using multithreading. The pure C++ function works fine, but I keep getting an error when trying to compile the mex function. I am using Matlab2019b with visual studio 2019 compiler.
I wrote the following minimal function:
#include "mex.hpp"
#include "mexAdapter.hpp"
#include <thread>
#include <chrono>
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
std::thread t=std::thread(sleeponesec);
t.join();
}
void sleeponesec(){
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
};
>> mex testmex.cpp
Building with 'Microsoft Visual C++ 2019'.
Error using mex
testmex.cpp
...\Mex\testmex.cpp(11): error C3867:
'MexFunction::sleeponesec': non-standard syntax; use '&' to create a pointer to member

답변 (2개)

Matt.
Matt. 2019년 10월 15일
Obviously std::this_thread::sleep_for(std::chrono::milliseconds(1000)); is just an example. replacing it by : int a=2; does ntop change anything and mex test.cpp reutnrs the same error.

Matt.
Matt. 2019년 10월 15일
I have found a workaround which consists in defining the function out of the class. I am not completely satisfied as I still do not really understand what was causing this error, but at least it works.
#include "mex.hpp"
#include "mexAdapter.hpp"
#include <thread>
#include <chrono>
void sleeponesec(){
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
std::thread t=std::thread(sleeponesec);
t.join();
}
};
I have also tried to call
  • std::thread t=std::thread(MexFunction::sleeponesec); but it returns the same error.
  • std::thread t=std::thread(&MexFunction::sleeponesec); which returns the scarry following errors:
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(40): error C2672: 'std::invoke':
no matching overloaded function found
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(48): note: see reference to
function template instantiation 'unsigned int std::thread::_Invoke<_Tuple,0,1>(void *) noexcept' being compiled
with
[
_Tuple=_Tuple
]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(56): note: see reference to
function template instantiation 'unsigned int (__cdecl *std::thread::_Get_invoke<_Tuple,0,1>(std::integer_sequence<unsigned __int64,0,1>)
noexcept)(void *)' being compiled
C:\Users\...\testmex.cpp(22): note: see reference
to function template instantiation 'std::thread::thread<void(__cdecl MexFunction::* )(matlab::data::TypedArray<double>
&),std::reference_wrapper<matlab::data::TypedArray<double>>,void>(_Fn &&,std::reference_wrapper<matlab::data::TypedArray<double>> &&)'
being compiled
with
[
_Fn=void (__cdecl MexFunction::* )(matlab::data::TypedArray<double> &)
]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(40): error C2893: Failed to
specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\type_traits(1571): note: see declaration
of 'std::invoke'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(40): note: With the following
template arguments:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(40): note: '_Callable=void
(__cdecl MexFunction::* )(matlab::data::TypedArray<double> &)'
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(40): note:
'_Types={std::reference_wrapper<matlab::data::TypedArray<double>>}'

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by