race-condition

Race Condition in Async/Await Code

两盒软妹~` 提交于 2019-12-18 21:49:30
问题 I just wonder whether a race condition occurs in the code below: int readingFiles; async Task<string> ReadFile (string file) { ++readingFiles; var text = await Stream.ReadFileAsync(file); --readingFiles; return text; } If ReadFile method is executed by a thread pool thread, readingFiles will be accessed by two different threads and the readingFiles variable is not protected by any synchronization idioms. It means that the first update to readingFiles should not be visible to the other thread

Race-condition creating folder in Python

荒凉一梦 提交于 2019-12-18 18:52:16
问题 I have a urllib2 caching module, which sporadically crashes because of the following code: if not os.path.exists(self.cache_location): os.mkdir(self.cache_location) The problem is, by the time the second line is being executed, the folder may exist, and will error: File ".../cache.py", line 103, in __init__ os.mkdir(self.cache_location) OSError: [Errno 17] File exists: '/tmp/examplecachedir/' This is because the script is simultaneously launched numerous times, by third-party code I have no

Xcode Incorrectly Reporting Swift Access Race Condition

限于喜欢 提交于 2019-12-18 17:38:06
问题 I believe XCode is incorrectly reporting Swift Access Race in my SynchronizedDictionary - or is it? My SynchronizedDictionary looks like this: public struct SynchronizedDictionary<K: Hashable, V> { private var dictionary = [K: V]() private let queue = DispatchQueue( label: "SynchronizedDictionary", qos: DispatchQoS.userInitiated, attributes: [DispatchQueue.Attributes.concurrent] ) public subscript(key: K) -> V? { get { return queue.sync { return self.dictionary[key] } } mutating set { queue

How 'undefined' a race condition can be?

点点圈 提交于 2019-12-18 16:59:10
问题 Let's say I define a following C++ object: class AClass { public: AClass() : foo(0) {} uint32_t getFoo() { return foo; } void changeFoo() { foo = 5; } private: uint32_t foo; } aObject; The object is shared by two threads, T1 and T2. T1 is constantly calling getFoo() in a loop to obtain a number (which will be always 0 if changeFoo() was not called before). At some point, T2 calls changeFoo() to change it (without any thread synchronization). Is there any practical chance that the values ever

Can method inlining optimization cause race conditions?

≡放荡痞女 提交于 2019-12-18 12:17:41
问题 As seen in this question: Raising C# events with an extension method - is it bad? I'm thinking of using this extension method to safely raise an event: public static void SafeRaise(this EventHandler handler, object sender, EventArgs e) { if (handler != null) handler(sender, e); } But Mike Rosenblum raise this concern in Jon Skeet's answer: You guys need to add the [MethodImpl(MethodImplOptions.NoInlining)] attribute to these extension methods or else your attempt to copy the delegate to a

Does one assembler instruction always execute atomically?

百般思念 提交于 2019-12-18 11:51:34
问题 Today I came across this question: you have a code static int counter = 0; void worker() { for (int i = 1; i <= 10; i++) counter++; } If worker would be called from two different threads, what value will counter have after both of them are finished? I know that actually it could be anything. But my internal guts tells me, that counter++ will most likely be translated into single assembler instruction, and if both threads are execute on the same core, counter will be 20. But what if those

Why does ThreadSanitizer report a race with this lock-free example?

試著忘記壹切 提交于 2019-12-18 11:50:19
问题 I've boiled this down to a simple self-contained example. The main thread enqueues 1000 items, and a worker thread tries to dequeue concurrently. ThreadSanitizer complains that there's a race between the read and the write of one of the elements, even though there is an acquire-release memory barrier sequence protecting them. #include <atomic> #include <thread> #include <cassert> struct FakeQueue { int items[1000]; std::atomic<int> m_enqueueIndex; int m_dequeueIndex; FakeQueue() : m

Handling race condition in model.save()

旧街凉风 提交于 2019-12-18 02:12:42
问题 How should one handle a possible race condition in a model's save() method? For example, the following example implements a model with an ordered list of related items. When creating a new Item the current list size is used as its position. From what I can tell, this can go wrong if multiple Items are created concurrently. class OrderedList(models.Model): # .... @property def item_count(self): return self.item_set.count() class Item(models.Model): # ... name = models.CharField(max_length=100)

Private constructor to avoid race condition

╄→尐↘猪︶ㄣ 提交于 2019-12-17 23:19:57
问题 I am reading the book Java Concurrency in Practice session 4.3.5 @ThreadSafe public class SafePoint{ @GuardedBy("this") private int x,y; private SafePoint (int [] a) { this (a[0], a[1]); } public SafePoint(SafePoint p) { this (p.get()); } public SafePoint(int x, int y){ this.x = x; this.y = y; } public synchronized int[] get(){ return new int[] {x,y}; } public synchronized void set(int x, int y){ this.x = x; this.y = y; } } I am not clear where It says The private constructor exists to avoid

OpenMP multiple threads update same array

心已入冬 提交于 2019-12-17 14:54:35
问题 I have the following code in my program and I want to accelerate it using OpenMP. ... for(i=curr_index; i < curr_index + rx_size; i+=2){ int64_t tgt = rcvq[i]; int64_t src = rcvq[i+1]; if (!TEST(tgt)) { pred[tgt] = src; newq[newq_count++] = tgt; } } Currently, I have a version as follows: ... chunk = rx_sz / omp_nthreads; #pragma omp parallel for num_threads(omp_nthreads) for (ii = 0; ii < omp_nthreads; ii++) { int start = curr_index + ii * chunk; for (index = start; index < start + chunk;