Monday, October 4, 2010

Synchronizing, part 1

In BB.Sync there are a few useful classes:


  • TFuture<T>, calls a method within a thread and/or waits for the result. In TinyChess I use it to get the response from the search (here ProcessComputer is called over an infinite loop)
function TChessEngine.ProcessComputer: TMoveStatus;
var
  move: TMove;

begin
  if FTask = nil then
    FTask := TFuture<TMove>.Create(FSearch.Execute, tpHighest);

  if FTask.Available then
  begin
    move := FTask.Value;
    if move <> 0 then
    begin
      SendDataToProtocol(msMove, TMoveHelper.ToString(move));

      result := Play(move);
    end else
      result := msCheckmate;

    FreeAndNil(FTask);
  end else
    result := msNone;
end;
  • Many locks that implements the ILock interface (semaphores, mutex, SpinLocks, LockFree locks, critical sections, etc)
  • Extended thread (TThreadEx) with Java behaviour (threads can wait or awake other threads)
  • Interlocked functions (including 64 bit operations)

No comments:

Post a Comment