-
Notifications
You must be signed in to change notification settings - Fork 137
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
[Question][HW4] How can I ensure that a struct is not dropped until a function pointed by a field of the struct completes? #905
Comments
I have the same problem. |
What I did to make the compiler happy is make a |
Apparently what I'm doing is really wrong. Running
|
I used |
The recommended way is to use |
But when I do that behavior might be dropped early since the function |
Since Recall that |
Got it. To sum up, as |
This question is related to the function
Behavior::resolve_one
.The function is responsible for the followings (as the paper says):
At step (2), the function should call the function (thunk) behind the pointer to the behavior (
*const Behavior
).But as the type
*const Behavior
does not implementSend
, it is not possible to call the function in another thread.Also, as the function pointer type
(dyn FnOnce() + Send + 'static)
does not implementSync
, it is not possible to share the function between threads.Therefore, we need a way to circumvent this constraints. One possible way is to use
std::ptr::read
, but it has a problem where that the spawning thread can dropBehavior
before the thunk completes.Q: is there a way to overcome the constraints while I can ensure that the function is not destroyed before running?
The text was updated successfully, but these errors were encountered: