mex multithreading with c++

8 visualizaciones (últimos 30 días)
Matt.
Matt. el 15 de Oct. de 2019
Respondida: Matt. el 15 de Oct. de 2019
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

Respuestas (2)

Matt.
Matt. el 15 de Oct. de 2019
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. el 15 de Oct. de 2019
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>>}'

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by