Skip to content

Commit e4d4f63

Browse files
committed
Address more review comments and improve spacing
1 parent 5cb1a39 commit e4d4f63

2 files changed

Lines changed: 32 additions & 24 deletions

File tree

src/server.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ static CTimingMeas JitterMeas ( 1000, "test2.dat" ); JitterMeas.Measure(); // TE
846846
// prepare and decode connected channels
847847
if ( !bUseMT )
848848
{
849+
// run the OPUS decoder for all data blocks
849850
DecodeReceiveDataBlocks ( this, 0, iNumClients - 1, iNumClients );
850851
}
851852
else
@@ -977,6 +978,8 @@ static CTimingMeas JitterMeas ( 1000, "test2.dat" ); JitterMeas.Measure(); // TE
977978
}
978979
}
979980

981+
// This is a static method used as a callback, and does not inherit a "this" pointer,
982+
// so it is necessary for the server instance to be passed as a parameter.
980983
void CServer::DecodeReceiveDataBlocks ( CServer* pServer,
981984
const int iStartChanCnt,
982985
const int iStopChanCnt,
@@ -989,6 +992,8 @@ void CServer::DecodeReceiveDataBlocks ( CServer* pServer,
989992
}
990993
}
991994

995+
// This is a static method used as a callback, and does not inherit a "this" pointer,
996+
// so it is necessary for the server instance to be passed as a parameter.
992997
void CServer::MixEncodeTransmitDataBlocks ( CServer* pServer,
993998
const int iStartChanCnt,
994999
const int iStopChanCnt,
@@ -1101,7 +1106,7 @@ void CServer::DecodeReceiveData ( const int iChanCnt,
11011106
// get current number of OPUS coded bytes
11021107
const int iCeltNumCodedBytes = vecChannels[iCurChanID].GetCeltNumCodedBytes();
11031108

1104-
for ( int iB = 0; iB < vecNumFrameSizeConvBlocks[iChanCnt]; iB++ )
1109+
for ( size_t iB = 0; iB < (size_t)vecNumFrameSizeConvBlocks[iChanCnt]; iB++ )
11051110
{
11061111
// get data
11071112
const EGetDataStat eGetStat = vecChannels[iCurChanID].GetData ( vecvecbyCodedData[iChanCnt], iCeltNumCodedBytes );
@@ -1410,10 +1415,10 @@ void CServer::MixEncodeTransmitData ( const int iChanCnt,
14101415
// optimization it would be better to set it only if the network frame size is changed
14111416
opus_custom_encoder_ctl ( pCurOpusEncoder, OPUS_SET_BITRATE ( CalcBitRateBitsPerSecFromCodedBytes ( iCeltNumCodedBytes, iClientFrameSizeSamples ) ) );
14121417

1413-
for ( int iB = 0; iB < vecNumFrameSizeConvBlocks[iChanCnt]; iB++ )
1418+
for ( size_t iB = 0; iB < (size_t)vecNumFrameSizeConvBlocks[iChanCnt]; iB++ )
14141419
{
14151420
iUnused = opus_custom_encode ( pCurOpusEncoder,
1416-
&vecsSendData[(long)iB * SYSTEM_FRAME_SIZE_SAMPLES * vecNumAudioChannels[iChanCnt]],
1421+
&vecsSendData[iB * SYSTEM_FRAME_SIZE_SAMPLES * vecNumAudioChannels[iChanCnt]],
14171422
iClientFrameSizeSamples,
14181423
&vecvecbyCodedData[iChanCnt][0],
14191424
iCeltNumCodedBytes );

src/threadpool.h

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,44 +44,47 @@ class CThreadPool
4444
auto enqueue(F&& f, Args&&... args)
4545
-> std::future<typename std::result_of<F(Args...)>::type>;
4646
~CThreadPool();
47+
4748
private:
4849
// need to keep track of threads so we can join them
49-
std::vector< std::thread > workers;
50+
std::vector< std::thread > workers;
5051
// the task queue
5152
std::queue< std::function<void()> > tasks;
5253

5354
// synchronization
54-
std::mutex queue_mutex;
55-
std::condition_variable condition;
56-
bool stop;
55+
std::mutex queue_mutex;
56+
std::condition_variable condition;
57+
bool stop;
5758
};
5859

5960
// the constructor just launches some amount of workers
6061
inline CThreadPool::CThreadPool(size_t threads)
6162
: stop(false)
6263
{
63-
for(size_t i = 0;i<threads;++i)
64-
workers.emplace_back(
65-
[this]
66-
{
67-
for(;;)
64+
for ( size_t i = 0; i < threads; ++i ) {
65+
workers.emplace_back ( [this] {
66+
for (;;)
6867
{
6968
std::function<void()> task;
7069

7170
{
72-
std::unique_lock<std::mutex> lock(this->queue_mutex);
73-
this->condition.wait(lock,
74-
[this]{ return this->stop || !this->tasks.empty(); });
75-
if(this->stop && this->tasks.empty())
71+
std::unique_lock<std::mutex> lock ( this->queue_mutex );
72+
this->condition.wait ( lock,
73+
[this] { return this->stop || !this->tasks.empty(); } );
74+
75+
if ( this->stop && this->tasks.empty() ) {
7676
return;
77-
task = std::move(this->tasks.front());
77+
}
78+
79+
task = std::move ( this->tasks.front() );
7880
this->tasks.pop();
7981
}
8082

8183
task();
8284
}
8385
}
8486
);
87+
}
8588
}
8689

8790
// add new work item to the pool
@@ -91,19 +94,19 @@ auto CThreadPool::enqueue(F&& f, Args&&... args)
9194
{
9295
using return_type = typename std::result_of<F(Args...)>::type;
9396

94-
auto task = std::make_shared< std::packaged_task<return_type()> >(
97+
auto task = std::make_shared< std::packaged_task<return_type()> > (
9598
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
9699
);
97100

98101
std::future<return_type> res = task->get_future();
99102
{
100-
std::unique_lock<std::mutex> lock(queue_mutex);
103+
std::unique_lock<std::mutex> lock ( queue_mutex );
101104

102105
// don't allow enqueueing after stopping the pool
103-
if(stop)
104-
throw std::runtime_error("enqueue on stopped CThreadPool");
106+
if ( stop )
107+
throw std::runtime_error( "enqueue on stopped CThreadPool" );
105108

106-
tasks.emplace([task](){ (*task)(); });
109+
tasks.emplace ( [task] () { (*task)(); } );
107110
}
108111
condition.notify_one();
109112
return res;
@@ -113,11 +116,11 @@ auto CThreadPool::enqueue(F&& f, Args&&... args)
113116
inline CThreadPool::~CThreadPool()
114117
{
115118
{
116-
std::unique_lock<std::mutex> lock(queue_mutex);
119+
std::unique_lock<std::mutex> lock ( queue_mutex );
117120
stop = true;
118121
}
119122
condition.notify_all();
120-
for(std::thread &worker: workers)
123+
for ( std::thread &worker: workers )
121124
worker.join();
122125
}
123126

0 commit comments

Comments
 (0)