-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogAsync.cpp
86 lines (75 loc) · 2.53 KB
/
LogAsync.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//---------------------------------------------------------------------------
#pragma hdrstop
#include <System.Classes.hpp>
#include "LogAsync.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
namespace PitchDet {
LogAsync::LogAsync( CallbackType Callback )
: callback_{ Callback }
{
Run();
}
//---------------------------------------------------------------------------
LogAsync::~LogAsync()
{
stopped_ = true;
condVar_.notify_all();
thConsumer_.join();
}
//---------------------------------------------------------------------------
void LogAsync::DoLogMessage( String Text, TColor Color )
{
std::unique_lock<std::mutex> Lock( mutex_ );
queue_.emplace( Text, Color );
condVar_.notify_one();
}
//---------------------------------------------------------------------------
void LogAsync::Run()
{
thConsumer_ =
std::thread(
[this](){
LogItemType Item;
while ( !stopped_ ) {
{
std::unique_lock<std::mutex> Lock( mutex_ );
condVar_.wait(
Lock, [&](){ return !queue_.empty() || stopped_; }
);
if ( !stopped_ ) {
if ( !queue_.empty() ) {
Item = std::move( queue_.front() );
queue_.pop();
}
}
else {
break;
}
}
// Manda elemento alla GUI
TThread::Synchronize(
(TThread*)nullptr,
[this,&Item](){
callback_( Item.first, Item.second );
}
);
}
{
std::unique_lock<std::mutex> Lock( mutex_ );
while ( !queue_.empty() ) {
Item = std::move( queue_.front() );
queue_.pop();
// Manda elemento alla GUI
TThread::Synchronize(
(TThread*)nullptr,
[this,&Item](){
callback_( Item.first, Item.second );
}
);
}
}
}
);
}
} // End of namespace RTTLogger