Click here to Skip to main content
15,901,205 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
supercat928-Jul-08 5:50
supercat928-Jul-08 5:50 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
Luc Pattyn28-Jul-08 7:00
sitebuilderLuc Pattyn28-Jul-08 7:00 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
supercat928-Jul-08 9:37
supercat928-Jul-08 9:37 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
led mike28-Jul-08 9:53
led mike28-Jul-08 9:53 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
Luc Pattyn28-Jul-08 10:09
sitebuilderLuc Pattyn28-Jul-08 10:09 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
supercat928-Jul-08 15:20
supercat928-Jul-08 15:20 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
Luc Pattyn28-Jul-08 16:05
sitebuilderLuc Pattyn28-Jul-08 16:05 
GeneralRe: Any good way to update an object reference plus something else in a single CompareExchange? Pin
supercat928-Jul-08 15:54
supercat928-Jul-08 15:54 
Mike Dimmick wrote:
32-bit processors support 32-bit interlocked operations; 64-bit processors in 64-bit mode support 64-bit interlocked operations and that's it.


According to Wikipedia, the IA64 architecture supports 128-bit interlocked operations, so code which assumed the existence of an interlocked CompareExchange twice as wide as an object reference should be workable on all .net machines.

The DDJ article was a bit disappointing, since it failed to mention a general effective use of CompareExchange:
  1. Create a new object.
  2. Begin loop:
    1. Latch old object reference
    2. Copy old object to new one, making any desired changes
    3. Use CompareExchange to store a reference to the new object in the old spot, if the old spot hasn't changed
  3. Loop until the CompareExchange succeeds.
Note that unlike a 'normal' spinlock, provided the time between latching the old reference and attempting to write the new one was short, the CompareExchange spinlock will seldom fail more than once per active CPU trying to write the variable. It never has to wait for another thread to do anything--merely for a chance to do its own thing without other threads disrupting it.

That general approach to CompareExchange works very nicely in a wide variety of situations. The two biggest weakness: (1) It's only practical for that are small enough to be copied quickly; the longer it takes to copy an object, the more likely the CompareExchange will be to fail and force a re-loop. (2) Allowing the reuse of objects from a pool opens up major complications; each operation thus requires getting a new object from the garbage collector, creating a performance bottleneck.

Adding support for CompareExchange on an 'Object-plus' structure would make it easier to allow safe reuse of objects from a pool. If each object kept a counter of how many times it had been reused and a copy of the counter were stored along with the object reference, that would eliminate the danger that a thread could attempt to use CompareExchange on an object which another thread had discarded (put in its pool for reuse) and reallocated.
Questionrounding to five decimal digits Pin
CICCIOLO6924-Jul-08 22:50
CICCIOLO6924-Jul-08 22:50 
Answercross-post Pin
Luc Pattyn25-Jul-08 5:18
sitebuilderLuc Pattyn25-Jul-08 5:18 
Answer[Cross Post]Re: rounding to five decimal digits (with link to original message) Pin
Scott Dorman25-Jul-08 5:21
professionalScott Dorman25-Jul-08 5:21 
AnswerRe: rounding to five decimal digits Pin
ghle26-Jul-08 5:47
ghle26-Jul-08 5:47 
QuestionWindows Service : More than one user Service may run within the same process Pin
Krugger40424-Jul-08 21:48
Krugger40424-Jul-08 21:48 
Answer[Cross Post]Re: Windows Service : More than one user Service may run within the same process Pin
Scott Dorman25-Jul-08 4:18
professionalScott Dorman25-Jul-08 4:18 
GeneralRe: [Cross Post]Re: Windows Service : More than one user Service may run within the same process Pin
Krugger40425-Jul-08 11:01
Krugger40425-Jul-08 11:01 
GeneralRe: [Cross Post]Re: Windows Service : More than one user Service may run within the same process Pin
Scott Dorman25-Jul-08 14:15
professionalScott Dorman25-Jul-08 14:15 
QuestionPlease Clear my doubt !! Pin
guriqbal8724-Jul-08 2:37
guriqbal8724-Jul-08 2:37 
AnswerRe: Please Clear my doubt !! Pin
Luc Pattyn24-Jul-08 3:09
sitebuilderLuc Pattyn24-Jul-08 3:09 
GeneralRe: Please Clear my doubt !! Pin
guriqbal8724-Jul-08 3:27
guriqbal8724-Jul-08 3:27 
Questioninject code in .net exe files Pin
zon_cpp22-Jul-08 20:15
zon_cpp22-Jul-08 20:15 
AnswerRe: inject code in .net exe files Pin
leppie24-Jul-08 4:36
leppie24-Jul-08 4:36 
QuestionTargetting .NET 2.0 with Visual Studio 2008? Pin
Rob Caldecott22-Jul-08 5:10
Rob Caldecott22-Jul-08 5:10 
AnswerRe: Targetting .NET 2.0 with Visual Studio 2008? Pin
Mark Salsbery22-Jul-08 5:49
Mark Salsbery22-Jul-08 5:49 
AnswerRe: Targetting .NET 2.0 with Visual Studio 2008? Pin
Paul Conrad25-Jul-08 16:38
professionalPaul Conrad25-Jul-08 16:38 
Questionwhere is Exactly used Delegate Pin
Shaik Haneef21-Jul-08 20:25
Shaik Haneef21-Jul-08 20:25 

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.