C# and thread-safety of a bool

A little bit late but should be useful to the others. You can implement your own thread safe boolean in the following way: // default is false, set 1 for true. private int _threadSafeBoolBackValue = 0; public bool ThreadSafeBool { get { return (Interlocked.CompareExchange(ref _threadSafeBoolBackValue, 1, 1) == 1); } set { if (value) Interlocked.CompareExchange(ref … Read more

C++11 Thread safety of Random number generators

The C++11 standard library is broadly thread safe. The thread safety guarantees on PRNG objects are the same as on containers. More specifically, since the PRNG classes are all pseudo-random, i.e. they generate a deterministic sequence based on a definite current state, there is really no room to be peeking or poking at anything outside … Read more

What is the best way to use Redis in a Multi-threaded Rails environment? (Puma / Sidekiq)

You use a separate global connection pool for your application code. Put something like this in your redis.rb initializer: require ‘connection_pool’ REDIS = ConnectionPool.new(size: 10) { Redis.new } Now in your application code anywhere, you can do this: REDIS.with do |conn| # some redis operations end You’ll have up to 10 connections to share amongst … Read more

Is it safe to use a boolean flag to stop a thread from running in C#

You better mark it volatile though: The volatile keyword indicates that a field might be modified by multiple concurrently executing threads. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times. But … Read more

Creating a thread-safe temporary file name

Dir::Tmpname.create You could use Dir::Tmpname.create. It figures out what temporary directory to use (unless you pass it a directory). It’s a little ugly to use given that it expects a block: require ‘tmpdir’ # => true Dir::Tmpname.create([‘prefix-‘, ‘.ext’]) {} # => “/tmp/prefix-20190827-1-87n9iu.ext” Dir::Tmpname.create([‘prefix-‘, ‘.ext’], ‘/my/custom/directory’) {} # => “/my/custom/directory/prefix-20190827-1-11x2u0h.ext” The block is there for code … Read more

Difference between “free-threaded” and “thread-safe”

There may well be other things meant in other contexts, but in cases I’ve worked with in the past, “free threaded” means it works, or at least can work, across different threads without any marshalling between apartments. Apartment-threading in contrast blocks off different “apartments” with separate copies of “global” data (which hence isn’t really global, … Read more

Must Spring component classes be thread-safe

Given @Controller public class MyController { @RequestMapping(value = “/index”) public String respond() { return “index”; } } Spring will create an instance of MyController. This is because Spring parses your configuration, <mvc:annotation-driven>, sees @Controller (which is like @Component) and instantiates the annotated class. Because it sees @RequestMapping as well, it generates a HandlerMapping for it, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)