Click here to Skip to main content
14,665,860 members
Home » Discussions » C#
   

C#

 
QuestionDynamic Accessibility Pin
DaveyM6931-Jul-08 0:12
professionalDaveyM6931-Jul-08 0:12 
QuestionShare point problem Pin
manju#12330-Jul-08 23:45
Membermanju#12330-Jul-08 23:45 
GeneralRe: Share point problem Pin
Simon P Stevens31-Jul-08 0:06
MemberSimon P Stevens31-Jul-08 0:06 
GeneralRe: Share point problem Pin
manju#12331-Jul-08 0:31
Membermanju#12331-Jul-08 0:31 
QuestionRecord Audio from Internet Explorer Pin
Malenko30-Jul-08 23:28
MemberMalenko30-Jul-08 23:28 
QuestionA quick question about volatile Pin
Megidolaon30-Jul-08 23:27
MemberMegidolaon30-Jul-08 23:27 
AnswerRe: A quick question about volatile Pin
Guffa30-Jul-08 23:44
MemberGuffa30-Jul-08 23:44 
AnswerRe: A quick question about volatile Pin
Mike Dimmick31-Jul-08 0:13
MemberMike Dimmick31-Jul-08 0:13 
Yes, normally the JIT compiler can generate code that reads a value from memory into a register, then reuse that register everywhere without referring back to the original memory location.

However, even generating code that loads the memory location into a register every time may not help. The reason is that modern processors have a hierarchy of caches, and typically have a buffer of pending writes so it can combine small writes into one larger transaction. This is because the system memory has quite a lot of latency to open one memory location, but once it's open, a small number of subsequent locations can be accessed quite quickly. The overhead to keep all the caches always synchronized would be quite extensive, so it isn't done.

The result is that the processor will just read the value from its own cache rather than go back to main memory, unless you explicitly tell it that it must go back to main memory, and at the same time to tell other processors in the system to flush this location from their write buffer. This is referred to as a 'memory fence' or memory barrier[^].

Using the volatile keyword in C# will cause acquire and release barriers to be generated appropriately. In C and C++ the semantics only suppress compiler optimizations; they do not generate memory fences. The JIT compiler will generate the right code for the type you're using.

On x86 processors, since the Pentium, variables up to 64 bits in size that are aligned correctly (which they will be, by the JIT compiler, unless you use StructLayout attributes to explicitly set the alignment) are always read atomically, so you don't need to worry about partial reads of long variables. Pentium Pro and newer processors will perform unaligned reads atomically as long as it remains within a single cache line (the unit of access to cacheable areas of main memory).

For objects, volatile applies to the object reference, not the object itself. You cannot apply volatile to a user-defined structure.

See also Raymond Chen's article High-performance multithreading is very hard[^]. In general, stick to Monitor or the lock keyword (which just calls Monitor.Enter and Monitor.Exit for you) until you can prove that the lock is too much of a bottleneck.


DoEvents: Generating unexpected recursion since 1991

AnswerRe: A quick question about volatile Pin
Megidolaon31-Jul-08 1:11
MemberMegidolaon31-Jul-08 1:11 
GeneralRe: A quick question about volatile Pin
Mike Dimmick31-Jul-08 1:32
MemberMike Dimmick31-Jul-08 1:32 
GeneralRe: A quick question about volatile Pin
Guffa31-Jul-08 1:37
MemberGuffa31-Jul-08 1:37 
AnswerRe: A quick question about volatile Pin
Megidolaon31-Jul-08 1:59
MemberMegidolaon31-Jul-08 1:59 
QuestionHow to insert a new row to MDB file [modified] Pin
Admin88730-Jul-08 21:53
MemberAdmin88730-Jul-08 21:53 
AnswerRe: How to insert a new row to MDB file Pin
leppie30-Jul-08 22:41
Memberleppie30-Jul-08 22:41 
AnswerRe: How to insert a new row to MDB file Pin
rah_sin30-Jul-08 22:42
professionalrah_sin30-Jul-08 22:42 
QuestionSetup with Custom Action Pin
Sifar - 030-Jul-08 21:45
MemberSifar - 030-Jul-08 21:45 
AnswerRe: Setup with Custom Action Pin
leppie30-Jul-08 22:43
Memberleppie30-Jul-08 22:43 
AnswerRe: Setup with Custom Action Pin
Frank Horn30-Jul-08 23:17
MemberFrank Horn30-Jul-08 23:17 
Questionproblem_details to solve the warning Pin
ktamanna30-Jul-08 21:42
Memberktamanna30-Jul-08 21:42 
AnswerRe: problem_details to solve the warning Pin
leppie30-Jul-08 22:44
Memberleppie30-Jul-08 22:44 
QuestionHow to resize the form at run time on load Pin
tasumisra30-Jul-08 21:32
Membertasumisra30-Jul-08 21:32 
AnswerRe: How to resize the form at run time on load Pin
Syed Shahid Hussain30-Jul-08 22:32
MemberSyed Shahid Hussain30-Jul-08 22:32 
GeneralRe: How to resize the form at run time on load Pin
tasumisra30-Jul-08 23:04
Membertasumisra30-Jul-08 23:04 
GeneralRe: How to resize the form at run time on load Pin
Syed Shahid Hussain31-Jul-08 17:39
MemberSyed Shahid Hussain31-Jul-08 17:39 
GeneralRe: How to resize the form at run time on load Pin
tasumisra31-Jul-08 23:36
Membertasumisra31-Jul-08 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.