26
Multithreading Made Simple with OmniThreadLibrary Primož Gabrijelčič

Multithreading Made Simple with OmniThreadLibrary

  • Upload
    moses

  • View
    52

  • Download
    4

Embed Size (px)

DESCRIPTION

Multithreading Made Simple with OmniThreadLibrary. Primož Gabrijelčič. Introduction. OmniThreadLibrary is …. … VCL for multithreading Simplifies programming tasks Componentizes solutions Allows access to the bare metal … trying to make multithreading possible for mere mortals - PowerPoint PPT Presentation

Citation preview

Page 1: Multithreading Made Simple with OmniThreadLibrary

Multithreading Made Simple with OmniThreadLibrary

Primož Gabrijelčič

Page 2: Multithreading Made Simple with OmniThreadLibrary

Introduction

Page 3: Multithreading Made Simple with OmniThreadLibrary

OmniThreadLibrary is …

• … VCL for multithreading– Simplifies programming tasks– Componentizes solutions– Allows access to the bare metal

• … trying to make multithreading possible for mere mortals

• … providing well-tested components packed in reusable classes with high-level parallel programming support

• … parallel programming today!

Page 4: Multithreading Made Simple with OmniThreadLibrary

Project status

• OpenBSD license• Actively developed

– 1008 commits

• Actively used– 2.0: 2710 downloads [in 7 months]– 2.1: 1187 downloads [in 3 months]– 2.2: current release, XE2 support

• Delphi 2007 and above; currently Win32 only

Page 5: Multithreading Made Simple with OmniThreadLibrary

Installation

• Download last installation from the Google Code or checkout the SVN repository– code.google.com/p/omnithreadlibrary/

• Add installation folder and its src subfolder to the project search path or Win32 library path

• Add the OtlParallel unit to the uses list• That’s all folks!

Page 6: Multithreading Made Simple with OmniThreadLibrary

Future

• Win64

• FMX

• OS/X

• iOS?

Page 7: Multithreading Made Simple with OmniThreadLibrary

OmniThreadLibrary basics

Page 8: Multithreading Made Simple with OmniThreadLibrary

Task vs. thread

Page 9: Multithreading Made Simple with OmniThreadLibrary

Communication

Page 10: Multithreading Made Simple with OmniThreadLibrary

High-level multithreading

Page 11: Multithreading Made Simple with OmniThreadLibrary

Be afraid

“New programmers are drawn to multithreading

like moths to flame, with similar results.”

-Danny Thorpe

Page 12: Multithreading Made Simple with OmniThreadLibrary

Why high-level approach?

• Designing parallel solutions is hard

• Writing multithreaded code is hard

• Testing multicore applications is hard

• Debugging multithreadingcode is pure insanity

• Debugging multithreading code is hard

Page 13: Multithreading Made Simple with OmniThreadLibrary

High-level multithreading

• Async– start background task and continue

• Future– start background calculation and retrieve the result

• Join– start multiple background tasks and wait

• ParallelTask– start multiple copies of one task and wait

Page 14: Multithreading Made Simple with OmniThreadLibrary

High-level multithreading

• ForEach– parallel iteration over many different containers

• Pipeline– run a multistage process

• Fork/Join– divide and conquer, in parallel

• Delphi 2009 required

Page 15: Multithreading Made Simple with OmniThreadLibrary

Async

• Parallel.Async(code)

Async(code)

code

Async(code)

code

Page 16: Multithreading Made Simple with OmniThreadLibrary

Future

• Future:=Parallel.Future<type>. (calculation);

• Query Future.Value;Future(code)

code

.Value

Result

Future(code)

code

.Value

Result

Page 17: Multithreading Made Simple with OmniThreadLibrary

Join

• Parallel.Join([task1, task2, task3, … taskN]).Execute

Join(code1, code2, code3)

code1

code2code3

Join(code1, code2, code3)

code1

code2code3

Page 18: Multithreading Made Simple with OmniThreadLibrary

ParallelTask

• Parallel.ParallelTask.Execute (code)

ParallelTask(code)

code

codecode

ParallelTask(code)

code

codecode

Page 19: Multithreading Made Simple with OmniThreadLibrary

ParallelFor

• Parallel.ForEach(from, to).Execute( procedure (const value: integer); begin //… end)

• Parallel.ForEach(source).Execute( procedure (const value: TOmniValue); begin //… end)

Page 20: Multithreading Made Simple with OmniThreadLibrary

ParallelFor

ForEach (source, code)

source

output

code code code

Optional

ForEach (source, code)

source

output

code code code

Optional

Page 21: Multithreading Made Simple with OmniThreadLibrary

while <has data> read <data> compress <data> encrypt <data> write <data>

while <has data> read <data> into <read-buf> compress <read-buf> into <compress-buf> encrypt <compress-buf> into <encrypt-buf> write <encrypt-buf>

while <has data> read <data> insert <data> into <compress queue>

read <data-c> from <compress queue> compress <data-c> insert <data-c> into <encrypt queue>

read <data-e> from <encrypt queue> encrypt <data-e> insert <data-e> into <write queue>

read <data-w> from <write queue> write <data-w>

while <has data> read <data> insert <data> into <compress queue>

while read <compress queue> compress <data> insert <data> into <encrypt queue>

while read <encrypt queue> encrypt <data> insert <data> into <write queue>

while read <write queue> write <data>

Pipeline example

Read

Compress

Encrypt

Write

CompressQueue

EncryptQueue

WriteQueue

Page 22: Multithreading Made Simple with OmniThreadLibrary

Pipeline

• Parallel.Pipeline([stage1, stage2, stage3]). Run

Pipeline(source, stage1, stage2,

stage3)

stage1

stage2stage3

source temp1 temp2

output

Pipeline(source, stage1, stage2,

stage3)

stage1

stage2stage3

source temp1 temp2

output

Page 23: Multithreading Made Simple with OmniThreadLibrary

Fork/Join

• Divide and conquer

ForkJoin (code)

computation pool

code codecode

ForkJoin (code)

computation pool

code codecode

Page 24: Multithreading Made Simple with OmniThreadLibrary

Multithreading is hard?

Page 25: Multithreading Made Simple with OmniThreadLibrary

Why high-level approach?

• Designing parallel solutions is hard

• Writing multithreaded code is hard

• Testing multicore applications is hard

• Debugging multithreadingcode is pure insanity

• Debugging multithreading code is hard

Page 26: Multithreading Made Simple with OmniThreadLibrary

Questions?