Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
fe93ddaf
Commit
fe93ddaf
authored
Nov 18, 2012
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added lambda function support to the timeout object.
parent
96e1a7c8
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
9 deletions
+66
-9
timeout.h
dlib/timeout/timeout.h
+23
-0
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