Hi Folks:
Nobody commented on my question about spawning detached thread tasks.
Consequently, I looked at the code in src/unix/thread.c and discovered
pthread_create() does not have provisions for spawning a detached thread.
The routine in the attached file spawns a detached thread correctly on
Unix/Linux
systems and works under fairly heavy testing. However, I am not sure it
is in line with the Libuv implementation conventions, and it is certainly
not elegant nor of optimal modularity.
I leave it up to the Libuv implementors to come up with a more complete
solution.
Best Regards,
Paul R.
On Friday, January 1, 2021 at 4:26:31 PM UTC-8 [email protected]
wrote:
> What is the right way to create a detached thread and insure resources
> are released upon termination under Libuv ?
>
>
--
You received this message because you are subscribed to the Google Groups
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/libuv/e616b72c-8a93-4cab-ab11-2031fe7c32adn%40googlegroups.com.
#define DETACHED_THREAD_KLUDGE
#ifdef DETACHED_THREAD_KLUDGE
//
// Creates a detached thread task.
// The paramters and their semantics are exactly the same as for pthread_create().
//
int uv_thread_detached_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
uv_thread_options_t params;
int err;
pthread_attr_t* attr;
pthread_attr_t attr_storage;
size_t pagesize;
size_t stack_size;
/* Used to squelch a -Wcast-function-type warning. */
union {
void (*in)(void*);
void* (*out)(void*);
} f;
params.flags = UV_THREAD_NO_FLAGS;
stack_size =
params.flags & UV_THREAD_HAS_STACK_SIZE ? params.stack_size : 0;
attr = NULL;
if (stack_size == 0) {
stack_size = thread_stack_size();
} else {
pagesize = (size_t)getpagesize();
/* Round up to the nearest page boundary. */
stack_size = (stack_size + pagesize - 1) &~ (pagesize - 1);
#ifdef PTHREAD_STACK_MIN
if (stack_size < PTHREAD_STACK_MIN)
stack_size = PTHREAD_STACK_MIN;
#endif
}
attr = &attr_storage;
if (pthread_attr_init(attr))
abort();
if (stack_size > 0) {
if (pthread_attr_setstacksize(attr, stack_size))
abort();
}
pthread_attr_setdetachstate(attr, PTHREAD_CREATE_DETACHED);
f.in = entry;
err = pthread_create(tid, attr, f.out, arg);
if (attr != NULL)
pthread_attr_destroy(attr);
pthread_detach(*tid);
return UV__ERR(err);
}
#endif // DETACHED_TRHREAD_KLUDGE