condition variable instead of busy looping
This commit is contained in:
parent
4e1dbbbecf
commit
826917ef17
@ -6,7 +6,8 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <atomic>
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
#include <ixwebsocket/IXWebSocket.h>
|
#include <ixwebsocket/IXWebSocket.h>
|
||||||
#include <ixwebsocket/IXSocket.h>
|
#include <ixwebsocket/IXSocket.h>
|
||||||
|
|
||||||
@ -25,13 +26,14 @@ namespace ix
|
|||||||
std::string _url;
|
std::string _url;
|
||||||
ix::WebSocket _webSocket;
|
ix::WebSocket _webSocket;
|
||||||
|
|
||||||
std::atomic<bool> _done;
|
|
||||||
bool _quiet;
|
bool _quiet;
|
||||||
|
|
||||||
|
std::mutex _mutex;
|
||||||
|
std::condition_variable _condition;
|
||||||
};
|
};
|
||||||
|
|
||||||
AutobahnTestCase::AutobahnTestCase(const std::string& url, bool quiet) :
|
AutobahnTestCase::AutobahnTestCase(const std::string& url, bool quiet) :
|
||||||
_url(url),
|
_url(url),
|
||||||
_done(false),
|
|
||||||
_quiet(quiet)
|
_quiet(quiet)
|
||||||
{
|
{
|
||||||
_webSocket.disableAutomaticReconnection();
|
_webSocket.disableAutomaticReconnection();
|
||||||
@ -77,7 +79,7 @@ namespace ix
|
|||||||
ss << " code " << msg->closeInfo.code;
|
ss << " code " << msg->closeInfo.code;
|
||||||
ss << " reason " << msg->closeInfo.reason << std::endl;
|
ss << " reason " << msg->closeInfo.reason << std::endl;
|
||||||
|
|
||||||
_done = true;
|
_condition.notify_one();
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
||||||
{
|
{
|
||||||
@ -97,7 +99,7 @@ namespace ix
|
|||||||
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
||||||
|
|
||||||
// And error can happen, in which case the test-case is marked done
|
// And error can happen, in which case the test-case is marked done
|
||||||
_done = true;
|
_condition.notify_one();
|
||||||
}
|
}
|
||||||
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
||||||
{
|
{
|
||||||
@ -121,12 +123,9 @@ namespace ix
|
|||||||
|
|
||||||
_webSocket.start();
|
_webSocket.start();
|
||||||
|
|
||||||
log("Waiting for being closed ...");
|
log("Waiting for test completion ...");
|
||||||
while (!_done)
|
std::unique_lock<std::mutex> lock(_mutex);
|
||||||
{
|
_condition.wait(lock);
|
||||||
std::chrono::duration<double, std::milli> duration(10);
|
|
||||||
std::this_thread::sleep_for(duration);
|
|
||||||
}
|
|
||||||
|
|
||||||
_webSocket.stop();
|
_webSocket.stop();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user