Presentation is loading. Please wait.

Presentation is loading. Please wait.

More! Multi-threaded Development

Similar presentations


Presentation on theme: "More! Multi-threaded Development"— Presentation transcript:

1 More! Multi-threaded Development
Jarrod Hollingworth ADUG Melbourne, June 2018

2 Overview Threads Delphi Parallel Programming Library Thread Safety
Challenges & Tips How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

3 Threads >= 1 per process Different CPUs in same process
Share process resources (heap memory, globals, file handles) Own stack, program counter, registers Controlled by OS (time slice/wait, paused, resumed) Switched at any time Low level details are important

4 Parallel Programming Library
XE7+, cross-platform System.Threading Based on TThread TParallel For loop, demo Basic thread safety Bonus demo: Conway’s game of life TTask Running tasks, demo: Parallel Squares Waiting for task, demo Joins, futures, demo Lesson descriptions should be brief.

5 TParallel class function For(
LowInclusive: Integer; HighInclusive: Integer; IteratorEvent: TProc<Integer, TLoopState>; Sender: TObject; Stride: Integer; Pool: TThreadPool): TLoopResult; (30 overloads: optional parameters, anonymous methods/procedure of object, …) Iterations are run in parallel. Returns once all iterations are complete. TLoopState procedure Stop; procedure Break; property Stopped: Boolean; property Faulted: Boolean; TLoopResult property Completed: Boolean; property LowestBreakIteration: Integer; class function Join(const Procs: array of TProc; Pool: TThreadPool): ITask; (several overloads) Procedures are run in parallel. Returns once all procedures are complete.

6 x86 Assembly: total := total + 1;
CPU1: CPU2: mov edx,[eax+$0c] total inc edx mov [eax+$0c],edx total mov edx,[eax+$0c] total inc edx mov [eax+$0c],edx total

7 x86 µOperations: Inc(total);
ASM: inc dword ptr [eax] µOp.load temp_reg, [eax] µOp.inc temp_reg µOp.store [eax], temp_reg Total  total CPU:

8 x86 µOperations: Inc(total);
ASM: inc dword ptr [eax] µOp.load temp_reg, [eax] µOp.inc temp_reg µOp.store [eax], temp_reg CPU1: CPU2: µOp.load temp_reg, [eax] µOp.inc temp_reg µOp.store [eax], temp_reg

9 TTask TTask class function Create/Run(Sender: TObject; Proc: TProc; APool: TThreadPool): ITask; class function WaitForAll(Tasks: array of ITask; Timeout: LongWord): Boolean; class function WaitForAny(Tasks: array of ITask; Timeout: LongWord): Integer; class function Future<T>(Sender: TObject; Func: TFunc<T>): IFuture<T>; ITask function Start: ITask; property Status: TTaskStatus; function Wait(Timeout: LongWord): Boolean; procedure Cancel; TTaskStatus Created, WaitingToRun, Running, Completed, WaitingForChildren, Canceled, Exception IFuture<T> property Value: T

10 TThreadPool TParallel.For/Join(…, ThreadPool: ThreadPool) TTask.Create/Run(…, ThreadPool: ThreadPool) TThreadPool is self-tuning based on # CPU cores and load Cannot free until all tasks are complete SetMaxWorkerThreads(Value: Integer) >= # CPU cores SetMinWorkerThreads(Value : Integer) <= MaxWorkerThreads Note: No guarantees. For total control use TThread.

11 Thread Safety Thread local variables (global storage) threadvar class threadvar Co-ordination & communication between threads (SyncObjs) TThread.Synchronize (immediate) TThread.Queue (when main thread idle) (SyncObjs) TEvent.WaitFor(Timeout)/SetEvent/ResetEvent (Windows) PostMessage/SendMessage(Handle) (Windows) PostThreadMessage(ThreadId) Shared flag/data/queue, shared memory (inter-process), … Example objectives At the end of this lesson, you will be able to: Save files to the team Web server. Move files to different locations on the team Web server. Share files on the team Web server.

12 Thread Safety cont. Locking shared data/resource
(SyncObjs) TCriticalSection.Acquire/Release/TryEnter  Fast (System) TMonitor.Enter(Obj, Timeout)/Exit/TryEnter: Boolean  Fast-ish, timeout (SysUtils) TMultiReadExclusiveWriteSynchronizer  Faster BeginRead/EndRead/BeginWrite/EndWrite (SyncObjs) TSemaphore  Slow, inter-process Create(InitialCount, AMaximumCount: Integer; Name: string) Acquire/Release(Count) (SyncObjs) TMutex.Create(Name: string)/Acquire/Release  Slow, inter-process Atomic operations (SyncObjs) TInterlocked.Increment/Decrement/Add/Exchange/… (RTL) AtomicIncrement/… (Windows) InterlockedIncrement… Example objectives At the end of this lesson, you will be able to: Save files to the team Web server. Move files to different locations on the team Web server. Share files on the team Web server.

13 Challenges & Tips Using the VCL/FMX & RTL
Performance (different constraints, self tuning) Deadlocks (multiple locks, non re-entrant locks, use timeouts) Unhandled exceptions Testing (every computer/device and every run will be different) Stopping (or not) threads (notify, auto free, TerminateThread) Debugging (thread status, naming threads, switching threads) TThread.NameThreadForDebugging(Name: string)

14 Simple communication design
Main thread Data (locked) Manager thread Job list UI Update Dispatch Synchronize/Queue/PostMessage Worker thread Worker thread Worker thread

15 Resources Book: Delphi High Performance - Primož Gabrijelčič
Wiki: Introduction video: Advanced techniques: MartinHarvey1.1/ToC.html Demos: Conway’s Life (TParallel.For) Pascal/RTL/Parallel%20Library/ Delphi Academy (Parallel Squares, TTask, Future)


Download ppt "More! Multi-threaded Development"

Similar presentations


Ads by Google