micro-os-plus/micro-os-plus-iii

implement thread::detach()

Opened this issue · 7 comments

it's currently not implemented (silently returns) but there is no note in the documentation about it

Right, I remember by the time I wrote it I had no idea what to do.

But the documentation must be updated anyway.

Do you have a use case where you need functionality added to it?

Do you have a use case where you need functionality added to it?

When you start a thread as fire-and-forget you don't want to keep a handle to the thread. So you call detach() on it before it goes out of scope.

Another option would be to implement std::async but that forces you to the std api that doesn't allow to set prioriities and stack size.

you don't want to keep a handle to the thread

I guess this makes more sense in C with POSIX threads, but in C++, if the thread is a local (stack) object, and you exit the scope, the object is destroyed anyway.

If the thread was allocated somewhere else (dynamically or statically), it is alive as long as that scope is alive, so detach again has not much to do.

Am I missing something?

I would say it's as if you would "move" the thread into the scheduler:

Once you detach, the thread continue running until it exits. The destructor doesn't have any effect anymore in that situation. Ressources are freed once the thread exits (see cpp reference).

That's probably possible with std::thread, which is only a wrapper over an internal thread, but os::thread is the actual object, with resources used by the scheduler, it normally cannot survive if moved somewhere else (or things get very complicated).

I don't know how to implement this, but for example CMSIS-RTOS seems to able to do that
(I know it's not C++ but still it might be worth checking the possibility once the threads are revisited)

Ok, will do.