MCPcopy Create free account
hub / github.com/davisking/dlib / add_task

Method add_task

dlib/threads/thread_pool_extension.h:171–210  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

169
170 template <typename T>
171 uint64 add_task (
172 T& obj,
173 void (T::*funct)()
174 )
175 {
176 auto_mutex M(m);
177 const thread_id_type my_thread_id = get_thread_id();
178
179 // find a thread that isn't doing anything
180 long idx = find_empty_task_slot();
181 if (idx == -1 && is_worker_thread(my_thread_id))
182 {
183 // this function is being called from within a worker thread and there
184 // aren't any other worker threads free so just perform the task right
185 // here
186
187 M.unlock();
188 (obj.*funct)();
189
190 // return a task id that is both non-zero and also one
191 // that is never normally returned. This way calls
192 // to wait_for_task() will never block given this id.
193 return 1;
194 }
195
196 // wait until there is a thread that isn't doing anything
197 while (idx == -1)
198 {
199 task_done_signaler.wait();
200 idx = find_empty_task_slot();
201 }
202
203 tasks[idx].thread_id = my_thread_id;
204 tasks[idx].task_id = make_next_task_id(idx);
205 tasks[idx].mfp0.set(obj,funct);
206
207 task_ready_signaler.signal();
208
209 return tasks[idx].task_id;
210 }
211
212 template <typename T>
213 uint64 add_task (

Callers

nothing calls this directly

Calls 5

get_thread_idFunction · 0.70
unlockMethod · 0.45
waitMethod · 0.45
setMethod · 0.45
signalMethod · 0.45

Tested by

no test coverage detected