Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/cpp-multithreaded: fixed locks and timeThread terminate conditions #434

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions examples/cpp-multithread/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ using namespace std;
uiMultilineEntry *e;
condition_variable cv;
mutex m;
unique_lock<mutex> ourlock(m);
thread *timeThread;
volatile bool running = true;
marioc64 marked this conversation as resolved.
Show resolved Hide resolved

void sayTime(void *data)
{
Expand All @@ -25,8 +25,9 @@ void sayTime(void *data)

void threadproc(void)
{
ourlock.lock();
while (cv.wait_for(ourlock, chrono::seconds(1)) == cv_status::timeout) {
unique_lock<mutex> ourlock(m);
while (running) {
cv.wait_for(ourlock, chrono::seconds(1));
time_t t;
char *base;
char *s;
Expand All @@ -41,6 +42,10 @@ void threadproc(void)

int onClosing(uiWindow *w, void *data)
{
{
unique_lock<mutex> l(m);
running = false;
}
cv.notify_all();
// C++ throws a hissy fit if you don't do this
// we might as well, to ensure no uiQueueMain() gets in after uiQuit()
Expand Down Expand Up @@ -81,8 +86,6 @@ int main(void)

uiBoxAppend(b, uiControl(e), 1);

// timeThread needs to lock ourlock itself - see http://stackoverflow.com/a/34121629/3408572
ourlock.unlock();
timeThread = new thread(threadproc);

uiWindowOnClosing(w, onClosing, NULL);
Expand Down