-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
single vs multi threaded as a builtin compile time option #1764
Comments
As mentioned in the above linked issue, another thing this option should do is turn thread local variables into global variables. |
This is an interesting idea. Regarding only userland code, do you see other advantages than providing "API sugar" to libraries, ie. having mirrored single/multi threaded APIs for free ?
I think this is very much needed as a global flag would be a far too low granularity. But how would the scoped-based instruction and global compilation flag would interact ? For example, you compile your project in Is this possible, and if so what happens ? Do we forbid cross-multithread-ness-boundaries state sharing when it would make a difference (annoying) ? Do we recompile the struct with |
May also be worth noting that Nim provides a similar option ( |
Beware that this is the source of many glibc bugs. See http://lua-users.org/lists/lua-l/2015-04/msg00010.html and then https://sourceware.org/bugzilla/show_bug.cgi?id=18192 |
Sure, it's quite possible that you get an error if you try to use |
If you're creating a library you can't control who might use you: what if you are used in a multi-threaded program? If you're creating a executable that loads any dynamic libraries then you need to be multi-thread ready just in case on of the libraries you load (or even their dependencies) are multi-threaded. If you proceed with this issue, I think you need some sort of marker injected into the binary/library saying "single-threaded", and make it an error to load a library without that flag. |
All the userland benefits I can think of can be categorized as mirrored single/multi threaded APIs for free. It seems like a worthwhile benefit to me.
This (and your following description) is a good question. When I originally talked about supporting this flag at any scope I was thinking about language differences, such as whether coroutines are emitted with atomic operations. You could do this, for example, if you could guarantee that a particular coroutine or set of coroutines would always be created, suspended, resumed, and awaited from the same thread. However it's not clear how this would work in general. For userland code, which will be checking
Then they'll be building with the single-threaded flag off, and everything will work correctly. In order to mess things up the library user would have to:
Note that even multithreaded applications can use code built in single threaded mode, as long as the single-threaded code only ever sees a single thread for the entire lifetime of the application.
This is a good idea, and would be further enhanced by #1535. |
One problem I just ran into is that even in single-threaded mode, Otherwise, one of these things must happen:
|
|
To me this is the biggest selling point of zig: being able to generate C ABI libraries that can be used from other languages. I think saying "rather than the usual way" is taking a far too zig-centric view. |
That's fair enough, but the other 2 points still stand, and, for the case where you are generating a C ABI library, you are in control of your own build process. So don't override the default by passing |
@andrewrk The short answer unfortunately is 'no'. Native Linux AIO is unreliable, it can still block or fail. I'm unclear on whether FreeBSD's and Solaris's AIO APIs are reliable. They don't seem to be in widespread use; make of that what you will. I'm working on |
There are many places where it matters whether code is single- or multi-threaded. For example:
This proposal is to add
--single-threaded
as a build-option that is exposed as@import("builtin").single_threaded
. Libraries can then use this to write code that is optimal for both cases. Most libraries can ignore the boolean; instead having functions that operate only on their input parameters or are simply not documented to be thread-safe. Where this value comes in useful is when a library has thread-safe functions, and the implementation can be optimized when it is known that there will only ever be a single thread. For example,std.atomic.Queue
can simply omit all the mutex locking code, and its functions remain "thread safe" because there will only ever be one thread.In the same way we plan to be able to set the build mode in a scope (#978) it should be possible to set the multithreaded-ness in a scope.
It's important to note that coroutines in particular, become extremely low overhead when compiling with
--single-threaded
. A--release-fast --single-threaded
build which uses coroutines and always stack allocated the frames would in theory generate the exact same code as if it used normal functions.The text was updated successfully, but these errors were encountered: