Posts

Showing posts from August, 2017

C++11 Multi-threaded Programming: Task-Queue Patterns - Part 2

Image
Introduction In the previous post , we discussed three task-queue patterns using thread synchronization primitives introduces in C++11. In this post, we will implement these patterns using  C++11 std:: async features. Here is the recap of the previous post: Single Producer and Single Consumer Basic model with one consumer thread appropriate for lightweight processing of work items sequentially. Single Producer and Multiple Consumers - Unordered Delivery Our work items are processed concurrently. The application hook (ReadyForDelivery)  needs to be thread-safe as it gets called by multiple consumer threads. We will adapt this model if ordering is not desired by the application and concurrent processing is needed for further processing by the application logic. Single Producer and Multiple Consumers - Ordered Delivery Here also, our work items are processed concurrently but additionally, the work items are delivered sequentially to the applicati...

C++11 Multi-threaded Programming: Task-Queue Patterns - Part 1

Image
Introduction In multi-threaded programming paradigm, a task-queue is widely used pattern for inter thread communication. A task is a CPU or IO bound work item that completes in finite time. A queue is a container that holds these tasks and facilitates first-in-first-out (FIFO) order of their execution. This queue acts as a bridge between producer and consumer threads and logically separates their processing logic. It introduces an asynchronous flow in the execution wherein the producer thread delegates the processing of work item to another thread and goes back to the processing of newer work items. A task is inserted in the queue by a producer thread and consumer thread removes the task from the queue for further processing. A common example of this flow is a thread that reads a raw buffer from network socket, enqueues the buffer and goes back to listening mode. The enqueued buffer is dequeued and processed by another thread. This thread may run the application logic or m...

Popular posts from this blog

C++11 Multi-threaded Programming: Task-Queue Patterns - Part 2

C++11 Multi-threaded Programming: Task-Queue Patterns - Part 1