This commit is contained in:
Kevin Wojniak 2021-01-02 10:22:03 -05:00 committed by GitHub
commit 96daf60e50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 5 deletions

View File

@ -12,25 +12,29 @@ using namespace std;
uiMultilineEntry *e; uiMultilineEntry *e;
condition_variable cv; condition_variable cv;
mutex m; mutex m;
unique_lock<mutex> ourlock(m);
thread *timeThread; thread *timeThread;
bool quitting = false;
void sayTime(void *data) void sayTime(void *data)
{ {
char *s = (char *) data; char *s = (char *) data;
uiMultilineEntryAppend(e, s); uiMultilineEntryAppend(e, s);
delete s; delete[] s;
} }
void threadproc(void) void threadproc(void)
{ {
ourlock.lock(); while (!quitting) {
while (cv.wait_for(ourlock, chrono::seconds(1)) == cv_status::timeout) {
time_t t; time_t t;
char *base; char *base;
char *s; char *s;
{
unique_lock<mutex> ourlock(m);
(void)cv.wait_for(ourlock, chrono::seconds(1));
}
t = time(NULL); t = time(NULL);
base = ctime(&t); base = ctime(&t);
s = new char[strlen(base) + 1]; s = new char[strlen(base) + 1];
@ -41,6 +45,7 @@ void threadproc(void)
int onClosing(uiWindow *w, void *data) int onClosing(uiWindow *w, void *data)
{ {
quitting = true;
cv.notify_all(); cv.notify_all();
// C++ throws a hissy fit if you don't do this // C++ throws a hissy fit if you don't do this
// we might as well, to ensure no uiQueueMain() gets in after uiQuit() // we might as well, to ensure no uiQueueMain() gets in after uiQuit()
@ -82,7 +87,6 @@ int main(void)
uiBoxAppend(b, uiControl(e), 1); uiBoxAppend(b, uiControl(e), 1);
// timeThread needs to lock ourlock itself - see http://stackoverflow.com/a/34121629/3408572 // timeThread needs to lock ourlock itself - see http://stackoverflow.com/a/34121629/3408572
ourlock.unlock();
timeThread = new thread(threadproc); timeThread = new thread(threadproc);
uiWindowOnClosing(w, onClosing, NULL); uiWindowOnClosing(w, onClosing, NULL);