How to create a thread/Task with a continuous loop?

Something like this would work: var cancellationTokenSource = new CancellationTokenSource(); var task = Repeat.Interval( TimeSpan.FromSeconds(15), () => CheckDatabaseForNewReports(), cancellationTokenSource.Token); The Repeat class looks like this: internal static class Repeat { public static Task Interval( TimeSpan pollInterval, Action action, CancellationToken token) { // We don’t use Observable.Interval: // If we block, the values start bunching up … Read more

Thread safe instantiation of a singleton

Make sure you read the discussion on this question/answer, too. Why should we separate alloc and init calls to avoid deadlocks in Objective-C? To expand on the race condition issue; the real fix is to not have indeterminate initialization within your application. Indeterminate or lazy initialization results in behavior that can easily change due to … Read more

Redirecting stdout and stderr to a PyQt4 QTextEdit from a secondary thread

Firstly, +1 for realising how thread-unsafe many of the examples on stack overflow are! The solution is to use a thread-safe object (like a Python Queue.Queue) to mediate the transfer of information. I’ve attached some sample code below which redirects stdout to a Python Queue. This Queue is read by a QThread, which emits the … Read more

Is Ruby’s stdlib Logger class thread-safe?

A quick look at logger.rb reveals code such as the following: def write(message) @mutex.synchronize do if @shift_age and @dev.respond_to?(:stat) begin check_shift_log rescue raise Logger::ShiftingError.new(“Shifting failed. #{$!}”) end end @dev.write(message) end end So while I can’t vouch for whether it gets thread-safety correct, I can confirm that it is making a concerted effort to do it … Read more

To what degree does std::shared_ptr ensure thread-safety?

As others have pointed out, you’ve got it figured out correctly regarding your original 3 questions. But the ending part of your edit Calling reset() in thread IV will delete previous instance of A class created in first thread and replace it with new instance? Moreover after calling reset() in IV thread other threads will … Read more

AtomicInteger and volatile [duplicate]

I believe that Atomic* actually gives both atomicity and volatility. So when you call (say) AtomicInteger.get(), you’re guaranteed to get the latest value. This is documented in the java.util.concurrent.atomic package documentation: The memory effects for accesses and updates of atomics generally follow the rules for volatiles, as stated in section 17.4 of The Java™ Language … Read more

Which is threadsafe atomic or non atomic?

For ObjC Properties — Neither are thread safe. Atomic is more resistant to threading errors. Overall, it is a curious default. The scenarios you would favor atomic for are very few. Atomic can increase the probability of correctness, but it’s at too low a level be considered a substitute for a proper locking mechanism. Therefore, … Read more

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