thread-safety

Does “volatile” guarantee anything at all in portable C code for multi-core systems?

拜拜、爱过 提交于 2019-12-22 03:15:33
问题 After looking at a bunch of other questions and their answers, I get the impression that there is no widespread agreement on what the "volatile" keyword in C means exactly. Even the standard itself does not seem to be clear enough for everyone to agree on what it means. Among other problems: It seems to provide different guarantees depending on your hardware and depending on your compiler. It affects compiler optimizations but not hardware optimizations, so on an advanced processor that does

Are Kotlin's singletons thread safe?

六眼飞鱼酱① 提交于 2019-12-22 01:26:06
问题 Are Kotlin singletons (more specifically, object declarations) thread-safe by construction? If not, what is the best practice to write thread safe singletons in Kotlin? I would guess they are, but I haven't been able to find any explicit statement about it in the docs. 回答1: Kotlin "object" is thread-safe by construction. As you can see in any decompile/dumping tool, declared object is just final class with static instance initialization + language syntax sugar to simplify instance access 来源:

HttpContext Class and its Thread Safety

爷,独闯天下 提交于 2019-12-21 21:58:19
问题 I have an Singleton object in application that has following property: private AllocationActionsCollection AllocationActions { get { return HttpContext.Current.Session["AllocationOptions.AllocationActions"] as AllocationActionsCollection; } set { HttpContext.Current.Session["AllocationOptions.AllocationActions"] = value; } } I'm dealing with one error ( HttpContext.Current.Session["AllocationOptions.AllocationActions"] is null even though it is supposed to me always set to valid instance...).

SSRS Code Shared Variables and Simultaneous Report Execution

馋奶兔 提交于 2019-12-21 21:19:08
问题 We have some SSRS reports that are failing when two of them are executed very close together. I've found out that if two instances of an SSRS report run at the same time, any Code variables declared at the class level (not inside a function) can collide. I suspect this may be the cause of our report failures and I'm working up a potential fix. The reason we're using the Code portion of SSRS at all is for things like custom group and page header calculation. The code is called from expressions

Thread-safe Enum Singleton

不羁的心 提交于 2019-12-21 21:12:14
问题 Enums are good for creating singleton. I know enum methods are not thread-safe so I tried to make it thread-safe. Can anyone please confirm if this implementation is correct or not. Is it fine to use static and volatile so many places and can it be optimized? As inner class is private so I have to create functions in enum to access inner class functionality. Can it be optimized? import java.util.Date; public enum SingletonWithEnum { INSTANCE; private static class Singleton{ private static

The behaviour of making a non-volatile reference to a volatile object in Java

回眸只為那壹抹淺笑 提交于 2019-12-21 20:58:08
问题 Coming from C/C++, I am a little confused about volatile object behavior in Java. I understand that volatile in Java has two properties: Won't bring object into cache, always keep it in main memory. Guarantee "happen-before" However I am not sure what happens if I make a new non-volatile reference to object. For example, class Example { private volatile Book b = null; public init() { b = new Book(...); } public use() { Book local = b; local.read(); } } AFAIK, volatile means the "book object"

Accessing UI view in another thread does *not* cause a crash. Why?

孤街浪徒 提交于 2019-12-21 20:56:04
问题 All: I really don't grok handlers yet. I thought that the code below -- modified so that, instead of using a handler, the UI widget (progress bar) was accessed directly -- would cause a cross-threading exception. But it doesn't. So, my question is, shouldn't this code crash? And if it doesn't, then when do I need to use a handler? public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progress = 0; progressBar = (ProgressBar)

Interrupt a thread waiting for user input and then exit the app

帅比萌擦擦* 提交于 2019-12-21 20:43:40
问题 I have two threads running, userInputThread waits for user input from the command line and interrupterThread tries to interrupt userInputThread 1 sec after starting. Obviously you cannot interrupt a thread that is blocked by the System.in . Another answer suggests to close System.in with System.in.close() before interrupting a thread. But when I run the following code, the userInputThread never gets interrupted and the app just hangs without closing. class InputInterruptionExample { private

Return locked resource from class with automatic unlocking

蓝咒 提交于 2019-12-21 19:42:44
问题 I would like to have a class member function which returns a pointer to a resource. The resource should be locked and unlocked automatically. I think of creating a non-copyable object which handles the locking. Do you think the following is a good solution? Is it thread-safe? Are there already tools in the STL for this use case? template<typename T, typename M> struct LockedResource { private: T* data_; std::unique_lock<std::mutex> lock_; public: LockedRessource(T* data, M& mtx) : data_{data}

Coding/Designing a generic thread-safe limiter (i.e. limit the execution of X() to Y many times per second)

℡╲_俬逩灬. 提交于 2019-12-21 17:54:17
问题 I'm planning to design a class to limit the execution of a function to given amount within the specified time, for example: Process max. 5 files in 1 second It should be thread-safe and performance hit should be minimal. How would you design such a class? I've got couple of ideas but none of them seemed right to me. Is there any known design pattern for such a task? ( I'm coding .NET but any language is OK ) From the outside class should work like this ( assuming it's singleton ): Setup: