hack.cpp: modern cpp STL threads
This commit is contained in:
parent
4739761575
commit
95dc6ba5cc
@ -32,6 +32,5 @@ extern bool initialized;
|
||||
const std::string &GetVersion();
|
||||
const std::string &GetType();
|
||||
void Initialize();
|
||||
void Think();
|
||||
void Shutdown();
|
||||
} // namespace hack
|
||||
|
@ -6,36 +6,22 @@
|
||||
*/
|
||||
|
||||
#include "common.hpp"
|
||||
#include <pthread.h>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
#include "hack.hpp"
|
||||
|
||||
pthread_mutex_t mutex_quit;
|
||||
pthread_t thread_main;
|
||||
static std::atomic isStopping = false;
|
||||
static std::thread thread_main;
|
||||
|
||||
bool IsStopping(pthread_mutex_t *mutex_quit_l)
|
||||
void *MainThread()
|
||||
{
|
||||
if (!pthread_mutex_trylock(mutex_quit_l))
|
||||
{
|
||||
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;
|
||||
std::this_thread::sleep_for(std::chrono_literals::operator""s(50));
|
||||
hack::Initialize();
|
||||
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();
|
||||
logging::Shutdown();
|
||||
@ -44,17 +30,14 @@ void *MainThread(void *arg)
|
||||
|
||||
void __attribute__((constructor)) attach()
|
||||
{
|
||||
// std::string test_str = "test";
|
||||
pthread_mutex_init(&mutex_quit, 0);
|
||||
pthread_mutex_lock(&mutex_quit);
|
||||
pthread_create(&thread_main, 0, MainThread, &mutex_quit);
|
||||
thread_main = std::thread(MainThread);
|
||||
}
|
||||
|
||||
void detach()
|
||||
{
|
||||
logging::Info("Detaching");
|
||||
pthread_mutex_unlock(&mutex_quit);
|
||||
pthread_join(thread_main, 0);
|
||||
isStopping = true;
|
||||
thread_main.join();
|
||||
}
|
||||
|
||||
void __attribute__((destructor)) deconstruct()
|
||||
|
@ -605,11 +605,6 @@ free(logname);*/
|
||||
}
|
||||
}
|
||||
|
||||
void hack::Think()
|
||||
{
|
||||
usleep(250000);
|
||||
}
|
||||
|
||||
void hack::Shutdown()
|
||||
{
|
||||
if (hack::shutdown)
|
||||
|
Reference in New Issue
Block a user