Reputation: 546
Background:
My project is C++ MFC
. But I am writing the code using modern C++ as the standard evolves. Currently, I invoke threads sometimes using std::thread
(later code) and sometimes the MFC CreateThread
complete with required waitForSingleObject
and thread priority parameter. Clearly, the std::thread
is more elegant and therefore more maintainable. It is important to over time make the code consistent and drop old idioms where possible.
Problem:
But invoking std::thread
with a class Baz
in sample code below that has CWinThread
as its base, fails to find a std::thread
constructor.
Sample Code:
struct Caz {};
struct Baz : public CWinThread // works if base is Caz
{
void operator()() {}
};
std::shared_ptr< Baz > b;
b = std::make_shared< Baz >();
std::thread bazThread( *b ); // error: can't find a constructor when CWinThread is base, but can for Caz as base
When the base class is CWinThread
it fails to resolve, but when the base class is Caz
it works fine. Both Caz
and CWinThread
have default constructors.
Why is CWinThread
making it fail?
Edit1:
The error message set is:
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2057,35): error C2665: 'std::tuple::tuple': none of the 2 overloads could convert all the argument types
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\tuple(321,5): message : could be 'std::tuple::tuple(std::tuple &&)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\tuple(320,5): message : or 'std::tuple::tuple(const std::tuple &)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : while trying to match the argument list '(_Ty2)'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : with
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : [
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : _Ty2=Baz 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\memory(2056,59): message : ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : see reference to function template instantiation 'std::unique_ptr<_Tuple,std::default_delete<_Ty>> std::make_unique<_Tuple,_Ty2&,0>(_Ty2 &)' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : with
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : [
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : _Ty=_Tuple,
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : _Ty2=Baz
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include\thread(53): message : ]
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : see reference to function template instantiation 'std::thread::thread<_Ty2&,,void>(_Fn)' being compiled
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : with
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : [
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : _Ty2=Baz,
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : _Fn=Baz &
1>E:\Hiveware\trunk\Windows\hives\Hiveware\SdcdcHiveware\GrammarSdcdcHiveware.cpp(153): message : ]
1>Done building project "SdcdcHiveware.vcxproj" -- FAILED.
Edit2:
// FUNCTION TEMPLATE make_unique
template <class _Ty, class... _Types, enable_if_t<!is_array_v<_Ty>, int> = 0>
_NODISCARD unique_ptr<_Ty> make_unique(_Types&&... _Args) { // make a unique_ptr
return unique_ptr<_Ty>(new _Ty(_STD forward<_Types>(_Args)...));
}
Upvotes: 1
Views: 439
Reputation: 546
Tentative answer unless someone finds a workaround.
std::thread
cannot be used in MFC
projects for class types that inherit from CWinThread
, also called user threads
for which sample code may be found at Creating Threads.
For traditional windows worker threads
invoking std::thread instead works fine.
Upvotes: 0