I'm just wondering if the race condition arises in the code below:
int readingFiles; async Task<string> ReadFile (string file) { ++readingFiles; var text = await Stream.ReadFileAsync(file); --readingFiles; return text; }
If the ReadFile method is executed by a thread pool thread, readFiles will be available in two different threads, and the readFiles variable will not be protected by any synchronization idioms.
This means that the first readFiles update should not be visible to another thread executing "-readingFiles". However, I NEVER saw that readFiles is -1 after "--readingFiles". I check if the same thread performs ++ and - operations with Thread.CurrentThread. In most cases, this is not the same thread, and I still don't see readFiles as -1.
Despite the fact that there is a race condition, and reading Files is not mutable, why do not I see the effect of this race condition?
multithreading c # asynchronous race-condition async-await
Tom K.
source share