thread-safety

iostream thread safety, must cout and cerr be locked separately?

流过昼夜 提交于 2020-01-01 04:46:05
问题 I understand that to avoid output intermixing access to cout and cerr by multiple threads must be synchronized. In a program that uses both cout and cerr, is it sufficient to lock them separately? or is it still unsafe to write to cout and cerr simultaneously? Edit clarification: I understand that cout and cerr are "Thread Safe" in C++11. My question is whether or not a write to cout and a write to cerr by different threads simultaneously can interfere with each other (resulting in

Is the List<T>.AddRange() thread safe?

会有一股神秘感。 提交于 2020-01-01 04:11:47
问题 Can I, without locking, safely call List.AddRange(r) from multiple threads? If not, what sort of trouble would I run into? 回答1: No , its documentation does not say it is thread safe, therefore it is not. Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe. As to what can go wrong, think about what AddRange(newItems) does: Check if there is enough space in the internal array If not: Allocate a new array Copy the

Are there greenDAO thread safety best practices?

痴心易碎 提交于 2020-01-01 04:04:01
问题 I'm having a go with greenDAO and so far it's going pretty well. One thing that doesn't seem to be covered by the docs or website (or anywhere :( ) is how it handles thread safety. I know the basics mentioned elsewhere, like "use a single dao session" (general practice for Android + SQLite), and I understand the Java memory model quite well. The library internals even appear threadsafe, or at least built with that intention. But nothing I've seen covers this: greenDAO caches entities by

Java - Immutable array thread-safety

蹲街弑〆低调 提交于 2020-01-01 03:04:27
问题 I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that

Java - Immutable array thread-safety

不问归期 提交于 2020-01-01 03:04:08
问题 I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that

Synchronizing on SimpleDateFormat vs. clone

妖精的绣舞 提交于 2020-01-01 01:48:26
问题 We know that the dateformat classes are not thread safe. I have a multi-threaded scenario where dateformats needs to be used. I can't really create a new instance in new thread as SimpledateFormat creation seems to be expensive(the constructor ends up calling "compile" which is costly). After some tests the only two options left for me are: External Synchronization - I really dont want to do this Cloning in each thread - Don't know whether there are some catches? Any suggestions? If guys have

C++ Thread Safe Integer

牧云@^-^@ 提交于 2020-01-01 01:36:07
问题 I have currently created a C++ class for a thread safe integer which simply stores an integer privately and has public get a set functions which use a boost::mutex to ensure that only one change at a time can be applied to the integer. Is this the most efficient way to do it, I have been informed that mutexes are quite resource intensive? The class is used a lot, very rapidly so it could well be a bottleneck... Googleing C++ Thread Safe Integer returns unclear views and oppinions on the

Sharing data between multiple java threads and get the updated value

空扰寡人 提交于 2019-12-31 14:59:57
问题 I want to create a java application , where we want to make rest calls for multiple users , with the help of an access token. I am using 1 thread per user . The access token, that I am using , is valid for 1 hour.Once the token expires , I will get an 401 error , and have to update the token for all the threads , and continue. I am thinking of using a volatile variable which I have made static to update all the threads. My requirement , is , the moment I get to know in one of threads that the

Sharing data between multiple java threads and get the updated value

僤鯓⒐⒋嵵緔 提交于 2019-12-31 14:59:04
问题 I want to create a java application , where we want to make rest calls for multiple users , with the help of an access token. I am using 1 thread per user . The access token, that I am using , is valid for 1 hour.Once the token expires , I will get an 401 error , and have to update the token for all the threads , and continue. I am thinking of using a volatile variable which I have made static to update all the threads. My requirement , is , the moment I get to know in one of threads that the

How can I send non-static data to a thread in Rust and is it needed in this example?

回眸只為那壹抹淺笑 提交于 2019-12-31 07:29:50
问题 I am trying to fire up a new thread using some heap data in Rust and I am getting a bunch of errors that stem from the need of the data to have 'static lifetime. I've worked my way backwards up my program but hit a problem. use std::sync::Arc; use std::thread; struct ThreadData { vector_of_strings: Vec<String>, terms: Vec<&'static str>, quotient: usize, } fn perform_search(slice: &[String], terms: &[&str]) { /* ... */ } fn threaded_search(td_arc: &Arc<ThreadData>) { let no_of_lines = td_arc