Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
fe93ddaf
Commit
fe93ddaf
authored
Nov 17, 2012
by
Davis King
Browse files
Added lambda function support to the timeout object.
parent
96e1a7c8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
9 deletions
+66
-9
dlib/timeout/timeout.h
dlib/timeout/timeout.h
+23
-0
dlib/timeout/timeout_abstract.h
dlib/timeout/timeout_abstract.h
+43
-9
No files found.
dlib/timeout/timeout.h
View file @
fe93ddaf
...
...
@@ -40,6 +40,15 @@ namespace dlib
virtual
~
bind
()
{}
};
template
<
typename
T
>
class
functor
:
public
bind
{
public:
functor
(
const
T
&
f
)
:
function
(
f
)
{}
T
function
;
void
go
()
{
function
();
}
};
template
<
typename
T
,
typename
R
>
class
zero
:
public
bind
{
...
...
@@ -65,6 +74,20 @@ namespace dlib
// This typedef is here for backwards compatibility with previous versions of dlib.
typedef
timeout
kernel_1a
;
template
<
typename
T
>
timeout
(
T
callback_function
,
unsigned
long
ms_to_timeout
)
:
t
(
*
this
,
&
timeout
::
trigger_timeout
)
{
b
=
new
functor
<
T
>
(
callback_function
);
t
.
set_delay_time
(
ms_to_timeout
);
t
.
start
();
}
template
<
typename
T
>
...
...
dlib/timeout/timeout_abstract.h
View file @
fe93ddaf
...
...
@@ -12,29 +12,63 @@ namespace dlib
{
/*!
WHAT THIS OBJECT REPRESENTS
This object provides a simple way to implement a timeout. An example will
make
its use clear. Suppose we want to read from a socket but we want to
terminate the
connection if the read takes longer than 10 seconds. This
could be accomplished
as follows:
This object provides a simple way to implement a timeout. An example will
make
its use clear. Suppose we want to read from a socket but we want to
terminate the
connection if the read takes longer than 10 seconds. This
could be accomplished
as follows:
connection* con = a connection from somewhere;
{
// setup a timer that will call con->shutdown() in 10 seconds
timeout t(*con,&connection::shutdown,10000);
// Now call read on the connection. If this call to read() takes
//
more
than 10 seconds then the t timeout will trigger and shutdown
//
the
connection. If read completes in less than 10 seconds then
//
the t
object will be destructed on the next line due to the }
//
and then the
timeout won't trigger.
// Now call read on the connection. If this call to read() takes
more
// than 10 seconds then the t timeout will trigger and shutdown
the
// connection. If read completes in less than 10 seconds then
the t
// object will be destructed on the next line due to the }
and then the
// timeout won't trigger.
con->read(buf,100);
}
Alternatively, if you have a compiler capable of using C++11 lambda
functions, you can use a syntax like this:
{
timeout t([con](){ con->shutdown(); }, 10000);
con->read(buf,100);
}
More generally, you can use this with things other than sockets. For
example, the following statement will print "Hello world!" after 1000ms:
timeout t([](){ cout << "Hello world!" << endl; }, 1000);
THREAD SAFETY
All methods of this class are thread safe.
!*/
public:
template
<
typename
T
>
timeout
(
T
callback_function
,
unsigned
long
ms_to_timeout
);
/*!
requires
- callback_function does not throw
ensures
- does not block.
- #*this is properly initialized
- if (this object isn't destructed in ms_to_timeout milliseconds) then
- callback_function() will be called in ms_to_timeout milliseconds.
throws
- std::bad_alloc
- dlib::thread_error
!*/
template
<
typename
T
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment