Wait Queue
Loading...
Searching...
No Matches
threaded_queue_buffer_demo.cpp File Reference

Multithreaded demo of chops::shared_buffer and chops::wait_queue. More...

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <thread>
#include <functional>
#include <algorithm>
#include <atomic>
#include <future>
#include <ctime>
#include "queue/wait_queue.hpp"
#include "buffer/shared_buffer.hpp"
Include dependency graph for threaded_queue_buffer_demo.cpp:

Classes

class  DeviceDataGenerator
 
class  DataProcessor
 
class  Database
 
class  ThreadManagement
 

Typedefs

using device_q_t = chops::wait_queue<int>
 
using data_q_t = chops::wait_queue<chops::const_shared_buffer>
 

Functions

template<class C >
const char * cast_to_char_ptr (const C buf)
 
int main (int argc, char *argv[])
 

Detailed Description

Multithreaded demo of chops::shared_buffer and chops::wait_queue.

Author
Thurman Gillespy

Copyright (c)2019 by Thurman Gillespy 3/22/19

Minor changes Apr 2025 by Cliff Green to match changed APIs.

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Sample make file: g++ -std=c++17 -I ~/Projects/utility-rack/include/ multithreaded_demo.cpp -lpthread

Typedef Documentation

◆ device_q_t

Project Overview

This demo program shows how to use chops::wait_queue and chops::shared_buffer in a multithreaded environment. The program simulates multiple peripheral data generators, perhaps sensors or network connections. THe data is handled by one or more data processors, which sorts and formats the data, and periodically sends it to a simulated database.

The program can have 1 or more DeviceDataGenerator threads that each put 20 random numbers into device_q, a chops::wait_queue. Over 1000 threads can be run sucessfully (default is 20). Each DeviceDataGenerator thread generates random numbers in its' own 'centile': thread 0: <0..99>; thread 1: <100..199>; thread 2: <200-299>, etc.

The device_q numbers are read by 1 or more (default is 5) DataProcessor threads. The numbers are sorted by centile. When 5 numbers in the same centile are collected, a string with the numbers is created that is placed into data_q, another wait_queue of type chops::wait_queue<chops::const_shared_buffer>>, ie, the string is copied into a new chops::const_shared_buffer in the data_q. The first number in the string is the 'index' that correspons to that centile, and the remaining numbers are kept in chronologic order.

0 87 17 65 5 32s 8 870 813 808 827 874

The data_q is read by the single Database thread. The string is extracted from the data_q and appened to the proper centile string in the database.

When the Database thread is finished, a 'Data Report' is printed. Each row contains the random nubmers created by a particular thread, in chronlogic order.

Data Report [0] 30 71 2 99 60 74 11 70 4 41 83 90 14 72 68 68 88 51 28 78 [1] 103 110 186 136 152 187 165 169 108 157 116 138 136 132 153 144 159 144 120 110 [2] 273 215 249 265 218 252 286 239 273 269 242 263 245 249 215 266 214 245 258 214 [3] 387 303 312 350 323 331 343 374 363 371 390 335 367 317 397 341 394 399 341 366 [4] 447 415 448 464 455 489 462 446 440 452 417 426 417 446 427 433 484 431 445 468

Mutex locks and conditional variable signaling is handled within the chops::wait_queue class.