Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
e88a4669
Commit
e88a4669
authored
Nov 23, 2014
by
gabi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
blocking queue to notify one and not all
parent
be9dd21f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
36 deletions
+41
-36
include/spdlog/details/blocking_queue.h
include/spdlog/details/blocking_queue.h
+41
-36
No files found.
include/spdlog/details/blocking_queue.h
View file @
e88a4669
...
...
@@ -69,65 +69,60 @@ public:
template
<
typename
Duration_Rep
,
typename
Duration_Period
,
typename
TT
>
bool
push
(
TT
&&
item
,
const
std
::
chrono
::
duration
<
Duration_Rep
,
Duration_Period
>&
timeout
)
{
std
::
unique_lock
<
std
::
mutex
>
ul
(
_mutex
);
if
(
_q
.
size
()
>=
_max_size
)
{
if
(
!
_item_popped_cond
.
wait_until
(
ul
,
clock
::
now
()
+
timeout
,[
this
]()
std
::
unique_lock
<
std
::
mutex
>
ul
(
_mutex
);
if
(
is_full
())
{
return
this
->
_q
.
size
()
<
this
->
_max_size
;
if
(
!
_item_popped_cond
.
wait_until
(
ul
,
clock
::
now
()
+
timeout
,
[
this
]()
{
return
!
this
->
is_full
();
}))
return
false
;
}
_q
.
push
(
std
::
forward
<
TT
>
(
item
));
if
(
_q
.
size
()
<=
1
)
{
ul
.
unlock
();
//So the notified thread will have better chance to accuire the lock immediatly..
_item_pushed_cond
.
notify_all
();
}
_item_pushed_cond
.
notify_one
();
return
true
;
}
// Push copy of item into the back of the queue.
// If the queue is full, block the calling thread until there is room.
// Push copy of item into the back of the queue.
// If the queue is full, block the calling thread until there is room.
template
<
typename
TT
>
void
push
(
TT
&&
item
)
{
while
(
!
push
(
std
::
forward
<
TT
>
(
item
),
std
::
chrono
::
seconds
(
1
0
)));
while
(
!
push
(
std
::
forward
<
TT
>
(
item
),
std
::
chrono
::
seconds
(
6
0
)));
}
// Pop a copy of the front item in the queue into the given item ref.
// If the queue is empty, block the calling thread util there is item to pop or timeout have passed.
// Return: false on timeout , true on successful pop/
// Pop a copy of the front item in the queue into the given item ref.
// If the queue is empty, block the calling thread util there is item to pop or timeout have passed.
// Return: false on timeout , true on successful pop/
template
<
class
Duration_Rep
,
class
Duration_Period
>
bool
pop
(
T
&
item
,
const
std
::
chrono
::
duration
<
Duration_Rep
,
Duration_Period
>&
timeout
)
{
{
std
::
unique_lock
<
std
::
mutex
>
ul
(
_mutex
);
if
(
_q
.
empty
())
if
(
is_
empty
())
{
if
(
!
_item_pushed_cond
.
wait_until
(
ul
,
clock
::
now
()
+
timeout
,
[
this
]()
{
return
!
this
->
_q
.
empty
();
return
!
this
->
is_
empty
();
}))
return
false
;
}
item
=
std
::
move
(
_q
.
front
());
_q
.
pop
();
if
(
_q
.
size
()
>=
_max_size
-
1
)
{
ul
.
unlock
();
//So the notified thread will have better chance to accuire the lock immediatly..
_item_popped_cond
.
notify_all
();
}
_item_popped_cond
.
notify_one
();
return
true
;
}
// Pop a copy of the front item in the queue into the given item ref.
// If the queue is empty, block the calling thread util there is item to pop.
// Pop a copy of the front item in the queue into the given item ref.
// If the queue is empty, block the calling thread util there is item to pop.
void
pop
(
T
&
item
)
{
while
(
!
pop
(
item
,
std
::
chrono
::
hours
(
1
)));
}
// Clear the queue
// Clear the queue
void
clear
()
{
{
...
...
@@ -143,6 +138,16 @@ private:
std
::
mutex
_mutex
;
std
::
condition_variable
_item_pushed_cond
;
std
::
condition_variable
_item_popped_cond
;
inline
bool
is_full
()
{
return
_q
.
size
()
>=
_max_size
;
}
inline
bool
is_empty
()
{
return
_q
.
size
()
==
0
;
}
};
}
...
...
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