-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
81 lines (68 loc) · 2.05 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <condition_variable>
#include <mutex>
#include <thread>
#include <iostream>
#include <gtk/gtk.h>
#include <string>
#include "gui.hpp"
#include "ga.hpp"
#include "chromosome.hpp"
#include "population.hpp"
#include "utils.hpp"
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
GUI* gui = nullptr;
#include <future>
extern std::future<void> gaDoneFuture;
extern void testSelection();
gboolean checkGADone(gpointer data) {
if (gaDoneFuture.wait_for(std::chrono::seconds(0)) ==
std::future_status::ready) {
std::cout << "GA is done!" << std::endl;
// The GA is done, so show the results
GUI::showResults();
return FALSE; // Returning FALSE removes this function from the timeout
// handlers.
}
return TRUE; // Returning TRUE keeps the timeout handler active.
}
void guiThread() {
std::unique_lock<std::mutex> lock(mtx);
// In your main loop, check if the GA is done
while (!ready) { // if not ready, wait until main() sends data
cv.wait(lock); // release lock and wait
}
// after the wait, we own the lock again
g_timeout_add(100, GUI::start, NULL);
}
int main(int argc, char *argv[]) {
srand(time(NULL)); // seed the random number generator
testSelection();
testCrossover();
std::cout << "Welcome to the Game of Life!" << std::endl;
gtk_init(&argc, &argv);
std::string boardFile;
if (argc > 1) {
boardFile = argv[1];
std::cout << "Loading board from file: " << boardFile << std::endl;
} else {
std::cout << "No board file specified, starting with blank board"
<< std::endl;
boardFile = "";
}
gui = new GUI(boardFile); // Create the GUI in the main thread
std::thread t1(guiThread);
// do some work
{
std::lock_guard<std::mutex> lock(mtx);
ready = true; // set data as ready
}
cv.notify_one(); // notify the waiting thread
g_timeout_add(100, checkGADone,
NULL); // Check if GA is done every 100ms
gtk_main(); // start the main GTK loop
t1.join();
delete gui; // Don't forget to delete the GUI when you're done with it
return 0;
}