building multithreaded solutions with omnithreadlibrary primož gabrijelčič,...
TRANSCRIPT
![Page 2: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/2.jpg)
OmniThreadLibrary
![Page 3: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/3.jpg)
OmniThreadLibrary
• Multithreading library for Delphi 2007/2009⇨
• Task oriented• Strong messaging support– Can be used with any threading infrastructure
• High level parallel programming
![Page 4: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/4.jpg)
Project Status
![Page 5: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/5.jpg)
Today’s Topics
• Communication vs. Shared data• Tasks vs. Threads• Thread pools
![Page 6: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/6.jpg)
Tasks
![Page 7: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/7.jpg)
Task <> Thread
• Task is part of code that has to be executed
• Thread is execution environment• You take care of the task,
OTL takes care of the thread
![Page 8: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/8.jpg)
Execution Models
• CreateTask(task_procedure)• CreateTask(task_method )• CreateTask(TOmniWorker_object)• CreateTask(anonymous_procedure)
• www.thedelphigeek.com/2008/09/omnithreadlibrary-patterns-how-to-not.html
• www.thedelphigeek.com/2009/11/omnithreadlibrary-patterns-task.html
![Page 9: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/9.jpg)
Thread Pools
![Page 10: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/10.jpg)
Thread pool
• Starting up a thread takes time• Thread pool keeps threads alive and
waits for tasks• Automatic thread startup/shutdown• User code executed at thread
creation– Connection pool
• .Run ⇨ .Schedule
![Page 11: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/11.jpg)
Communication
![Page 12: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/12.jpg)
Shared Data
• Pros– Only one copy– Fast if only reading
• Cons– Locking• Bad scaling• Deadlocks, livelocks
• Keep in mind– Fine-grained is better– Optimistic locking
![Page 13: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/13.jpg)
Messaging
• Pros– No shared data - no* locking
• Cons– Hard to understand– Increased memory usage
*For sufficiently flexible definition of “No”
![Page 14: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/14.jpg)
Common Sense
• Sometimes you need both• Minimize shared data• Minimize interaction points
“If your solution depends on sharing data million times a second, you’re
doomed.” -me
![Page 15: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/15.jpg)
Messaging Solutions
• Windows messages• Pipes• Mailslots• Sockets (TCP/IP)• Shared memory + (micro)locking– Used by the OmniThreadLibrary
communication primitives
![Page 16: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/16.jpg)
Messaging in the OTL
• Bounded stack• Bounded queue• Dynamic queue
– Dynamically allocated, O(1) enqueue and dequeue– Useful for single-threaded applications too– www.thedelphigeek.com/2010/02/dynamic-lock-
free-queue-doing-it-right.html
• All are threadsafe, microlocking, and support multiple readers and writers
![Page 17: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/17.jpg)
TOmniValue
• Used in OTL messaging• Record with operators– Can contain strings, interfaces, objects …
• Faster than Variant• Much faster than TValue (but slightly
less powerfull)– www.thedelphigeek.com/2010/03/speed-
comparison-variant-tvalue-and.html
![Page 18: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/18.jpg)
Show me the code!
![Page 19: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/19.jpg)
Danger, Will Robinson!
“New programmers are drawn to multithreading
like moths to flame, with similar results.”
-Danny Thorpe
Image © Hammacher Schlemmer
![Page 20: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/20.jpg)
Be Afraid
• Be very afraid!
• Designing parallel solutions is hard• Writing multithreaded code is hard• Testing multicore apps is hard• Debugging multithreading code is
pure insanity
![Page 21: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/21.jpg)
Keep in Mind
• Don’t parallelize everything• Don’t create thousands of threads• Rethink the algorithm• Prove the improvements• Test, test and test
![Page 22: Building Multithreaded Solutions with OmniThreadLibrary Primož Gabrijelčič, primoz@gabrijelcic.org](https://reader036.vdocuments.us/reader036/viewer/2022062301/56649e4c5503460f94b41bb3/html5/thumbnails/22.jpg)
Q & A