jeudi 21 juillet 2022

Ia a prameter for a thread object mandated to be created in the destination thead

Lool at the following code:

#if defined(_WIN32)
    #include <Windows.h>
#elif defined(__unix__)
    #include <pthread.h>
#endif
#include <iostream>
#include <thread>
#include <type_traits>

using namespace std;

struct S
{
    S( char const *str );
};

S::S( char const *str )
{
#if defined(_WIN32)
    using id_t = DWORD;
#elif defined(__unix__)
    using id_t = pthread_t;
#endif
    static_assert(is_scalar_v<id_t>, "thread id has to be scalar");
    id_t id;
#if defined(_WIN32)
    id = GetCurrentThreadId();
#elif defined(__unix__)
    id = pthread_self();
#endif
    cout << id << endl;
}

int main()
{
    S( nullptr );
    auto thrObj = []( S const &s ) { };
    thread( thrObj, nullptr ).join();
    thread( thrObj, S( nullptr ) ).join();
}

Does the standard directly or indirectly mandate that the S() in the second line is created in the destination thread ? Or is this just somehow impossible to have creation on the initiating side ?

Aucun commentaire:

Enregistrer un commentaire