Code of the day:

Code of the day:

    while (conn.bExecuting) do
    begin
      sleep(10);
      Application.ProcessMessages;
    end;

What the...

Comments

  1. Alas it mostly shows how lacking the threading support in VCL is... The proper alternative to that snippet is rather heavy and complex if you only work with out of the box functionality...

    ReplyDelete
  2. conn.bExecuting... I just find that funny on several levels.

    ReplyDelete
  3. I confess I'm not 100% clear on what is going on.....

    ReplyDelete
  4. Not that complicated.  You just create a mailbox and send let the threads send telegrams to the main thread for it to pick up the mail.

    ReplyDelete
  5. The plot thickens!

    procedure TADO_WE_Connection.SetConnectionExecuting;
    begin
      // only one process can set executing at the given moment
      // until that process has done executing next process cannot even start executing.
      { TODO -oNick : This looks very suspicious }
      WaitForSingleObject(hLock, INFINITE);
      try
        while FbExecuting do sleep(1);
        FbExecuting := True;
      finally
        ReleaseMutex(hLock);
      end;
    end;

    ReplyDelete
  6. Nice! Can I get the author's autograph? ;)

    ReplyDelete
  7. Nick Hodges that code is probably used to wait for an event which is triggered within the current thread context. Processmessages is called to let events fire and Sleep is called to not consume too much CPU altough 10mS is a little bit short in that context. The "conn.bExecutin let me think it is related to communication whcih is probably interrupt driven and only triggers an event when a complete line / block : message is received. This code is perfectly correct in a cooperative multi-tasking environment.

    ReplyDelete
  8. That loop with sleep(1) is an alternative to a triggerable event, 1 is a bit low to minimize CPU usage, but if the execution is short enough, it could have been picked to minimize latency. A triggerable event is preferable for long waits, but used to have significant overhead. The mutex is used to protect the Boolean which is used as signaling flag, which is okay, the Boolean being byte-sized, assignments to it are atomic.

    So while it's possible to write a more academically correct version, that wouldn't really make the code look simpler ;-)

    ReplyDelete
  9. I fear that it could be either. And the only way to find out is to further investigate the code.

    I am also working with "interesting" multithreaded code. Specifically, to perform a sequence of tasks a thread pool is created, then each thread is activated once the previous thread terminates. Almost all of the tasks depend on the previous task(s) finishing successfully. It is very odd.

    ReplyDelete
  10. Hey... I was on codeine when I wrote that. ;).

    ReplyDelete
  11. Well, why not? Just waiting for a message, with some rather odd code!

    ReplyDelete
  12. Whenever I see bExecuting or FbExecuting or any other hungarian notation in Delphi code I reach for my gun

    ReplyDelete

Post a Comment