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
27b2d034
Commit
27b2d034
authored
Dec 07, 2017
by
Davis King
Browse files
Options
Browse Files
Download
Plain Diff
Merge
parents
95d16fd0
0ff862ae
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
128 deletions
+19
-128
threads_kernel_1.h
dlib/threads/threads_kernel_1.h
+19
-128
No files found.
dlib/threads/threads_kernel_1.h
View file @
27b2d034
...
...
@@ -12,6 +12,9 @@
#include "../windows_magic.h"
#include <windows.h>
#include "../algs.h"
#include <condition_variable>
#include <mutex>
#include <chrono>
namespace
dlib
...
...
@@ -43,21 +46,22 @@ namespace dlib
mutex
(
)
{
InitializeCriticalSection
(
&
cs
);
}
~
mutex
(
)
{
DeleteCriticalSection
(
&
cs
);
}
)
{
}
void
lock
(
)
const
{
EnterCriticalSection
(
&
cs
);
}
)
const
{
cs
.
lock
(
);
}
void
unlock
(
)
const
{
LeaveCriticalSection
(
&
cs
);
}
)
const
{
cs
.
unlock
(
);
}
private
:
mutable
CRITICAL_SECTION
cs
;
friend
class
signaler
;
mutable
std
::
mutex
cs
;
// restricted functions
mutex
(
mutex
&
);
// copy constructor
...
...
@@ -77,147 +81,40 @@ namespace dlib
signaler
(
const
mutex
&
associated_mutex
)
:
hSemaphore
(
CreateSemaphore
(
NULL
,
0
,
100000000
,
NULL
)),
waiters
(
0
),
hCountSema
(
CreateSemaphore
(
NULL
,
0
,
100000000
,
NULL
)),
m
(
associated_mutex
)
{
if
(
hSemaphore
==
NULL
||
hCountSema
==
NULL
)
{
if
(
hSemaphore
!=
NULL
)
{
CloseHandle
(
hSemaphore
);
}
if
(
hCountSema
!=
NULL
)
{
CloseHandle
(
hCountSema
);
}
throw
dlib
::
thread_error
(
ECREATE_SIGNALER
,
"in function signaler::signaler() an error occurred making the signaler"
);
}
}
~
signaler
(
)
{
CloseHandle
(
hSemaphore
);
CloseHandle
(
hCountSema
);
}
)
{
}
void
wait
(
)
const
{
// get a lock on the mutex for the waiters variable
waiters_mutex
.
lock
();
// mark that one more thread will be waiting on this signaler
++
waiters
;
// release the mutex for waiters
waiters_mutex
.
unlock
();
// release the associated mutex
m
.
unlock
();
// wait for the semaphore to be signaled
WaitForSingleObject
(
hSemaphore
,
INFINITE
);
// signal that we are awake
ReleaseSemaphore
(
hCountSema
,(
LONG
)
1
,
NULL
);
// relock the associated mutex
m
.
lock
();
std
::
unique_lock
<
std
::
mutex
>
cs
(
m
.
cs
,
std
::
defer_lock
);
cv
.
wait
(
cs
);
}
bool
wait_or_timeout
(
unsigned
long
milliseconds
)
const
{
// get a lock on the mutex for the waiters variable
waiters_mutex
.
lock
();
// mark that one more thread will be waiting on this signaler
++
waiters
;
// release the mutex for waiters
waiters_mutex
.
unlock
();
// release the associated mutex
m
.
unlock
();
bool
value
;
// wait for the semaphore to be signaled
if
(
WaitForSingleObject
(
hSemaphore
,
milliseconds
)
==
WAIT_TIMEOUT
)
{
// in this case we should decrement waiters because we are returning
// due to a timeout rather than because someone called signal() or
// broadcast().
value
=
false
;
// signal that we are awake
ReleaseSemaphore
(
hCountSema
,(
LONG
)
1
,
NULL
);
// get a lock on the mutex for the waiters variable
waiters_mutex
.
lock
();
// mark that one less thread will be waiting on this signaler.
if
(
waiters
!=
0
)
--
waiters
;
// release the mutex for waiters
waiters_mutex
.
unlock
();
}
else
{
value
=
true
;
// signal that we are awake
ReleaseSemaphore
(
hCountSema
,(
LONG
)
1
,
NULL
);
}
// relock the associated mutex
m
.
lock
();
return
value
;
std
::
unique_lock
<
std
::
mutex
>
cs
(
m
.
cs
,
std
::
defer_lock
);
auto
status
=
cv
.
wait_until
(
cs
,
std
::
chrono
::
system_clock
::
now
()
+
std
::
chrono
::
milliseconds
(
milliseconds
));
return
status
==
std
::
cv_status
::
no_timeout
;
}
void
signal
(
)
const
{
// get a lock on the mutex for the waiters variable
waiters_mutex
.
lock
();
if
(
waiters
>
0
)
{
--
waiters
;
// make the semaphore release one waiting thread
ReleaseSemaphore
(
hSemaphore
,
1
,
NULL
);
// wait for signaled thread to wake up
WaitForSingleObject
(
hCountSema
,
INFINITE
);
}
// release the mutex for waiters
waiters_mutex
.
unlock
();
cv
.
notify_one
();
}
void
broadcast
(
)
const
{
// get a lock on the mutex for the waiters variable
waiters_mutex
.
lock
();
if
(
waiters
>
0
)
{
// make the semaphore release all the waiting threads
ReleaseSemaphore
(
hSemaphore
,(
LONG
)
waiters
,
NULL
);
// wait for count to be zero
for
(
unsigned
long
i
=
0
;
i
<
waiters
;
++
i
)
{
WaitForSingleObject
(
hCountSema
,
INFINITE
);
}
waiters
=
0
;
}
// release the mutex for waiters
waiters_mutex
.
unlock
();
cv
.
notify_all
();
}
const
mutex
&
get_mutex
(
...
...
@@ -225,13 +122,7 @@ namespace dlib
private
:
mutable
HANDLE
hSemaphore
;
mutable
unsigned
long
waiters
;
mutex
waiters_mutex
;
mutable
HANDLE
hCountSema
;
mutable
std
::
condition_variable
cv
;
const
mutex
&
m
;
...
...
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