Commit 00662b4b authored by Davis King's avatar Davis King

Added a bunch of overloads for add_task_by_value()

parent 97cf9ba1
...@@ -43,6 +43,11 @@ namespace ...@@ -43,6 +43,11 @@ namespace
void set_global_var() { global_var = 9; } void set_global_var() { global_var = 9; }
void set_global_var_const() const { global_var = 9; } void set_global_var_const() const { global_var = 9; }
void set_global_var_arg1(int val) { global_var = val; }
void set_global_var_const_arg1(int val) const { global_var = val; }
void set_global_var_arg2(int val, int val2) { global_var = val+val2; }
void set_global_var_const_arg2(int val, int val2) const { global_var = val+val2; }
void operator()() void operator()()
{ {
global_var = 9; global_var = 9;
...@@ -242,6 +247,79 @@ namespace ...@@ -242,6 +247,79 @@ namespace
tp.wait_for_task(id); tp.wait_for_task(id);
DLIB_TEST(global_var == 9); DLIB_TEST(global_var == 9);
global_var = 0;
a = 4;
DLIB_TEST(global_var == 0);
id = tp.add_task(f, &add_functor::set_global_var_arg1, a);
tp.wait_for_task(id);
DLIB_TEST(global_var == 4);
global_var = 0;
a = 4;
DLIB_TEST(global_var == 0);
id = tp.add_task_by_value(f, &add_functor::set_global_var_arg1, a);
tp.wait_for_task(id);
DLIB_TEST(global_var == 4);
global_var = 0;
a = 4;
b = 3;
DLIB_TEST(global_var == 0);
id = tp.add_task(f, &add_functor::set_global_var_arg2, a, b);
tp.wait_for_task(id);
DLIB_TEST(global_var == 7);
global_var = 0;
a = 4;
b = 3;
DLIB_TEST(global_var == 0);
id = tp.add_task_by_value(f, &add_functor::set_global_var_arg2, a, b);
tp.wait_for_task(id);
DLIB_TEST(global_var == 7);
global_var = 0;
a = 4;
b = 3;
DLIB_TEST(global_var == 0);
id = tp.add_task(f, &add_functor::set_global_var_const_arg2, a, b);
tp.wait_for_task(id);
DLIB_TEST(global_var == 7);
global_var = 0;
a = 4;
b = 3;
DLIB_TEST(global_var == 0);
id = tp.add_task_by_value(f, &add_functor::set_global_var_const_arg2, a, b);
tp.wait_for_task(id);
DLIB_TEST(global_var == 7);
global_var = 0;
a = 4;
DLIB_TEST(global_var == 0);
id = tp.add_task(f, &add_functor::set_global_var_const_arg1, a);
tp.wait_for_task(id);
DLIB_TEST(global_var == 4);
global_var = 0;
a = 4;
DLIB_TEST(global_var == 0);
id = tp.add_task_by_value(f, &add_functor::set_global_var_const_arg1, a);
tp.wait_for_task(id);
DLIB_TEST(global_var == 4);
global_var = 0;
DLIB_TEST(global_var == 0);
id = tp.add_task_by_value(f, &add_functor::set_global_var);
tp.wait_for_task(id);
DLIB_TEST(global_var == 9);
global_var = 0; global_var = 0;
DLIB_TEST(global_var == 0); DLIB_TEST(global_var == 0);
...@@ -250,6 +328,13 @@ namespace ...@@ -250,6 +328,13 @@ namespace
DLIB_TEST(global_var == 9); DLIB_TEST(global_var == 9);
global_var = 0;
DLIB_TEST(global_var == 0);
id = tp.add_task_by_value(f, &add_functor::set_global_var_const);
tp.wait_for_task(id);
DLIB_TEST(global_var == 9);
} }
......
...@@ -586,6 +586,40 @@ namespace dlib ...@@ -586,6 +586,40 @@ namespace dlib
return id; return id;
} }
template <typename T>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)() const
)
{
thread_pool_implementation::function_object_copy_instance<const T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<const T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item,funct);
uint64 id = impl->add_task_internal(temp, function_copy);
return id;
}
template <typename T>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)()
)
{
thread_pool_implementation::function_object_copy_instance<T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item,funct);
uint64 id = impl->add_task_internal(temp, function_copy);
return id;
}
uint64 add_task ( uint64 add_task (
void (*funct)() void (*funct)()
) )
...@@ -655,6 +689,28 @@ namespace dlib ...@@ -655,6 +689,28 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1),
future<A1>& arg1
)
{
thread_pool_implementation::function_object_copy_instance<T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item,funct,arg1.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the future to this task
arg1.task_id = id;
arg1.tp = impl;
return id;
}
template <typename T, typename T1, typename A1> template <typename T, typename T1, typename A1>
uint64 add_task ( uint64 add_task (
const T& obj, const T& obj,
...@@ -672,6 +728,27 @@ namespace dlib ...@@ -672,6 +728,27 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1) const,
future<A1>& arg1
)
{
thread_pool_implementation::function_object_copy_instance<const T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<const T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item,funct,arg1.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the future to this task
arg1.task_id = id;
arg1.tp = impl;
return id;
}
template <typename T1, typename A1> template <typename T1, typename A1>
uint64 add_task ( uint64 add_task (
void (*funct)(T1), void (*funct)(T1),
...@@ -756,6 +833,31 @@ namespace dlib ...@@ -756,6 +833,31 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1,
typename T2, typename A2>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2),
future<A1>& arg1,
future<A2>& arg2
)
{
thread_pool_implementation::function_object_copy_instance<T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, funct, arg1.get(), arg2.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the futures to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
return id;
}
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2> typename T2, typename A2>
uint64 add_task ( uint64 add_task (
...@@ -777,6 +879,31 @@ namespace dlib ...@@ -777,6 +879,31 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1,
typename T2, typename A2>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2) const,
future<A1>& arg1,
future<A2>& arg2
)
{
thread_pool_implementation::function_object_copy_instance<const T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<const T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, funct, arg1.get(), arg2.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the futures to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
return id;
}
template <typename T1, typename A1, template <typename T1, typename A1,
typename T2, typename A2> typename T2, typename A2>
uint64 add_task ( uint64 add_task (
...@@ -875,6 +1002,35 @@ namespace dlib ...@@ -875,6 +1002,35 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3),
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3
)
{
thread_pool_implementation::function_object_copy_instance<T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, funct, arg1.get(), arg2.get(), arg3.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the futures to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
arg3.task_id = id;
arg3.tp = impl;
return id;
}
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3> typename T3, typename A3>
...@@ -900,6 +1056,35 @@ namespace dlib ...@@ -900,6 +1056,35 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3) const,
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3
)
{
thread_pool_implementation::function_object_copy_instance<const T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<const T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, funct, arg1.get(), arg2.get(), arg3.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the futures to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
arg3.task_id = id;
arg3.tp = impl;
return id;
}
template <typename T1, typename A1, template <typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3> typename T3, typename A3>
...@@ -1012,6 +1197,39 @@ namespace dlib ...@@ -1012,6 +1197,39 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3,
typename T4, typename A4>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3,T4),
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3,
future<A4>& arg4
)
{
thread_pool_implementation::function_object_copy_instance<T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, funct, arg1.get(), arg2.get(), arg3.get(), arg4.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the futures to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
arg3.task_id = id;
arg3.tp = impl;
arg4.task_id = id;
arg4.tp = impl;
return id;
}
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3, typename T3, typename A3,
...@@ -1041,6 +1259,39 @@ namespace dlib ...@@ -1041,6 +1259,39 @@ namespace dlib
return id; return id;
} }
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3,
typename T4, typename A4>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3,T4) const,
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3,
future<A4>& arg4
)
{
thread_pool_implementation::function_object_copy_instance<const T>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<const T>(obj);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, funct, arg1.get(), arg2.get(), arg3.get(), arg4.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the futures to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
arg3.task_id = id;
arg3.tp = impl;
arg4.task_id = id;
arg4.tp = impl;
return id;
}
template <typename T1, typename A1, template <typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3, typename T3, typename A3,
......
...@@ -303,7 +303,7 @@ namespace dlib ...@@ -303,7 +303,7 @@ namespace dlib
ensures ensures
- if (is_task_thread() == true and there aren't any free threads available) then - if (is_task_thread() == true and there aren't any free threads available) then
- calls (obj.*funct)() within the calling thread and returns - calls (obj.*funct)() within the calling thread and returns
when it finishes when it finishes.
- else - else
- the call to this function blocks until there is a free thread in the pool - the call to this function blocks until there is a free thread in the pool
to process this new task. Once a free thread is available the task to process this new task. Once a free thread is available the task
...@@ -312,6 +312,27 @@ namespace dlib ...@@ -312,6 +312,27 @@ namespace dlib
for the submitted task to finish. for the submitted task to finish.
!*/ !*/
template <typename T>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)()
);
/*!
requires
- funct == a valid member function pointer for class T
ensures
- makes a copy of obj, call it OBJ_COPY.
- if (is_task_thread() == true and there aren't any free threads available) then
- calls (OBJ_COPY.*funct)() within the calling thread and returns
when it finishes.
- else
- the call to this function blocks until there is a free thread in the pool
to process this new task. Once a free thread is available the task
is handed off to that thread which then calls (OBJ_COPY.*funct)().
- returns a task id that can be used by this->wait_for_task() to wait
for the submitted task to finish.
!*/
template <typename T> template <typename T>
uint64 add_task ( uint64 add_task (
T& obj, T& obj,
...@@ -457,6 +478,30 @@ namespace dlib ...@@ -457,6 +478,30 @@ namespace dlib
for the submitted task to finish. for the submitted task to finish.
!*/ !*/
template <typename T, typename T1, typename A1>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1),
future<A1>& arg1
);
/*!
requires
- funct == a valid member function pointer for class T
- (obj.*funct)(arg1.get()) must be a valid expression.
(i.e. The A1 type stored in the future must be a type that can be passed into the given function)
ensures
- makes a copy of obj, call it OBJ_COPY.
- if (is_task_thread() == true and there aren't any free threads available) then
- calls (OBJ_COPY.*funct)(arg1.get()) within the calling thread and returns
when it finishes.
- else
- the call to this function blocks until there is a free thread in the pool
to process this new task. Once a free thread is available the task
is handed off to that thread which then calls (OBJ_COPY.*funct)(arg1.get()).
- returns a task id that can be used by this->wait_for_task() to wait
for the submitted task to finish.
!*/
template <typename T, typename T1, typename A1> template <typename T, typename T1, typename A1>
uint64 add_task ( uint64 add_task (
const T& obj, const T& obj,
...@@ -484,6 +529,30 @@ namespace dlib ...@@ -484,6 +529,30 @@ namespace dlib
for the submitted task to finish. for the submitted task to finish.
!*/ !*/
template <typename T, typename T1, typename A1>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1) const,
future<A1>& arg1
);
/*!
requires
- funct == a valid member function pointer for class T
- (obj.*funct)(arg1.get()) must be a valid expression.
(i.e. The A1 type stored in the future must be a type that can be passed into the given function)
ensures
- makes a copy of obj, call it OBJ_COPY.
- if (is_task_thread() == true and there aren't any free threads available) then
- calls (OBJ_COPY.*funct)(arg1.get()) within the calling thread and returns
when it finishes.
- else
- the call to this function blocks until there is a free thread in the pool
to process this new task. Once a free thread is available the task
is handed off to that thread which then calls (OBJ_COPY.*funct)(arg1.get()).
- returns a task id that can be used by this->wait_for_task() to wait
for the submitted task to finish.
!*/
template <typename T1, typename A1> template <typename T1, typename A1>
uint64 add_task ( uint64 add_task (
void (*funct)(T1), void (*funct)(T1),
...@@ -536,6 +605,13 @@ namespace dlib ...@@ -536,6 +605,13 @@ namespace dlib
future<A2>& arg2 future<A2>& arg2
); );
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2),
future<A1>& arg1,
future<A2>& arg2
);
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2> typename T2, typename A2>
uint64 add_task ( uint64 add_task (
...@@ -545,6 +621,15 @@ namespace dlib ...@@ -545,6 +621,15 @@ namespace dlib
future<A2>& arg2 future<A2>& arg2
); );
template <typename T, typename T1, typename A1,
typename T2, typename A2>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2) const,
future<A1>& arg1,
future<A2>& arg2
);
template <typename T1, typename A1, template <typename T1, typename A1,
typename T2, typename A2> typename T2, typename A2>
uint64 add_task ( uint64 add_task (
...@@ -564,7 +649,7 @@ namespace dlib ...@@ -564,7 +649,7 @@ namespace dlib
); );
template <typename F, typename A1, typename A2, typename A3> template <typename F, typename A1, typename A2, typename A3>
uint64 add_task ( uint64 add_task_by_value (
const F& function_object, const F& function_object,
future<A1>& arg1, future<A1>& arg1,
future<A2>& arg2, future<A2>& arg2,
...@@ -582,6 +667,17 @@ namespace dlib ...@@ -582,6 +667,17 @@ namespace dlib
future<A3>& arg3 future<A3>& arg3
); );
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3),
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3
);
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3> typename T3, typename A3>
...@@ -593,6 +689,17 @@ namespace dlib ...@@ -593,6 +689,17 @@ namespace dlib
future<A3>& arg3 future<A3>& arg3
); );
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3) const,
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3
);
template <typename T1, typename A1, template <typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3> typename T3, typename A3>
...@@ -615,7 +722,7 @@ namespace dlib ...@@ -615,7 +722,7 @@ namespace dlib
); );
template <typename F, typename A1, typename A2, typename A3, typename A4> template <typename F, typename A1, typename A2, typename A3, typename A4>
uint64 add_task ( uint64 add_task_by_value (
const F& function_object, const F& function_object,
future<A1>& arg1, future<A1>& arg1,
future<A2>& arg2, future<A2>& arg2,
...@@ -636,6 +743,19 @@ namespace dlib ...@@ -636,6 +743,19 @@ namespace dlib
future<A4>& arg4 future<A4>& arg4
); );
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3,
typename T4, typename A4>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3,T4),
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3,
future<A4>& arg4
);
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3, typename T3, typename A3,
...@@ -649,6 +769,19 @@ namespace dlib ...@@ -649,6 +769,19 @@ namespace dlib
future<A4>& arg4 future<A4>& arg4
); );
template <typename T, typename T1, typename A1,
typename T2, typename A2,
typename T3, typename A3,
typename T4, typename A4>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)(T1,T2,T3,T4) const,
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3,
future<A4>& arg4
);
template <typename T1, typename A1, template <typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3, typename T3, typename A3,
...@@ -674,6 +807,12 @@ namespace dlib ...@@ -674,6 +807,12 @@ namespace dlib
void (T::*funct)() const, void (T::*funct)() const,
); );
template <typename T>
uint64 add_task_by_value (
const T& obj,
void (T::*funct)() const
);
uint64 add_task ( uint64 add_task (
void (*funct)() void (*funct)()
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment