-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Context for Concurrent Tasks. #9028
Comments
I don't think everything is already decided. So feel free to make proposals (preferably based on Weave API) and don't forget to read #1868 . |
Before answering your questions: when looking at your code I see one thing you should not do - neither in C, nor in Go, nor in V: modify/read a variable ( fn coroutine(shared f FifoData, ch chan bool) {
...
lock f {
f.data -= id
}
...
shared f := FifoData{0}
go coroutine(shared f, ch)
...
lock f {
f.data += id
}
...
rlock f {
println(f)
} Passing
Well, the "suggested way" to do something depends on what you want to accomplish. To tell a concurrent thread to end itself you need some atomic flag like // receiving thread:
new_working_packet := <-ch or {
// could not receive anything because `ch` was closed and nothing is left in the queue
return maybe_some_result
}
// sending thread
ch <- new working thread or {
// could not push into channel because it was closed
return maybe_some_result
}
fn th() ?f64 { ... }
// wait for a thread to finish
handle := go th()
// do something else in between
result := handle.wait() or {
// some error handling
3.141592653589793 // default result
}
I'm not aware of any plans, but it might be a nice idea to add something like this to the library. I'll think about it.
I'm not sure if I understand correctly what you mean by that. You can push/pop arrays like |
Maybe we should add a chapter "concurrency dos and don'ts" or "concurrency best practices" to the docs. |
Having thought about it I come to the conclusion that everything it Go's Go's ch := chan f64{}
go fn(ch chan f64) {
time.sleep(800 * time.millisecond)
ch.close()
}(ch) So, I think V doesn't need a |
Seems small enough to put it to doc as an example (I find it quite generic to show how to do such stuff in V). |
@UweKrueger synchronization mechanism is undoubtedly needed if a shared instance has more than one producer. However, if the parallelism is not achieved by memory sharing but by communication, which means somehow copies of data may exist in different units and less or only one producer exists at one time, thus the lock can be avoided or relieved to some degree. That's one special occasion especially needed in distributed calculation. As for concurrency communication, currently using channel as signal or using select mechanism surely functions well for universal usage. Context module may not be necessary but can strengthen such practice. Final question, user-defined length of data can't be passed by channel means user always need to wrap data buffer in V struct, in my understanding, it should only provide reference sharing. Sometimes, direct data sharing may also be needed. |
@amoyyy it has been a while since you created this issue. There already is a context module similar to Golang's context. You can check the docs here 👌🏻 https://modules.vlang.io/context.html |
Currently, if a concurrent task is not set up in main(), the cancelation of the child concurrent task should be handled by person. It's fine to using a signal flag communicated by channel like below.
a demo:
Is this the suggested way in V? And I wonder if there are other possible implements.
By the way, will a context module (like golang) be provided in future?
And is it possible to push and pop self-define length of data into or from channel?
The text was updated successfully, but these errors were encountered: