Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Starl1ght/d6a54df31961087fea8a762051ee4aeb to your computer and use it in GitHub Desktop.
Save Starl1ght/d6a54df31961087fea8a762051ee4aeb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <mutex>
#include <chrono>
#include <list>
using namespace std::chrono_literals;
std::string panda;
std::mutex mt;
std::condition_variable cv;
void th();
class Intz {
public:
Intz(int i) : mi{ static_cast<float>(i) }, to{ i }, speed{ 0.f }, moving{ false } {};
void Increase(int to, uint32_t over_ms) {
this->to = to;
speed = static_cast<float>(to - mi) / over_ms;
moving = true;
}
bool Tick(uint32_t dt) {
if (moving) {
mi += speed * dt;
if (mi >= to) {
mi = to;
moving = false;
return true;
}
}
return false;
}
int Get() const { return mi; }
private:
int to;
float speed, mi;
bool moving;
};
std::list <std::weak_ptr<Intz>> gls;
void main() {
std::thread thr{ th };
while (true) {
for (auto& ptr : gls) {
if (!ptr.expired()) {
auto lock = ptr.lock();
auto ret = lock->Tick(1000);
if (ret == true) {
cv.notify_one();
} else {
std::cout << lock->Get() << std::endl;
}
} else {
// ?
}
}
}
system("pause");
thr.join();
}
/*#define SCENE(scene_name) \
void scene_name() { \
std::unique_lock<std::mutex> lk{*/
#define REGISTER(ptr) gls.push_back(std::weak_ptr<Intz>(ptr));
#define SYNC(operation) \
operation; \
cv.wait(lk);
void th() {
std::unique_lock<std::mutex> lk{ mt };
// REGISTER(
// SYNC(gaba)
// ASYNC(BIBA)
auto bbz = std::make_shared<Intz>(10);
REGISTER(bbz);
SYNC(bbz->Increase(100, 10000));
SYNC(bbz->Increase(1000, 10000));
SYNC(bbz->Increase(2000, 10000));
auto bbx = std::make_shared<Intz>(10);
SYNC(bbx->Increase(100, 5000));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment