24#ifndef LIBTHREADAR_RATELIER_SCATTER_HPP
25#define LIBTHREADAR_RATELIER_SCATTER_HPP
84 void scatter(std::unique_ptr<T> & one,
signed int flag = 0);
94 std::unique_ptr<T>
worker_get_one(
unsigned int & slot,
signed int & flag);
103 static const unsigned int cond_empty = 0;
104 static const unsigned int cond_full = 1;
108 std::unique_ptr<T> obj;
113 slot(
signed int val) { empty =
true; flag = val; };
114 slot(
const slot & ref) { obj.reset(); empty = ref.empty; index = ref.index; flag = ref.flag; };
117 unsigned int next_index;
118 unsigned int lowest_index;
119 std::vector<slot> table;
120 std::map<unsigned int, unsigned int> corres;
121 std::deque<unsigned int> empty_slot;
126 table(size, slot(flag)),
132 for(
unsigned int i = 0; i < size; ++i)
133 empty_slot.push_back(i);
138 unsigned int tableindex;
143 while(empty_slot.empty())
144 verrou.wait(cond_full);
146 tableindex = empty_slot.back();
150 if(tableindex >= table.size())
152 if( ! table[tableindex].empty)
157 table[tableindex].empty =
false;
158 table[tableindex].obj = std::move(one);
159 table[tableindex].index = next_index;
160 table[tableindex].flag = flag;
162 corres[next_index] = tableindex;
165 empty_slot.pop_back();
166 if(verrou.get_waiting_thread_count(cond_empty) > 0)
167 verrou.signal(cond_empty);
172 verrou.broadcast(cond_empty);
173 verrou.broadcast(cond_full);
181 std::unique_ptr<T> ret;
186 std::map<unsigned int, unsigned int>::iterator it = corres.begin();
192 if(it != corres.end())
194 if(it->first < lowest_index)
201 if(it->second >= table.size())
203 if(table[it->second].empty)
205 if( ! table[it->second].obj)
210 ret = std::move(table[it->second].obj);
211 slot = table[it->second].index;
212 flag = table[it->second].flag;
213 table[it->second].empty =
true;
215 if(lowest_index != slot)
221 empty_slot.push_back(it->second);
224 if(verrou.get_waiting_thread_count(cond_full) > 0)
225 verrou.signal(cond_full);
232 verrou.wait(cond_empty);
241 verrou.broadcast(cond_empty);
242 verrou.broadcast(cond_full);
252 unsigned int size = table.size();
258 for(
unsigned int i = 0; i < size; ++i)
260 table[i].obj.reset();
261 table[i].empty =
true;
262 empty_slot.push_back(i);
266 verrou.broadcast(cond_empty);
267 verrou.broadcast(cond_full);
Wrapper around the Posix pthread_cond_t object and its associated mutex.
The class ratelier_scatter's purpose it to scatter an ordered set of data to many worker threads.
std::unique_ptr< T > worker_get_one(unsigned int &slot, signed int &flag)
For a worker thread to obtain an object in the lowest slot available.
void scatter(std::unique_ptr< T > &one, signed int flag=0)
For the non-worker thread to provide data to the ratelier_scatter.
void reset()
reset the object in its prestine state
#define THREADAR_BUG
Macro used to throw an exception_bug when execution reach that statement.
defines the mutex C++ class
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...