Created
May 24, 2021 18:40
-
-
Save markusbuchholz/07b6a25bd37f89cadd45b0240a834a1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <mutex> | |
#include <thread> | |
#include <chrono> | |
#include <unistd.h> | |
std::mutex mx; | |
//---------------------------------------------------------------------- | |
struct shMemory | |
{ | |
int shearedTimer; | |
} shm; | |
//---------------------------------------------------------------------- | |
auto get_time() | |
{ | |
return std::chrono::high_resolution_clock::now(); | |
} | |
//---------------------------------------------------------------------- | |
void resetTime(int t, bool resetTime) | |
{ | |
if (!resetTime) | |
{ | |
std::cout << "no reset in " << __FUNCTION__ << std::endl; | |
shm.shearedTimer++; | |
} | |
if (resetTime) | |
{ | |
std::cout << " ### RESET ###" << __FUNCTION__ << std::endl; | |
shm.shearedTimer = 0; | |
} | |
} | |
//---------------------------------------------------------------------- | |
void watchDog() | |
{ | |
while (true) | |
{ | |
std::this_thread::sleep_for(std::chrono::milliseconds(200)); | |
std::cout << " ======> " << __FUNCTION__ << " barking " << shm.shearedTimer << " sek" << std::endl; | |
mx.lock(); | |
resetTime(shm.shearedTimer, false); | |
mx.unlock(); | |
if (shm.shearedTimer % 10 == 0) | |
{ | |
std::cout << " Sending Alert..." << std::endl; | |
} | |
} | |
} | |
//---------------------------------------------------------------------- | |
void threadOneWorker(int sleep) | |
{ | |
auto start = get_time(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
std::cout << __FUNCTION__ << std::endl; | |
std::this_thread::sleep_for(std::chrono::milliseconds(sleep)); | |
mx.lock(); | |
resetTime(0, true); | |
mx.unlock(); | |
} | |
auto stop = get_time(); | |
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); | |
std::cout << "total exeqution time of " << __FUNCTION__ << " : " << duration.count() << std::endl; | |
} | |
//---------------------------------------------------------------------- | |
void threadTwoWorker(int sleep) | |
{ | |
auto start = get_time(); | |
for (int i = 0; i < 10000; i++) | |
{ | |
std::cout << __FUNCTION__ << std::endl; | |
std::this_thread::sleep_for(std::chrono::milliseconds(sleep)); | |
mx.lock(); | |
resetTime(0, true); | |
mx.unlock(); | |
} | |
auto stop = get_time(); | |
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); | |
std::cout << "total exeqution time of " << __FUNCTION__ << " : " << duration.count() << std::endl; | |
} | |
//---------------------------------------------------------------------- | |
int main() | |
{ | |
pid_t pid; | |
std::cout << "PID : " << getpid() << std::endl; | |
std::thread threadOne(threadOneWorker, 1900); // spawn new thread that calls threadOneWorker() | |
std::thread threadTwo(threadTwoWorker, 750); // spawn new thread that calls threadTwoWorker() | |
std::thread threadThree(watchDog); // spawn new thread that calls watchDog() | |
threadOne.join(); | |
threadTwo.join(); | |
threadThree.join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment