python dictionary is thread safe?

The other answers already correctly addressed what’s apparently your actual question: Does it mean I can or cannot modified the items in a dictionary while iterating over it? by explaining that thread safety has nothing to do with the issue, and in any case, no, you cannot modify a dict while iterating over it. However, … Read more

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

I’m no expert, but cppreference.com has what appears to me to be some pretty good information on volatile. Here’s the gist of it: Every access (both read and write) made through an lvalue expression of volatile-qualified type is considered an observable side effect for the purpose of optimization and is evaluated strictly according to the … Read more

Threads – Why a Lock has to be followed by try and finally

Because a try/finally block is the only way to guarantee that a segment of code is executed after another completes. You ask why not do this: public void function1(){ read.lock(); this.getSharedInt(); read.unlock(); } What happens when this.getSharedInt() throws an exception? Then your read.unlock() line will not be executed, causing program deadlock. Sure, it may be … Read more

How can I guarantee that a type that doesn’t implement Sync can actually be safely shared between threads?

Another solution for this case is to move a mutable reference to the item into the thread, even though mutability isn’t required. Since there can be only one mutable reference, the compiler knows that it’s safe to be used in another thread. use crossbeam; // 0.7.3 use std::cell::RefCell; fn main() { let mut val = … Read more

Is it thread safe to set Active Resource HTTP authentication on a per-user basis?

Monkey patch the host, user and password methods of ActiveResource::Base class: class ActiveResource::Base # store the attribute value in a thread local variable class << self %w(host user password).each do |attr| define_method(attr) do Thread.current[“active_resource.#{attr}”] end define_method(“#{attr}=”) do |val| Thread.current[“active_resource.#{attr}”] = val end end end end Now set the credentials in every request class ApplicationController < … Read more