The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

    20140508-Delphi-2007--Project-Options--Cannot-Edit-Application-Title-HelpFile-Icon-Theming

    20140430-Fiddler-Filter-Actions-Button-Run-Filterset-now

    20140424-Windows-7-free-disk-space

    More Photos
  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,776 other followers

An exponential back-off implementation I used somewhere; probably room for improvement, but it works good enough.

Posted by jpluimers on 2017/01/17

I will probably need this again somewhere in the future: An exponential back-off implementation I used somewhere; probably room for improvement, but it works good enough.

It’s Delphi, but I’ve not seen practical implementations in C# either.

(the updated version thanks to Anders Melander).

–jeroen

5 Responses to “An exponential back-off implementation I used somewhere; probably room for improvement, but it works good enough.”

  1. bugcheck said

    Nope; Wrong on both accounts.

    I’m just attempting to acquire the lock before the test for time out instead of the reverse. The result is the same.
    And I’m doubling the wait time using simple integer addition instead of using a floating point math function.

    If you can’t grok the code you can easily run both versions in your head to verify. It’s not that many iterations: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048->exit = 11 attempts

    • Kevin G. McCoy said

      Another way to do it is to left-shift the sleep time by one bit during each iteration.

  2. David Moorhouse said

    Nice work. BugCheck’s simplification omits the exponential part (simple linear progression instead) and omits the final call to TryGetLocationLock once the maximum wait time has been exceeded.

  3. bugcheck said

    Impressive piece of overengineering.

    function TryGetLocationLockWithExponentialBackOff: Boolean;
    const
    cBase = 2;
    cMaxWaitMilliSeconds = 1500;
    var
    lWaitMilliSeconds: integer;
    begin
    Result := True;
    lWaitMilliSeconds := cBase;
    while (not TryGetLocationLock) do
    begin
    if (lWaitMilliSeconds > cMaxWaitMilliSeconds) then
    Exit(False);
    Sleep(lWaitMilliSeconds);
    Inc(lWaitMilliSeconds, lWaitMilliSeconds);
    end;
    end;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: