Commit 3275e58f authored by Davis King's avatar Davis King

Upgraded the thread_function object so that it is more general. It

can now call arbitrary function objects with up to 4 arguments.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403134
parent 8c10dd9a
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "threads_kernel.h" #include "threads_kernel.h"
#include "auto_mutex_extension.h" #include "auto_mutex_extension.h"
#include "threaded_object_extension.h" #include "threaded_object_extension.h"
#include "../smart_pointers.h"
namespace dlib namespace dlib
{ {
...@@ -20,90 +21,157 @@ namespace dlib ...@@ -20,90 +21,157 @@ namespace dlib
{ {
public: public:
virtual void go() = 0; virtual void go() = 0;
virtual ~base_funct() {} virtual ~base_funct() {}
}; };
template <typename T> template <typename F, typename T1, typename T2, typename T3, typename T4>
class super_funct_arg : public base_funct class super_funct_4 : public base_funct
{ {
public: public:
super_funct_arg ( super_funct_4 ( F funct, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
void (*funct)(T),
T arg
)
{ {
a = arg; a1 = arg1;
a2 = arg2;
a3 = arg3;
a4 = arg4;
f = funct; f = funct;
} }
void go() { f(a); } void go() { f(a1, a2, a3, a4); }
T a; T1 a1;
void (*f)(T); T2 a2;
T3 a3;
T4 a4;
F f;
}; };
template <typename F, typename T1, typename T2, typename T3>
class super_funct_3 : public base_funct
{
public:
super_funct_3 ( F funct, T1 arg1, T2 arg2, T3 arg3)
{
a1 = arg1;
a2 = arg2;
a3 = arg3;
f = funct;
}
void go() { f(a1, a2, a3); }
class super_funct_no_arg : public base_funct T1 a1;
T2 a2;
T3 a3;
F f;
};
template <typename F, typename T1, typename T2>
class super_funct_2 : public base_funct
{ {
public: public:
super_funct_no_arg ( super_funct_2 ( F funct, T1 arg1, T2 arg2)
void (*funct)()
)
{ {
a1 = arg1;
a2 = arg2;
f = funct; f = funct;
} }
void go() { f(); }
void (*f)(); void go() { f(a1, a2); }
T1 a1;
T2 a2;
F f;
};
template <typename F, typename T>
class super_funct_1 : public base_funct
{
public:
super_funct_1 ( F funct, T arg)
{
a = arg;
f = funct;
}
void go() { f(a); }
T a;
F f;
}; };
template <typename T> template <typename F>
class super_Tfunct_no_arg : public base_funct class super_funct_0 : public base_funct
{ {
public: public:
super_Tfunct_no_arg ( super_funct_0 ( F funct)
const T& funct
)
{ {
f = funct; f = funct;
} }
void go() { f(); } void go() { f(); }
T f; F f;
}; };
public: public:
template <typename T> template <typename F>
thread_function ( thread_function (
const T& funct F funct
) )
{ {
f = new super_Tfunct_no_arg<T>(funct); f.reset(new super_funct_0<F>(funct));
start(); start();
} }
template <typename F, typename T>
thread_function ( thread_function (
void (*funct)() F funct,
T arg
) )
{ {
f = new super_funct_no_arg(funct); f.reset(new super_funct_1<F,T>(funct,arg));
start(); start();
} }
template <typename T> template <typename F, typename T1, typename T2>
thread_function ( thread_function (
void (*funct)(T), F funct,
T arg T1 arg1,
T2 arg2
)
{
f.reset(new super_funct_2<F,T1,T2>(funct, arg1, arg2));
start();
}
template <typename F, typename T1, typename T2, typename T3>
thread_function (
F funct,
T1 arg1,
T2 arg2,
T3 arg3
)
{
f.reset(new super_funct_3<F,T1,T2,T3>(funct, arg1, arg2, arg3));
start();
}
template <typename F, typename T1, typename T2, typename T3, typename T4>
thread_function (
F funct,
T1 arg1,
T2 arg2,
T3 arg3,
T4 arg4
) )
{ {
f = new super_funct_arg<T>(funct,arg); f.reset(new super_funct_4<F,T1,T2,T3,T4>(funct, arg1, arg2, arg3, arg4));
start(); start();
} }
...@@ -111,7 +179,6 @@ namespace dlib ...@@ -111,7 +179,6 @@ namespace dlib
) )
{ {
threaded_object::wait(); threaded_object::wait();
delete f;
} }
bool is_alive ( bool is_alive (
...@@ -133,7 +200,7 @@ namespace dlib ...@@ -133,7 +200,7 @@ namespace dlib
f->go(); f->go();
} }
base_funct* f; scoped_ptr<base_funct> f;
// restricted functions // restricted functions
thread_function(thread_function&); // copy constructor thread_function(thread_function&); // copy constructor
......
...@@ -14,19 +14,19 @@ namespace dlib ...@@ -14,19 +14,19 @@ namespace dlib
{ {
/*! /*!
WHAT THIS OBJECT REPRESENTS WHAT THIS OBJECT REPRESENTS
This object represents a thread on a global C++ function. That is, This object represents a thread on a global C++ function or function
it allows you to run a global function in its own thread. object. That is, it allows you to run a function in its own thread.
!*/ !*/
public: public:
template <typename T> template <typename F>
thread_function ( thread_function (
const T& funct F funct
); );
/*! /*!
ensures ensures
- #*this is properly initialized - #*this is properly initialized
- the function object funct has been started in its own thread - the function funct has been started in its own thread
throws throws
- std::bad_alloc - std::bad_alloc
- dlib::thread_error - dlib::thread_error
...@@ -34,13 +34,15 @@ namespace dlib ...@@ -34,13 +34,15 @@ namespace dlib
gathering resources to create threading objects. gathering resources to create threading objects.
!*/ !*/
template <typename F, typename T1>
thread_function ( thread_function (
void (*funct)() F funct,
T1 arg1
); );
/*! /*!
ensures ensures
- #*this is properly initialized - #*this is properly initialized
- the function pointed to by funct has been started in its own thread - A thread has been created and it will call funct(arg1)
throws throws
- std::bad_alloc - std::bad_alloc
- dlib::thread_error - dlib::thread_error
...@@ -48,16 +50,53 @@ namespace dlib ...@@ -48,16 +50,53 @@ namespace dlib
gathering resources to create threading objects. gathering resources to create threading objects.
!*/ !*/
template <typename T> template <typename F, typename T1, typename T2>
thread_function ( thread_function (
void (*funct)(T), F funct,
T arg T1 arg1,
T2 arg2
); );
/*! /*!
ensures ensures
- #*this is properly initialized - #*this is properly initialized
- the function pointed to by funct has been started in its own thread and - A thread has been created and it will call funct(arg1, arg2)
passed the argument arg throws
- std::bad_alloc
- dlib::thread_error
the constructor may throw this exception if there is a problem
gathering resources to create threading objects.
!*/
template <typename F, typename T1, typename T2, typename T3>
thread_function (
F funct,
T1 arg1,
T2 arg2,
T3 arg3
);
/*!
ensures
- #*this is properly initialized
- A thread has been created and it will call funct(arg1, arg2, arg3)
throws
- std::bad_alloc
- dlib::thread_error
the constructor may throw this exception if there is a problem
gathering resources to create threading objects.
!*/
template <typename F, typename T1, typename T2, typename T3, typename T4>
thread_function (
F funct,
T1 arg1,
T2 arg2,
T3 arg3,
T4 arg4
);
/*!
ensures
- #*this is properly initialized
- A thread has been created and it will call funct(arg1, arg2, arg3, arg4)
throws throws
- std::bad_alloc - std::bad_alloc
- dlib::thread_error - dlib::thread_error
......
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