cppthread 1.1.16
C++ Thread Library
item_with_predicate.cpp
Go to the documentation of this file.
1// Copyright (c) 2013-2025 Made to Order Software Corp. All Rights Reserved
2//
3// https://snapwebsites.org/project/cppthread
4// contact@m2osw.com
5//
6// This program is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 2 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License along
17// with this program; if not, write to the Free Software Foundation, Inc.,
18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
39// self
40//
42
43#include "cppthread/exception.h"
44#include "cppthread/guard.h"
45
46
47// C++
48//
49#include <iostream>
50
51
52
53namespace cppthread
54{
55
56
57
92 : f_dependencies({ dependency })
93{
94}
95
96
117 : f_dependencies(dependencies)
118{
119}
120
121
130
131
158{
159 guard lock(f_mutex);
160
161 if(f_processing)
162 {
163 throw in_use_error("workload already being processed, you can't add more dependencies to it.");
164 }
165
166 f_dependencies.push_back(item);
167}
168
169
184{
185 guard lock(f_mutex);
186
187 if(f_processing)
188 {
189 throw in_use_error("workload already being processed, you can't add more dependencies to it.");
190 }
191
192 f_dependencies.insert(f_dependencies.begin(), dependencies.cbegin(), dependencies.cend());
193}
194
195
218{
219 guard lock(f_mutex);
220
221 for(auto it(f_dependencies.begin()); it != f_dependencies.end(); )
222 {
223 if(it->expired())
224 {
225 it = f_dependencies.erase(it);
226 }
227 else
228 {
229 ++it;
230 }
231 }
232
233 if(f_dependencies.empty())
234 {
235 f_processing = true;
236 return true;
237 }
238
239 return false;
240}
241
242
243
244
245
313} // namespace cppthread
314// vim: ts=4 sw=4 et
Lock a mutex in an RAII manner.
Definition guard.h:42
virtual ~item_with_predicate()
The destructor of the item with predicate.
bool f_processing
Whether this workload is being processed.
virtual bool valid_workload() const
The valid_workload() to test whether we can process this item.
void add_dependencies(dependencies_t const &dependencies)
Add a set of dependencies at once.
item_with_predicate(pointer_t dependency=pointer_t())
Initialize the item with one dependency.
std::shared_ptr< item_with_predicate > pointer_t
The item_with_predicate shared pointer type.
mutex f_mutex
The mutex used to protect the predicate variables.
std::deque< weak_pointer_t > dependencies_t
The type representing the list of dependencies.
dependencies_t f_dependencies
Set of dependencies.
void add_dependency(pointer_t dependency)
Add an item as a predicate of this item.
Exceptions for the thread environment.
Thread Runner and Managers.
Item with Predicate for Worker FIFO Pool.

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.