简体   繁体   中英

Can I make a Static variable Volatile in c# in Async-await architecture ?. What are the pros and cons?

I am developing a network application in Winform c# 6.0. The application using async await architecture. In some places I am accessing static public variables by another classes without creating objects. I want to know if there is any drawback of declaring static variables volatile or not.

Thanks in advance.

Why we use volatile

Fast memory is expensive. RAM is acceptably fast, but compared to the memory used inside the CPU even it is slow. Modern Multicore CPU's have layers internal caching memory, that get faster the closer to a single core you get.

With Multithreading applications it becomes an issue propagating a single change all the way from a L1 cache to L3, and then back upwards into all other L1 caches. So you could run into issues like the L1 chache of some cores having gotten the update, but others still being on a previous value. A race condition on hardware level.

There is also the part where the Compiler, JiT and the Runtime can decide do add and remove temproary variables if it is deemed advantageous for performance. Like pruning 2+ accesses of the same index, to 1 access that copies to a temporary variable. Or cutting a temporary variable out as "underused". So you end up unpredictable stale values all over the place.

VOLTAILE turns all that off. So you will not get additional sources of race conditons from the hardware, JiT, runtime or any similar source. You still have all the normal danger of race conditons however, so it is not Thread Safety - merely a part of it.

While you do not need it with async/await

async/await is designed as a threadless way to implement multitasking. So async/await will not even run into the issues volatile is trying to fix.

For a long time "throwing threads at it" was the most expedient way to get Multitasking done in just about every language. Multi threading is only stricly nessesrary with CPU bound works, but it was a quick, dirty and reliable way to get Multtiasking done. The code for doing multiasking in a single thread was verbose, complex and incredibly easy for a human programmer to mess up. async/await has the compiler and runtime take care of the plumbing for you, the same way they deal with memory management for you. With those two (and compatriots in other languages) we can finally stop overeusing multithreading to get multitasking done.

Avoid static missuse

static has often been used as a easy way to exchange data. My advise is to only use static from compile (const) and runtime (static readonly) constants.

The closest I ever got to exchange data with it, was using a class that needs instantiation and assigning a instance to a static field. With code being given the instance via a argument, never even needing access to the static field directly.

That way at least I can have multiple instances, exchange implementations and do other stuff that direct access to a static field would never allow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM