Skip to content

Commit 5e406cd

Browse files
committed
Improved C++11 threads usage.
1 parent 318a4d9 commit 5e406cd

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

SocketTest/Tests.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,14 @@ TEST_F(TCPTest, TestLoopback)
159159
ASSERT_NO_THROW(m_pTCPServer.reset(new CTCPServer(PRINT_LOG, TCP_SERVER_PORT)));
160160

161161
#ifdef WINDOWS
162-
std::future<bool> futListen = std::async([&] { return m_pTCPServer->Listen(ConnectedClient); });
162+
// Not always starts a new thread, std::launch::async must be passed to force it.
163+
std::future<bool> futListen = std::async(std::launch::async,
164+
[&] { return m_pTCPServer->Listen(ConnectedClient); });
163165
#else
164166
auto ListenTask = [&] { return m_pTCPServer->Listen(ConnectedClient); };
165-
std::thread ListenThread(ListenTask);
167+
std::packaged_task< bool(void) > packageListen { ListenTask };
168+
std::future<bool> futListen = packageListen.get_future();
169+
std::thread ListenThread { std::move(packageListen) }; // pack. task is not copyable
166170
#endif
167171

168172
// client side
@@ -171,16 +175,16 @@ TEST_F(TCPTest, TestLoopback)
171175
unsigned iPeriod = 50 + (rand() % (999 - 50));
172176

173177
// give time to let the server object reach the accept instruction.
174-
#ifdef LINUX
175-
usleep(500000);
176-
#else
177-
Sleep(500);
178-
#endif
178+
SleepMs(500);
179179

180180
ASSERT_TRUE(m_pTCPClient->Connect("localhost", "6669"));
181181
#ifdef WINDOWS
182182
ASSERT_TRUE(futListen.get());
183183
#else
184+
/* with std::thread we can't easily get the result of Listen
185+
* unlike std::async/promise/packaged_task
186+
*/
187+
ASSERT_TRUE(futListen.get());
184188
ListenThread.join();
185189
#endif
186190
ASSERT_FALSE(ConnectedClient == INVALID_SOCKET);
@@ -200,12 +204,8 @@ TEST_F(TCPTest, TestLoopback)
200204
EXPECT_GT(m_pTCPServer->Receive(ConnectedClient, szRcvBuffer, 13), 0);
201205
EXPECT_EQ(strSendData, szRcvBuffer);
202206
memset(szRcvBuffer, '\0', 14);
203-
204-
#ifdef LINUX
205-
usleep(iPeriod*1000);
206-
#else
207-
Sleep(iPeriod);
208-
#endif
207+
208+
SleepMs(iPeriod);
209209
}
210210

211211
// disconnect
@@ -259,11 +259,12 @@ TEST_F(SSLTCPTest, TestLoopback)
259259
//m_pSSLTCPServer->SetSSLCerthAuth(CERT_AUTH_FILE); // not mandatory
260260

261261
#ifdef WINDOWS
262-
std::future<bool> futListen = std::async([&]() -> bool
262+
// Not always starts a new thread, std::launch::async must be passed to force it.
263+
std::future<bool> futListen = std::async(std::launch::async,
264+
[&]() -> bool
263265
{
264266
// give time to let the server object reach the accept instruction.
265-
for (int iSec = 0; iSec < 1; ++iSec)
266-
Sleep(1000);
267+
SleepMs(1000);
267268

268269
//m_pSSLTCPClient->SetSSLCerthAuth(CERT_AUTH_FILE); // not mandatory
269270
//m_pSSLTCPClient->SetSSLKeyFile(SSL_KEY_FILE); // not mandatory
@@ -273,14 +274,8 @@ TEST_F(SSLTCPTest, TestLoopback)
273274
auto ConnectTask = [&]() -> bool
274275
{
275276
// give time to let the server object reach the accept instruction.
276-
#ifdef LINUX
277-
std::cout << "** Connect task : delay\n";
278-
for (int iSec = 0; iSec < 2000; ++iSec)
279-
usleep(1000);
280-
#else
281-
for (int iSec = 0; iSec < 2; ++iSec)
282-
Sleep(1000);
283-
#endif
277+
std::cout << "** Connect task : delay 2 seconds\n";
278+
SleepMs(2000);
284279
std::cout << "** Connect task : begin connect\n";
285280

286281
//m_pSSLTCPClient->SetSSLKeyFile(SSL_KEY_FILE); // not mandatory
@@ -290,14 +285,20 @@ TEST_F(SSLTCPTest, TestLoopback)
290285
std::cout << "** Connect task : end connect\n";
291286
return bRet;
292287
};
293-
std::thread ConnectThread(ConnectTask);
288+
std::packaged_task< bool(void) > packageConnect { ConnectTask };
289+
std::future<bool> futConnect = packageConnect.get_future();
290+
std::thread ConnectThread { std::move(packageConnect) }; // pack. task is not copyable
294291
#endif
295292

296293
m_pSSLTCPServer->Listen(ConnectedClient);
297294

298295
#ifdef WINDOWS
299296
ASSERT_TRUE(futListen.get());
300297
#else
298+
/* with std::thread we can't easily get the result of Listen
299+
* contrary to std::async/promise/packaged_task
300+
*/
301+
ASSERT_TRUE(futConnect.get());
301302
ConnectThread.join();
302303
#endif
303304

@@ -325,11 +326,7 @@ TEST_F(SSLTCPTest, TestLoopback)
325326
EXPECT_EQ(strSendData, szRcvBuffer);
326327
memset(szRcvBuffer, '\0', 14);
327328

328-
#ifdef LINUX
329-
usleep(iPeriod * 1000);
330-
#else
331-
Sleep(iPeriod);
332-
#endif
329+
SleepMs(iPeriod);
333330
}
334331

335332
// disconnect

SocketTest/test_utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,8 @@ bool GetFileTime(const char* const & pszFilePath, time_t& tLastModificationTime)
169169

170170
return false;
171171
}
172+
173+
void SleepMs(int iMilisec)
174+
{
175+
std::this_thread::sleep_for(std::chrono::milliseconds(iMilisec));
176+
}

SocketTest/test_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <algorithm>
55
#include <cerrno>
6+
#include <chrono>
67
#include <cmath>
78
#include <cstdio>
89
#include <cstdlib>
@@ -50,4 +51,6 @@ int TestUPProgressCallback(void* ptr, double dTotalToDownload, double dNowDownlo
5051
long GetGMTOffset();
5152
bool GetFileTime(const char* const & pszFilePath, time_t& tLastModificationTime);
5253

54+
void SleepMs(int iMilisec);
55+
5356
#endif

0 commit comments

Comments
 (0)