hack.cpp: modern cpp STL threads

This commit is contained in:
TotallyNotElite 2019-08-26 22:44:03 +02:00
parent 4739761575
commit 95dc6ba5cc
3 changed files with 11 additions and 34 deletions

View File

@ -32,6 +32,5 @@ extern bool initialized;
const std::string &GetVersion(); const std::string &GetVersion();
const std::string &GetType(); const std::string &GetType();
void Initialize(); void Initialize();
void Think();
void Shutdown(); void Shutdown();
} // namespace hack } // namespace hack

View File

@ -6,36 +6,22 @@
*/ */
#include "common.hpp" #include "common.hpp"
#include <pthread.h> #include <thread>
#include <atomic>
#include "hack.hpp" #include "hack.hpp"
pthread_mutex_t mutex_quit; static std::atomic isStopping = false;
pthread_t thread_main; static std::thread thread_main;
bool IsStopping(pthread_mutex_t *mutex_quit_l) void *MainThread()
{ {
if (!pthread_mutex_trylock(mutex_quit_l)) std::this_thread::sleep_for(std::chrono_literals::operator""s(50));
{
logging::Info("Shutting down, unlocking mutex");
pthread_mutex_unlock(mutex_quit_l);
return true;
}
else
{
return false;
}
return true;
}
void *MainThread(void *arg)
{
pthread_mutex_t *mutex_quit_l = (pthread_mutex_t *) arg;
hack::Initialize(); hack::Initialize();
logging::Info("Init done..."); logging::Info("Init done...");
while (!IsStopping(mutex_quit_l)) while (!isStopping)
{ {
hack::Think(); std::this_thread::sleep_for(std::chrono_literals::operator""ms(500));
} }
hack::Shutdown(); hack::Shutdown();
logging::Shutdown(); logging::Shutdown();
@ -44,17 +30,14 @@ void *MainThread(void *arg)
void __attribute__((constructor)) attach() void __attribute__((constructor)) attach()
{ {
// std::string test_str = "test"; thread_main = std::thread(MainThread);
pthread_mutex_init(&mutex_quit, 0);
pthread_mutex_lock(&mutex_quit);
pthread_create(&thread_main, 0, MainThread, &mutex_quit);
} }
void detach() void detach()
{ {
logging::Info("Detaching"); logging::Info("Detaching");
pthread_mutex_unlock(&mutex_quit); isStopping = true;
pthread_join(thread_main, 0); thread_main.join();
} }
void __attribute__((destructor)) deconstruct() void __attribute__((destructor)) deconstruct()

View File

@ -605,11 +605,6 @@ free(logname);*/
} }
} }
void hack::Think()
{
usleep(250000);
}
void hack::Shutdown() void hack::Shutdown()
{ {
if (hack::shutdown) if (hack::shutdown)