thread-safety

Is a SecurityContext shared between requests when using Spring Security?

十年热恋 提交于 2019-12-22 05:11:58
问题 I'm seeing some strange behaviour when using stateless token-based authentication on a rest API written using Spring Boot. The client includes a JWT token with each request, and a custom filter I've written that extends GenericFilterBean adds an Authentication object based on the claims in the token to the security context using the following : SecurityContextHolder.getContext().setAuthentication(authentication); And clears the context after processing the request by doing :

Using a F# event and asynchronous in multi-threaded code

断了今生、忘了曾经 提交于 2019-12-22 05:08:36
问题 I'm working a lot with asynchronous workflows and agents in F#. While I was going a little bit deeper into events I noticed that the Event<_>() type is not thread-safe. Here I'm not talking about the common problem of raising an event. I'm actually talking about subscribing and removing/disposing from an event. For testing purposes, I have written this short program: let event = Event<int>() let sub = event.Publish [<EntryPoint>] let main argv = let subscribe sub x = async { let mutable

Mysterious behavior of Dictionary<TKey, TSource>

青春壹個敷衍的年華 提交于 2019-12-22 04:34:10
问题 I'm working on a huge system based on Asp.net MVC 3.0 and working on Mono-2.10.8 (Windows 7). Everything was fine until a moment couple of days ago. Inside my API I have several utility classes using dictionaries. For example, like this one: public static class KeyUtility { static KeyUtility() { Alphabet = new[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; ReverseAlphabet =

Why would ABAddressbookRef need to be created for each thread?

南楼画角 提交于 2019-12-22 04:29:15
问题 Apple says: Important: Instances of ABAddressBookRef can not be used by multiple threads. Each thread must make its own instance. But why? I know that some particular class or operations must be done in main thread. And I know some objects are not thread-safe (which means it would cause problem if these objects are accessed by two different threads concurrently). But, if you can make sure that the the thread-unsafe objects are accessed by only one thread at any moment, then there should be

Efficient consumer thread with multiple producers

旧城冷巷雨未停 提交于 2019-12-22 04:16:23
问题 I am trying to make a producer/consumer thread situation more efficient by skipping expensive event operations if necessary with something like: //cas(variable, compare, set) is atomic compare and swap //queue is already lock free running = false // dd item to queue – producer thread(s) if(cas(running, false, true)) { // We effectively obtained a lock on signalling the event add_to_queue() signal_event() } else { // Most of the time if things are busy we should not be signalling the event add

Java Remove Specific Item From ConcurrentHashMap

馋奶兔 提交于 2019-12-22 04:04:30
问题 Is using the remove() method okay? I've read an article that synchronization hasn't been added to the remove method. How do I properly remove a specific item from a ConcurrentHashMap? Example Code: ConcurrentHashMap<String,Integer> storage = new ConcurrentHashMap<String,Integer>(); storage.put("First", 1); storage.put("Second", 2); storage.put("Third",3); //Is this the proper way of removing a specific item from a tread-safe collection? storage.remove("First"); for (Entry<String, Integer>

Caching expensive data in C++ - function-scoped statics vs mutable member variables

做~自己de王妃 提交于 2019-12-22 04:01:23
问题 I've got a relatively expensive data-fetching operation that I want to cache the results of. This operation is called from const methods, roughly like this: double AdjustData(double d, int key) const { double factor = LongRunningOperationToFetchFactor(key); return factor * d; } I'd like AdjustData to remain const , but I want to cache out the factor so I only fetch it the first time. At present I'm using a mutable map<int, double> to store the result (the map being from key to factor ), but I

guava-libraries: is Iterators.cycle() thread-safe?

给你一囗甜甜゛ 提交于 2019-12-22 03:54:13
问题 Suppose I have the following class: public class Foo { private List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5); private Iterator<Integer> iterator = Iterators.cycle(list); public void bar(){ Integer value = iterator.next(); doSomethingWithAnInteger(value); } } If an instance of Foo is acessed simultaneously by two threads, I need that each thread gets a different value from iterator.next() . Does the bar() method have to be made synchronized? Or is iterator.next() guaranteed to be

Is the null coalescing operator (??) in C# thread-safe?

只谈情不闲聊 提交于 2019-12-22 03:20:13
问题 Is there a race condition in the following code that could result in a NullReferenceException ? -- or -- Is it possible for the Callback variable to be set to null after the null coalescing operator checks for a null value but before the function is invoked? class MyClass { public Action Callback { get; set; } public void DoCallback() { (Callback ?? new Action(() => { }))(); } } EDIT This is a question that arose out of curiosity. I don't normally code this way. I'm not worried about the

Calculating average and percentiles from a histogram map?

依然范特西╮ 提交于 2019-12-22 03:20:11
问题 I have written a timer which will measure the performance of a particular code in any multithreaded application. In the below timer, it will also populate the map with how many calls took x milliseconds. I will use this map as part of my histogram to do further analysis, like what percentage of calls took this much milliseconds and etc. public static class StopWatch { public static ConcurrentHashMap<Long, Long> histogram = new ConcurrentHashMap<Long, Long>(); /** * Creates an instance of the