-
Notifications
You must be signed in to change notification settings - Fork 163
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
Rationale behind thread_local for color_mode singleton #259
Comments
According to N3337 §3.7.2 [basic.stc.thread],
It means that you will implicitly make different object per thread by adding So, roughly speaking, we will have different static variable per thread. This allows us to call |
Yes, this is what I am concerned about. I was thinking there were quite few use cases where we would want to use different color modes per thread. What would you think? |
Well, there is not such a strong motive here, simply that I was concerned about not being thread-safe. I was thinking that there can be an implementation like, one thread takes In my use cases, I usually read one or a few configuration files first in a single thread at the beginning, and then the parallelized code runs based on that input, so the implementation of this part is not really an issue. Since your code is likely to actually read files in multiple threads, if it is actually inconvenient there, I think that means we should consider changing it. |
You might be talking about a different location of thread-safety, but a singleton seems to be thread-safe. |
initialization is ok, but I think changing state from threads is not. no? |
Hmm, that's true. Then, I think using |
I thought about using mutex and atomic when I added Consider the case where threads with different color settings exist. In such a situation, it is almost meaningless to protect only state updates with the In summary, there are the following differences between these two options:
If threads with different color settings run simultaneously, However, if you found this choice makes it inconvenient, we need to reconsider. It is an old-fashioned solution, but what about adding a macro like Also, having a |
I agree with your idea of |
toml11/include/toml11/fwd/color_fwd.hpp
Line 47 in 5727819
IIUC, a singleton is typically meant to provide a single shared instance across the entire application regardless of the number of threads. Can I know your rationale for using thread_local for color_mode?
The text was updated successfully, but these errors were encountered: