Project
Loading...
Searching...
No Matches
test_Fifo.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
16
17#define BOOST_TEST_MODULE Utility test
18#define BOOST_TEST_MAIN
19#define BOOST_TEST_DYN_LINK
20#include <boost/test/unit_test.hpp>
21#include "Fifo.h"
22#include <iostream>
23#include <iomanip>
24#include <unistd.h>
25#include <thread>
26
27const int nEvents = 6;
28const int sleepTime = 1;
29
30template <typename T>
32{
33 std::cout << "processing " << value << std::endl;
34 sleep(sleepTime);
35 return (value + 1 < nEvents);
36}
37
38template <class FifoT, typename T>
39void pushFifo(FifoT& fifo, T value = FifoT::value_type)
40{
41 std::cout << "pushing " << value << std::endl;
42 fifo.push(value);
43}
44
46{
47 using value_type = unsigned int;
49
50 // start a consumer thread which pulls from the FIFO to the function
51 // processValue with a simulated prcessing of one second.
52 std::thread consumer([&fifo]() {
53 do {
54 } while (fifo.pull([](value_type v) { return processValue(v); }));
55 });
56
57 // fill some values into the FIFO which the consumer can process
58 // immediately
59 unsigned int value = 0;
60 pushFifo(fifo, value++);
61 pushFifo(fifo, value++);
62 pushFifo(fifo, value++);
63
64 // now continue filling with a period longer than the consumer
65 // processing, consumer and producer are in sync once consumer
66 // has processed events which have been added to the FIFO before
67 while (value < nEvents) {
68 sleep(2 * sleepTime);
69 pushFifo(fifo, value++);
70 }
71
72 consumer.join();
73}
Thread safe FIFO.
A thread safe FIFO.
Definition Fifo.h:60
bool pull(F processor)
Definition Fifo.h:95
const GLdouble * v
Definition glcorearb.h:832
GLsizei const GLfloat * value
Definition glcorearb.h:819
void pushFifo(FifoT &fifo, T value=FifoT::value_type)
Definition test_Fifo.cxx:39
BOOST_AUTO_TEST_CASE(test_Fifo)
Definition test_Fifo.cxx:45
const int sleepTime
Definition test_Fifo.cxx:28
const int nEvents
Definition test_Fifo.cxx:27
bool processValue(T value)
Definition test_Fifo.cxx:31