Programming: Concurrency Patterns: Difference between revisions

From wikinotes
No edit summary
No edit summary
Line 7: Line 7:
|}
|}
</blockquote><!-- Resources -->
</blockquote><!-- Resources -->
= Monitor Object =
<blockquote>
Manages task execution within a single thread.
</blockquote><!-- Monitor -->
= Active Object =
<blockquote>
Decouple thread an object is created in from the thread the object's code will be executed in.<br>
without exposing lock/queue/etc details to the caller.
Thread/Subprocess objects are frequently exposed this way (ex: <code>QtCore.QThread</code>)
* Caller instantiates a proxy object
* On a method call, the proxy returns a Future/Promise object is created (in background, a request is enqueued)
* In background, a worker performs item from queue
* You may wait/join on the Future/Promise to wait for it to return it's results
</blockquote><!-- Active Object -->


= Producer/Consumer =
= Producer/Consumer =
<blockquote>
<blockquote>
Safely parallelized execution.
* A producer adds data to a synchronized queue
* A producer adds data to a synchronized queue
* A variable number of consumers process data from a synchronized queue in a loop
* A variable number of consumers process data from a synchronized queue in a loop

Revision as of 22:26, 6 August 2022

Resources

Wikipedia: Concurrency Patterns https://en.wikipedia.org/wiki/Concurrency_pattern

Monitor Object

Manages task execution within a single thread.


Active Object

Decouple thread an object is created in from the thread the object's code will be executed in.
without exposing lock/queue/etc details to the caller.

Thread/Subprocess objects are frequently exposed this way (ex: QtCore.QThread)

  • Caller instantiates a proxy object
  • On a method call, the proxy returns a Future/Promise object is created (in background, a request is enqueued)
  • In background, a worker performs item from queue
  • You may wait/join on the Future/Promise to wait for it to return it's results

Producer/Consumer

Safely parallelized 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)