Click here to Skip to main content
15,891,950 members
Home / Discussions / C#
   

C#

 
QuestionDataset Update Pin
Stefano Negro16-May-09 10:03
Stefano Negro16-May-09 10:03 
QuestionMulti-threading using AutoResetEvent [modified] Pin
steve_rm16-May-09 9:28
steve_rm16-May-09 9:28 
AnswerRe: Multi-threading using AutoResetEvent Pin
S. Senthil Kumar16-May-09 9:39
S. Senthil Kumar16-May-09 9:39 
AnswerRe: Multi-threading using AutoResetEvent Pin
steve_rm16-May-09 17:24
steve_rm16-May-09 17:24 
GeneralRe: Multi-threading using AutoResetEvent Pin
Joe Woodbury16-May-09 18:21
professionalJoe Woodbury16-May-09 18:21 
GeneralRe: Multi-threading using AutoResetEvent Pin
steve_rm16-May-09 23:53
steve_rm16-May-09 23:53 
GeneralRe: Multi-threading using AutoResetEvent Pin
S. Senthil Kumar17-May-09 1:26
S. Senthil Kumar17-May-09 1:26 
GeneralRe: Multi-threading using AutoResetEvent Pin
Daniel Grunwald17-May-09 1:30
Daniel Grunwald17-May-09 1:30 
"atomic" means the value will get set at once - other threads reading the value at the same time will either see the old value or the new value, nothing "in between".
Reading/writings strings and ints is also atomic.
However, reading/writing longs (or other value types >32 bit) is NOT atomic: you might end up reading 32 bits from the old value and the other 32 bits from the new value.

But even though it's atomic, I'd suggest that you still put a lock around it.

For example, in this code:
Initialization:
int a = 0, b = 0, c = 0;

Thread 1:
a = 1;
if (b == 0) c++;

Thread 2:
b = 1;
if (a == 0) c++;

From a simple look at the code, it seems that c will be 0 or 1.
c shouldn't be 2 because "c++" cannot run on both threads - it's incremented only if the other thread hasn't set its flag (a or b) yet.
Writes and reads to a,b are atomic. The increment of c is not atomic (read and write of c are atomic, but the combination read-increment-write isn't atomic).

But actually, it is possible for both "c++" to execute, so c could become 2! (though only on dual-core machines)
It's also possible that both "c++" execute but c still becomes only 1.

Both "c++" can execute because x86 CPUs are allowed to move the "read"-instruction from the if-condition above the write-instruction from the assignment above (or they might execute the read and write in parallel). I've tested this myself using hand-written assembler code, an Intel Core Duo WILL move reads above writes where possible!
You'll have to insert a memory barrier instruction to prevent the CPU from doing that.
In C#, the "volatile" keyword can be used for some kinds of memory barriers: volatile writes have release semantics, volatile reads have acquire semantics. That's sufficient for many cases of unsynchronized access to variables, but it doesn't help in the example I gave above - we need a full memory barrier there.

->
If you access shared variables without locks, might have to insert a memory barriers to synchronize the memory between CPUs. It's extremely hard to get those right.
So I would suggest that you keep it simple and always use a lock.
AnswerRe: Multi-threading using AutoResetEvent Pin
Luc Pattyn16-May-09 10:00
sitebuilderLuc Pattyn16-May-09 10:00 
Questionfind file in web Pin
michaelgr116-May-09 4:17
michaelgr116-May-09 4:17 
AnswerRe: find file in web Pin
Roland Szigeti16-May-09 4:23
Roland Szigeti16-May-09 4:23 
GeneralRe: find file in web Pin
michaelgr116-May-09 4:32
michaelgr116-May-09 4:32 
GeneralRe: find file in web Pin
Roland Szigeti16-May-09 4:34
Roland Szigeti16-May-09 4:34 
GeneralRe: find file in web Pin
michaelgr116-May-09 4:36
michaelgr116-May-09 4:36 
GeneralRe: find file in web Pin
harold aptroot16-May-09 6:16
harold aptroot16-May-09 6:16 
GeneralRe: find file in web Pin
Manas Bhardwaj16-May-09 6:17
professionalManas Bhardwaj16-May-09 6:17 
GeneralRe: find file in web Pin
michaelgr116-May-09 6:35
michaelgr116-May-09 6:35 
AnswerRe: find file in web Pin
MumbleB16-May-09 6:45
MumbleB16-May-09 6:45 
GeneralRe: find file in web Pin
michaelgr116-May-09 7:09
michaelgr116-May-09 7:09 
GeneralRe: find file in web Pin
michaelgr116-May-09 8:28
michaelgr116-May-09 8:28 
AnswerRe: find file in web Pin
S. Senthil Kumar16-May-09 9:11
S. Senthil Kumar16-May-09 9:11 
QuestionC++ virtual destructor in C#? Why not? Pin
devvvy16-May-09 2:23
devvvy16-May-09 2:23 
AnswerRe: C++ virtual destructor in C#? Why not? Pin
S. Senthil Kumar16-May-09 4:45
S. Senthil Kumar16-May-09 4:45 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
devvvy16-May-09 5:37
devvvy16-May-09 5:37 
GeneralRe: C++ virtual destructor in C#? Why not? Pin
S. Senthil Kumar16-May-09 9:01
S. Senthil Kumar16-May-09 9:01 

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.