Project

General

Profile

Support #7314

Worker-Thread-Warning: "Wt: attachThread(): no thread is holding this application's lock?"

Added by Fabian V about 2 years ago. Updated about 2 years ago.

Status:
New
Priority:
High
Assignee:
-
Target version:
-
Start date:
11/07/2019
Due date:
% Done:

0%

Estimated time:

Description

Using Wt 4.1.1

I'm running a Wt::WApplication with a worker thread. The worker runs the following code:

// worker thread function
// UiApp inherits from Wt::WApplication
void UiApp::work()
{
  attachThread(true);
  enableUpdates();

  while(true) {
    try {
      std::function<void()> func = [this]() {
        modelMediator->getSubscriptions()->insertOrUpdate();
        modelMediator->getPublishings()->insertOrUpdate();
      };

      Wt::WServer::instance()->post(sessionId(), func);
      triggerUpdate();
      std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    catch(const std::exception& e) {
      logger->error("[ControlTabWidget::work] error running gui update thread: " + std::string(e.what()));
    }

    if(!isRunning) {
      attachThread(false);
      return;
    }
  }
}

// function that's binded to a button to start/stop the worker-thread.
void UiApp::doStartStopGuiUpdate()
{
  if(startStopGuiUpdateButton->isChecked()) {
    if(isRunning) {
      isRunning = false;
    }
    startStopGuiUpdateButton->setText("Start Gui");
  }
  else {
    if(!isRunning) {
      if(worker.joinable()) {
        worker.join();
      }
      worker = std::thread(std::bind(&UiApp::work, this));
      isRunning = true;
    }
    startStopGuiUpdateButton->setText("Stop Gui");
  }
}

So my first question is: Is this code using Wt::WServer::instance()->post() correct?

When I start multiple sessions in different browsers some threads/sessions create the following warning in the logfile.log:

"Wt: attachThread(): no thread is holding this application's lock?"

And my second question is: What can I do against this warning? Should I ignore it?

Thank you very much.

#1

Updated by Roel Standaert about 2 years ago

So you're starting a thread that attaches itself to the WApplication without getting the update lock?

You need to have the update lock when updating a WApplication, so you will have to either use post(), or grab the UpdateLock within another thread.

Also, you need to call enableUpdates at a point where you have the UpdateLock.

See the serverpush example for an example where UpdateLock is used: https://github.com/emweb/wt/tree/master/examples/feature/serverpush

Also available in: Atom PDF