Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement thread::detach() #69

Open
LnnrtS opened this issue Jan 18, 2021 · 7 comments
Open

implement thread::detach() #69

LnnrtS opened this issue Jan 18, 2021 · 7 comments
Assignees
Labels

Comments

@LnnrtS
Copy link
Contributor

LnnrtS commented Jan 18, 2021

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

@ilg-ul ilg-ul self-assigned this Jan 18, 2021
@ilg-ul ilg-ul added the bug label Jan 18, 2021
@ilg-ul
Copy link
Contributor

ilg-ul commented Jan 18, 2021

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?

@LnnrtS
Copy link
Contributor Author

LnnrtS commented Jan 18, 2021

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.

@ilg-ul
Copy link
Contributor

ilg-ul commented Jan 18, 2021

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?

@LnnrtS
Copy link
Contributor Author

LnnrtS commented Jan 18, 2021

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).

@ilg-ul
Copy link
Contributor

ilg-ul commented Jan 18, 2021

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).

@LnnrtS
Copy link
Contributor Author

LnnrtS commented Jan 18, 2021

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)

@ilg-ul
Copy link
Contributor

ilg-ul commented Jan 18, 2021

Ok, will do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants