Skip to content

Instantly share code, notes, and snippets.

@n7275
Created August 20, 2021 20:46
Show Gist options
  • Save n7275/1887182777e73d55866abf37da8e8074 to your computer and use it in GitHub Desktop.
Save n7275/1887182777e73d55866abf37da8e8074 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <mutex>
#include <thread>
#include <Windows.h>
void sinusoid(bool &die, double &t, double &val, std::mutex &m)
{
while (!die)
{
m.lock();
val = sin(t);
t = t + 0.00001;
m.unlock();
}
}
void sinprt(bool& die, double& t, double& val, std::mutex& m)
{
while (!die)
{
m.lock();
std::cout << "\t" << val << "\t\t" << t << "\n";
m.unlock();
Sleep(10);
}
}
int main()
{
double T = 0;
double VAL = 0;
bool die = false;
std::mutex M;
std::thread T1 = std::thread(sinusoid, std::ref(die), std::ref(T), std::ref(VAL), std::ref(M));
std::thread T2 = std::thread(sinprt, std::ref(die), std::ref(T), std::ref(VAL), std::ref(M));
std::cin.get();
die = true;
T1.join();
T2.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment