Я попробовал это для Win32, и это код:
Код: Выделить всё
#include
#include
#include
#include
#include
using namespace std;
template
void xthread( Fn &&fn, Args &&... args )
{
using tupl_t = tuple;
auto argsSeq = make_index_sequence();
auto thr = []( index_sequence )
{
return +[]( LPVOID lpvThreadParam ) -> DWORD
{
tupl_t &tupl = *(tupl_t *)lpvThreadParam;
get( tupl )( get( tupl ) ... );
return 0;
};
};
auto fnObj = make_unique( forward( fn ), forward( args ) ... );
HANDLE hThread = CreateThread( nullptr, 0, thr( argsSeq ), fnObj.get(), 0, nullptr );
if( hThread )
fnObj.reset(),
WaitForSingleObject( hThread, INFINITE );
else
{
auto back = []( Dst &&dst, auto &&src )
{
if constexpr( is_rvalue_reference_v && !is_const_v )
dst = move( src );
};
back( forward( fn ), get( *fnObj ) );
[&]( index_sequence )
{
(back( forward( args ), get( *fnObj.get() ) ), ...);
}( argsSeq );
}
}
int main()
{
string str( "hello world" );
xthread( []( string &str ) { cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78450024/own-thread-creation-which-reverts-function-parameters-on-thread-creatrion-failur[/url]
Мобильная версия