Use std::thread for NetworkClient threads

This commit is contained in:
Adam Honse 2020-04-29 17:09:44 -05:00
parent 81acc47cd6
commit 04b87ce2eb
2 changed files with 14 additions and 36 deletions

View file

@ -13,29 +13,6 @@ static void Sleep(unsigned int milliseconds)
}
#endif
//Thread functions have different types in Windows and Linux
#ifdef WIN32
#define THREAD static void
#define THREADRETURN
#else
#define THREAD static void*
#define THREADRETURN return(NULL);
#endif
THREAD connection_thread(void *param)
{
NetworkClient* client = static_cast<NetworkClient*>(param);
client->ConnectionThread();
THREADRETURN
}
THREAD listen_thread(void *param)
{
NetworkClient* client = static_cast<NetworkClient*>(param);
client->ListenThread();
THREADRETURN
}
NetworkClient::NetworkClient(std::vector<RGBController *>& control) : controllers(control)
{
unsigned int requested_controllers;
@ -47,14 +24,10 @@ NetworkClient::NetworkClient(std::vector<RGBController *>& control) : controller
requested_controllers = 0;
//Start the connection thread
#ifdef WIN32
_beginthread(connection_thread, 0, this);
_beginthread(listen_thread, 0, this);
#else
pthread_t thread;
pthread_create(&thread, NULL, &connection_thread, this);
pthread_create(&thread, NULL, &listen_thread, this);
#endif
ConnectionThread = new std::thread(&NetworkClient::ConnectionThreadFunction, this);
//Start the listener thread
ListenThread = new std::thread(&NetworkClient::ListenThreadFunction, this);
//Wait for server to connect
while(!server_connected)
@ -97,7 +70,7 @@ NetworkClient::NetworkClient(std::vector<RGBController *>& control) : controller
}
}
void NetworkClient::ConnectionThread()
void NetworkClient::ConnectionThreadFunction()
{
//This thread manages the connection to the server
while(1)
@ -117,7 +90,7 @@ void NetworkClient::ConnectionThread()
}
}
void NetworkClient::ListenThread()
void NetworkClient::ListenThreadFunction()
{
printf("Network client listener started\n");
//This thread handles messages received from the server

View file

@ -2,6 +2,8 @@
#include "NetworkProtocol.h"
#include "net_port.h"
#include <thread>
#pragma once
class NetworkClient
@ -9,8 +11,8 @@ class NetworkClient
public:
NetworkClient(std::vector<RGBController *>& control);
void ConnectionThread();
void ListenThread();
void ConnectionThreadFunction();
void ListenThreadFunction();
void ProcessReply_ControllerCount(unsigned int data_size, char * data);
void ProcessReply_ControllerData(unsigned int data_size, char * data, unsigned int dev_idx);
@ -36,4 +38,7 @@ private:
net_port port;
bool server_connected;
unsigned int server_controller_count;
};
std::thread * ConnectionThread;
std::thread * ListenThread;
};