Commit c3932281 authored by Davis King's avatar Davis King

Added the ability to tell a threaded_object that it should restart its thread

upon thread termination.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403669
parent 24b33ef2
......@@ -116,6 +116,73 @@ namespace dlib
s.broadcast();
}
// ----------------------------------------------------------------------------------------
void threaded_object::
restart (
)
{
auto_mutex M(m_);
DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
"\tvoid threaded_object::restart()"
<< "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
<< "\n\tthis: " << this
);
if (is_alive_ == false)
{
if (create_new_thread<threaded_object,&threaded_object::thread_helper>(*this) == false)
{
is_running_ = false;
throw thread_error();
}
should_respawn_ = false;
}
else
{
should_respawn_ = true;
}
is_alive_ = true;
is_running_ = true;
should_stop_ = false;
s.broadcast();
}
// ----------------------------------------------------------------------------------------
void threaded_object::
set_respawn (
)
{
auto_mutex M(m_);
DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
"\tvoid threaded_object::set_respawn()"
<< "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
<< "\n\tthis: " << this
);
should_respawn_ = true;
}
// ----------------------------------------------------------------------------------------
bool threaded_object::
should_respawn (
) const
{
auto_mutex M(m_);
DLIB_ASSERT(id1 != get_thread_id() || id_valid == false,
"\tbool threaded_object::should_respawn()"
<< "\n\tYou can NOT call this function from the thread that executes threaded_object::thread"
<< "\n\tthis: " << this
);
return should_respawn_;
}
// ----------------------------------------------------------------------------------------
void threaded_object::
......@@ -149,6 +216,7 @@ namespace dlib
should_stop_ = true;
is_running_ = false;
should_respawn_ = false;
s.broadcast();
}
......@@ -179,18 +247,30 @@ namespace dlib
id1 = get_thread_id();
id_valid = true;
#endif
while (true)
{
m_.lock();
should_respawn_ = false;
m_.unlock();
thread();
auto_mutex M(m_);
thread();
auto_mutex M(m_);
if (should_respawn_)
continue;
#ifdef ENABLE_ASSERTS
id_valid = false;
id_valid = false;
#endif
is_alive_ = false;
is_running_ = false;
should_stop_ = false;
s.broadcast();
is_alive_ = false;
is_running_ = false;
should_stop_ = false;
s.broadcast();
return;
}
}
// ----------------------------------------------------------------------------------------
......
......@@ -21,6 +21,7 @@ namespace dlib
- is_running_ == false
- is_alive_ == false
- should_stop_ == false
- should_respawn_ == false
#ifdef ENABLE_ASSERTS
- id_valid == false
......@@ -31,6 +32,7 @@ namespace dlib
- is_running() == is_running_
- is_alive() == is_alive_
- should_stop() == should_stop_
- should_respawn() == should_respawn_
#ifdef ENABLE_ASSERTS
......@@ -66,6 +68,15 @@ namespace dlib
void start (
);
void restart (
);
void set_respawn (
);
bool should_respawn (
) const;
void pause (
);
......@@ -91,6 +102,7 @@ namespace dlib
bool is_running_;
bool is_alive_;
bool should_stop_;
bool should_respawn_;
bool id_valid;
// restricted functions
......
......@@ -16,6 +16,7 @@ namespace dlib
INITIAL VALUE
- is_running() == false
- is_alive() == false
- should_respawn() == false
WHAT THIS OBJECT REPRESENTS
This object represents a simple threaded object. To use it you inherit
......@@ -96,6 +97,48 @@ namespace dlib
#is_alive() == false and #is_running() == false
!*/
void set_respawn (
);
/*!
requires
- is not called from this->thread()
ensures
- #should_respawn() == true
!*/
bool should_respawn (
) const;
/*!
requires
- is not called from this->thread()
ensures
- returns true if the thread will automatically restart upon termination and
false otherwise. Note that every time a thread starts it sets should_respawn()
back to false. Therefore, a single call to set_respawn() can cause at most
one respawn to occur.
!*/
void restart (
);
/*!
requires
- is not called from this->thread()
ensures
- This function atomically executes set_respawn() and start(). The precise meaning of this
is defined below.
- if (is_alive()) then
- #should_respawn() == true
- else
- #should_respawn() == false
- #is_alive() == true
- #is_running() == true
- #should_stop() == false
throws
- std::bad_alloc or dlib::thread_error
If either of these exceptions are thrown then
#is_alive() == false and #is_running() == false
!*/
void pause (
);
/*!
......@@ -113,6 +156,7 @@ namespace dlib
ensures
- #should_stop() == true
- #is_running() == false
- #should_respawn() == false
!*/
protected:
......@@ -123,8 +167,7 @@ namespace dlib
requires
- is only called from the thread that executes this->thread()
ensures
- if (is_running() == false && should_stop() == false) then
- blocks until (#is_running() == true || #should_stop() == true)
- calls to this function block until (#is_running() == true || #should_stop() == true)
- if (this thread is supposed to terminate) then
- returns true
- else
......
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