Programming: Concurrency Patterns: Difference between revisions
No edit summary |
|||
Line 48: | Line 48: | ||
<blockquote> | <blockquote> | ||
Safely share an object between threads,<br> | Safely share an object between threads,<br> | ||
each method call on a proxy-object | each method call on a proxy-object enqueues an execution in a separate thread.<br> | ||
Unlike '''Monitor Object''', method can be enqueued several times (does not block on perform)<br> | Unlike '''Monitor Object''', method can be enqueued several times (does not block on perform)<br> | ||
Also unlike '''Monitor Object''', you may choose to perform tasks in a different order than they were enqueued.<br> | Also unlike '''Monitor Object''', you may choose to perform tasks in a different order than they were enqueued.<br> |
Revision as of 04:22, 7 August 2022
Resources
Wikipedia: Concurrency Patterns https://en.wikipedia.org/wiki/Concurrency_pattern
Producer/Consumer
Safely parallelized task enqueueing/execution.
- A producer adds data to a synchronized queue
- A variable number of consumers process data from a synchronized queue in a loop
- When producer is finished, send a poison pill for each worker, informing it to break/exit the loop gracefully
- Join on worker threads (wait to close)
Monitor Object
Safely share an object between multiple threads,
wait in any caller until the original perform is done (ensuring only one task is ever performed with this object ata time).
The caller does not need to know about the perform-lock, it will simply block until the monitor is able to process a new task.Say we only ever want one thread performing IO on a file at any given time
We create a WriterMonitor, which might have methods read/write
- in thread-1, we call read(), which blocks while in the background creates and performs a thread
- in thread-2, we call write(), it will automatically block until thread-1's read() call is finished, then it will take lock and write()
https://www.youtube.com/watch?v=_p-TM1x48zk
# thread-1 # # worker-thread # # thread-2 # proxy.call() >--------+ (start) | +-------< proxy.call() | (wait) (result) <--------+ | (start) | +-------> (result)
Active Object
Safely share an object between threads,
each method call on a proxy-object enqueues an execution in a separate thread.
Unlike Monitor Object, method can be enqueued several times (does not block on perform)
Also unlike Monitor Object, you may choose to perform tasks in a different order than they were enqueued.
Hides the lock/queue logic from the caller.examples:
- update a progressbar in the UI thread from a background thread
- Qt's signals/slots are an expansion of this pattern
Overview:
- Background thread instantiates a proxy object
- On a method call, the proxy returns a Future/Promise (a request is enqueued to UI thread)
- The UI thread eventually performs the task
- You may wait/join on the Future/Promise to wait for it to return it's results
https://www.youtube.com/watch?v=U9Tf7h-etl0
# bg-thread class Proxy # (ActiveObject) methods that will run in sep thread class Future # waitable object, will obtain result when performed # ui-thread class Queue # holds method requests class Scheduler # (EventLoop) chooses next method request to run