Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Interesting details

14 views
Skip to first unread message

Bonita Montero

unread,
Jul 21, 2022, 6:30:07 AM7/21/22
to
#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 << ": " << str << endl;
}

int main()
{
S( "hello world" );
jthread( []( S const &s ) { }, "hello world" ).join();
jthread( []( S const &s ) { }, S( "hello world" ) ).join();
}

I usually pass the same parameter type for the parameters of a thread-
or function-object. In the above code I do that not with one creation
of the thread-object. Interestingly the actual object is created on
the side of the created object, i.e. the internal function<>-object
the thread useds internally carries an object of the the given type.
That means wou won't be able to catch any construction exceptions
in the destination thread.
0 new messages