Click here to Skip to main content
15,904,935 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Convert a value in one number range to a value in another number range? Pin
Thrash50513-Jul-08 13:42
Thrash50513-Jul-08 13:42 
GeneralRe: Convert a value in one number range to a value in another number range? Pin
Luc Pattyn13-Jul-08 14:10
sitebuilderLuc Pattyn13-Jul-08 14:10 
AnswerRe: Convert a value in one number range to a value in another number range? Pin
Nelek14-Jul-08 21:37
protectorNelek14-Jul-08 21:37 
QuestionRe: Convert a value in one number range to a value in another number range? Pin
feekejd14-Aug-08 1:43
feekejd14-Aug-08 1:43 
AnswerRe: Convert a value in one number range to a value in another number range? Pin
Roger Wright15-Jul-08 19:54
professionalRoger Wright15-Jul-08 19:54 
GeneralRe: Convert a value in one number range to a value in another number range? Pin
Nelek16-Jul-08 21:32
protectorNelek16-Jul-08 21:32 
GeneralRe: Convert a value in one number range to a value in another number range? Pin
CPallini16-Jul-08 21:50
mveCPallini16-Jul-08 21:50 
QuestionUsing CompareExchange to synthesize bigger atomic operations Pin
supercat912-Jul-08 12:54
supercat912-Jul-08 12:54 
The Threading.Interlocked.CompareExchange operation is wonderful, from both a theoretical and practical perspective. It allows for lock-free implementations of many algorithms. For example, to add something to a stack:
Sub AddToStack(X as Whatever)
  Dim OldStack as Whatever
  Do
    OldStack = TheStack.Next
    X.Next = OldStack
  Loop While Threading.Interlocked.CompareExchange(TheStack.Next, X, OldStack) <> OldStack
End Sub
Nice and simple and elegant. If no other thread messes with the stack during the execution of the above code, it runs once. If another thread messes with the stack, it may have to run again, but if will never have to wait for another thread to do something. It may have to wait for another thread to not do something, but since the only obstructive actions will be those that achieve useful progress, there is no danger of livelock.

A couple of wrinkles in the CompareExchange ointment, though:
  1. Some data structures require more than one instruction to 'commit' changes; implementations of such data structures will be more complicated.
  2. Many algorithms require that if the object being 'CompareExchange'd is changed, the CompareExchange detect it. If new objects are created and used every time something changes, the CompareExchange will detect it and there will be no problem. If objects are reused from a pool, however, it's possible that an object someone was expecting to CompareExchange will be disposed of (returned to the pool), reallocated, and put in the place of the old object that was being 'CompareExchange'd.
What is the best way to take advantage of CompareExchange while avoiding dangers like object reuse? Physically, I would think that since the Intel processors allow a CompareExchange of a 64-bit long, they should be able support a CompareExchange of a data item that consists of an object plus a 32-bit 'reuse' count; even on 64-bit platforms, a 64-bit data type should be able to hold an object plus a 'reuse' count (I can't imagine any need for more than 2^32 objects to be handled by one thread in the foreseeable future). Unfortunately, .Net doesn't support any such constructs and I know of no way they could be simulated without boxing and unboxing (which would defeat the whole purpose).

How would one best implement something like the following:
Class AtomicPair
  Public Class Pair
    Public Thing1,Thing2 as Whatever
  End Class

  Public MyPair as Pair

  Public Sub SetPair(ByVal NewThing1 as Whatever, NewThing2 as Whatever)
    Dim NewPair as New AtomicPair.Pair

    NewPair.Thing1 = NewThing1
    NewPair.Thing2 = NewThing2
    MyPair = NewPair
  End Sub

  Public Sub SetThing1(ByVal NewThing1)
    Dim NewPair as New AtomicPair.Pair
    Dim OldPair as AtomicPair.Pair

    NewPair.Thing1 = NewThing1
    Do
      OldPair = MyPair
      NewPair.Thing2 = OldPair.Thing2
    While Threading.Interlocked.CompareExchange(MyPair, NewPair, OldPair) <> OldPair
  End Sub
End Class
Assume a similar method for SetThing2. As implemeted using 'New', the above will work reliably to allow atomic updates of one or both fields of the pair. If objects were allocated from a pool instead of using New, however, the code could break. What are the best approaches to dealing with the issue?
QuestionJava Algorithms Pin
benjamin yap11-Jul-08 7:38
benjamin yap11-Jul-08 7:38 
AnswerRe: Java Algorithms Pin
Alan Balkany11-Jul-08 9:14
Alan Balkany11-Jul-08 9:14 
AnswerRe: Java Algorithms Pin
Paul Conrad11-Jul-08 10:31
professionalPaul Conrad11-Jul-08 10:31 
GeneralRe: Java Algorithms Pin
Luc Pattyn11-Jul-08 10:56
sitebuilderLuc Pattyn11-Jul-08 10:56 
GeneralRe: Java Algorithms Pin
Paul Conrad11-Jul-08 10:58
professionalPaul Conrad11-Jul-08 10:58 
QuestionRandom number generator Pin
Kwanalouie10-Jul-08 9:17
Kwanalouie10-Jul-08 9:17 
AnswerRe: Random number generator [modified] Pin
Scorch10-Jul-08 10:03
Scorch10-Jul-08 10:03 
AnswerRe: Random number generator Pin
Luc Pattyn10-Jul-08 13:08
sitebuilderLuc Pattyn10-Jul-08 13:08 
AnswerRe: Random number generator Pin
Mark Churchill10-Jul-08 14:47
Mark Churchill10-Jul-08 14:47 
AnswerRe: Random number generator Pin
cp987611-Jul-08 0:44
cp987611-Jul-08 0:44 
AnswerRe: Random number generator Pin
yassir hannoun19-Jul-08 2:42
yassir hannoun19-Jul-08 2:42 
GeneralRe: Random number generator Pin
PIEBALDconsult9-Aug-08 19:28
mvePIEBALDconsult9-Aug-08 19:28 
GeneralRe: Random number generator Pin
yassir hannoun9-Aug-08 23:39
yassir hannoun9-Aug-08 23:39 
GeneralRe: Random number generator Pin
PIEBALDconsult10-Aug-08 3:10
mvePIEBALDconsult10-Aug-08 3:10 
QuestionFormatting text to a pyramid shape Pin
KaptinKrunch9-Jul-08 10:09
KaptinKrunch9-Jul-08 10:09 
AnswerRe: Formatting text to a pyramid shape Pin
Alan Balkany11-Jul-08 9:23
Alan Balkany11-Jul-08 9:23 
QuestionInverse of NORMSDIST function Pin
sumit70343-Jul-08 18:10
sumit70343-Jul-08 18:10 

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.