delphi零碎笔记01
001、uses System.SyncObjs; //同步对象
TEvent = class(THandleObject)
002、uses System.Threading;
//线程、线程池、线程池中线程的安全共享标记、线程池的状态、
//任务、池化的任务
[HPPGEN(HPPGenAttribute.mkFriend, 'DELPHICLASS TTask; /* __interface */ ITask; DELPHICLASS TAbstractTask')]
TThreadPool = class sealed
public type
IControlFlag = interface(IInterface)
function Increment: Integer;
end;
//...::::::线程池
TSafeSharedCardinal = record
//...线程池中线程的安全共享标记
public
constructor Create(var SharedVar: Cardinal);
function Increment: Cardinal; inline;
function Decrement: Cardinal; inline;
class operator Explicit(Value: TSafeSharedCardinal): Cardinal; inline;
property Value: Cardinal read GetCardinal write SetCardinal;
end;
public //线程池公共属性、方法、函数::::::
constructor Create;
destructor Destroy; override;
class function NewControlFlag: IControlFlag; static;
procedure QueueWorkItem(Sender: TObject; WorkerEvent: TNotifyEvent; const AControlFlag: IControlFlag = nil); overload;
procedure QueueWorkItem(const WorkerEvent: TProc; const AControlFlag: IControlFlag = nil); overload;
// Returns false if attempting to set a value smaller than the number of processors. Setting this
// value too high can lead problems with other libraries and processes. If too many threads execute
// at the same time, task/thread switching overhead can become restrictive.
function SetMaxWorkerThreads(Value: Integer): Boolean;
// Returns false if attempting to set a value < 0 or > MaxWorkerThreads. The actual number of pool
// threads could be less than this value depending on actual demand. Setting this to too few threads could
// be less than optimal resource utilization.
function SetMinWorkerThreads(Value: Integer): Boolean;
property MaxWorkerThreads: Integer read GetMaxWorkerThreads;
property MinWorkerThreads: Integer read GetMinWorkerThreads;
/// <summary>
/// The default global threadpool.
/// </summary>
class property Default: TThreadPool read FDefaultPool;
/// <summary>
/// This will be the threadpool when accessed from a thread within that threadpool. For the main-thread or
/// non-threadpool threads, this will be the same as the Default property above.
/// </summary>
class property Current: TThreadPool read GetCurrentThreadPool;
end;
TThreadPoolStats = record
//...线程池的状态::
public
/// <summary>Total number of worker threads within the thread pool</summary>
property WorkerThreadCount: Integer read FWorkerThreadCount;
/// <summary>Corresponds to the MinWorkerThreads property</summary>
property MinLimitWorkerThreadCount: Integer read FMinLimitWorkerThreadCount;
/// <summary>Corresponds to the MaxWorkerThreads property</summary>
property MaxLimitWorkerThreadCount: Integer read FMaxLimitWorkerThreadCount;
/// <summary>Number of threads waiting for work to do</summary>
property IdleWorkerThreadCount: Integer read FIdleWorkerThreadCount;
/// <summary>Number of global queued work requests</summary>
property QueuedRequestCount: Integer read FQueuedRequestCount;
/// <summary>Number of worker threads in the process of being retired</summary>
property RetiredWorkerThreadCount: Integer read FRetiredWorkerThreadCount;
/// <summary>Running average of CPU usage</summary>
property AverageCPUUsage: Integer read FAverageCPUUsage;
/// <summary>Current snapshot of CPU usage</summary>
property CurrentCPUUsage: Integer read FCurrentCPUUsage;
/// <summary>Shared value among worker threads to manage orderly thread suspension</summary>
property ThreadSuspended: Integer read FThreadSuspended;
/// <summary>Shared value among worker threads to manage orderly thread suspension</summary>
property LastSuspendTick: Cardinal read FLastSuspendTick;
/// <summary>Used by the monitor thread to keep from creating threads too quickly</summary>
property LastThreadCreationTick: Cardinal read FLastThreadCreationTick;
/// <summary>Used by the monitor thread to only create new threads when there is work to do</summary>
property LastQueuedRequestCount: Integer read FLastQueuedRequestCount;
/// <summary>
/// When called from within a threadpool thread, returns the stats for the owning threadpool. Outside of a
/// threadpool thread, this will return the stats for the default threadpool
/// </summary>
class property Current: TThreadPoolStats read GetCurrentThreadPoolStats;
/// <summary>Returns the stats for the default threadpool</summary>
class property Default: TThreadPoolStats read GetDefaultThreadPoolStats;
end; //::线程池的状态
010、Timeout: TTimeSpan //时间截的最完整的record记录类型:内含时间记号(滴答)Tick:
public const
TicksPerMillisecond = 10000;
TicksPerSecond = 1000 * Int64(TicksPerMillisecond);
TicksPerMinute = 60 * Int64(TicksPerSecond);
TicksPerHour = 60 * Int64(TicksPerMinute);
TicksPerDay = 24 * TIcksPerHour;
//及很丰富的属性、函数和过程
011、Timeout: Cardinal = INFINITE //const INFINITE = Cardinal($FFFFFFFF); //无符号最大整数:应用:一直等下去://ITask.wait(INFINITE);
012、TThread.GetTickCount
class function TThread.GetTickCount: Cardinal;
{$IF Defined(MSWINDOWS)}
begin
Result := Winapi.Windows.GetTickCount;
end;
{$ELSEIF Defined(MACOS)}
begin
Result := AbsoluteToNanoseconds(mach_absolute_time) div 1000000;
end;
{$ELSEIF Defined(POSIX)}
var
res: timespec;
begin
clock_gettime(CLOCK_MONOTONIC, @res);
Result := (Int64(1000000000) * res.tv_sec + res.tv_nsec) div 1000000;
end;
{$ELSE OTHERPLATFORM}
{$MESSAGE Fatal 'Method not implemented for Platform'}
{$ENDIF OTHERPLATFORM}
013、TMarshaller
type
// High-level aid for marshalling arguments to and from OS / native API invocation.操作系统高级别助手:集结编组参数和OS /原生API的调用请求。
// Auto-destruction to ease use. 轻松使用”自动解构“。
TMarshaller = record
// .....
// Complete all pending operations. This:
// * un-fixes any fixed arrays or strings
// * copies back any out or in/out paramaters
// * frees any memory allocated for copied data
// When the marshaller record goes out of scope, Flush is called automatically.
/ /完成所有的等待/期待的操作。它会:
/ / * un-fixes任何固定数组或字符串
/ / * 产生任何内存进出参数的副本
/ / * 释放分配的内存复制数据
/ / 当marshaller集结编组记录超出范围时,Flush自动清洗被自动调用:
procedure Flush; //:但会增加CPU或GPU的计时
// 用法:
var
LMarshaller:TMarshaller;
// ......
LMarshaller.Flush; // :操作系统高级别:清理内存分配、回写任何进出参数、清零数组和strings //:但会增加CPU或GPU的计时