-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
while(true) getting removed when building for release mode (?) #21396
Comments
This comment was marked as spam.
This comment was marked as spam.
1 similar comment
This comment was marked as spam.
This comment was marked as spam.
Without the use of any synchronization primitives (fences, atomics) the compiler currently doesn't consider the memory effects of other threads, As for the workarounds you found, Zig's memory model currently mostly follows what LLVM provides to support the newest C and C++ standards (see also #6396), EDIT: Also feel free to ask for advice in one of the community spaces, which can be more friendly to newcomers and can help differentiate what is or isn't a Zig bug. |
So this probably isn't a bug in the compiler but rather a lack of synchronization method when dealing with threads, do you think this should in that case be closed @rohlem? Also thanks for the resources provided, i've been investigating a little bit more about fences and atomics and atomics seems to be something i could try studying further for my usecase which is write/reading at the same time |
Discussion what the code is meant to do probably fits best into the above-linked memory model issue #6396 . |
Yeah they look much more appropiate than this one, so, sure. Thank you! |
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
Hi! I observed this issue while creating a program which made use of two threads that consume a queue ds, so the first thread will add allocated data to the queue while the second thread will consume it. First of all am sorry for the long repro program, this was the best i could do to try to reproduce it since others simplified attempts would just fail to reproduce the issue. Maybe experts can help me here understand a little bit why this is behaving the way it's behaving?
This is the example program i ended writing for repro purposes
This program works fine if one runs it by using
zig build run
in an empty initialised project, and produces the next output:The problem happens when building with -Doptimize=Release{Safe,Fast,Small} flags, if one does something like this
It will just omit the instructions to .dequeue() in the second thread, and produce the next:
So messing a little bit i observed that updating the second thread code to something like this would make it work in release mode aswell
And that will start printing both ?? and the hello {s} {s}, and also call deinit on *Person
so investigating i came up with this issue that was kinda similar but they suggested in some point doing
asm volatile ("nop")
And if i replace that std.debug.print("??") with asm volatile ("nop") it starts to work aswell just fine in release mode.
Expected Behavior
Tbh im not sure wether the optimization issue is because of the anonymous structs usage, or because of the structs as arguments, but anyways expected behavior i think would be that the while (true) doesn't gets removed because if i need to be dequeuing every time i need to do a while true prior the .dequeue, ik a while (queue.dequeue()) could do but that will end the execution when no more elements are found which is not my usecase.
The text was updated successfully, but these errors were encountered: