简体   繁体   中英

how to use boost::bind with std::thread (in Wt c++)

I'm using Wt C++ framework and need to connect a push button with a class function. The below code works fine but it's needed to run the function doors_open_all on a thread to allow usage of other actions at the same time.

Wt::WPushButton *open_doors_button = new Wt::WPushButton("open all");
container_box->addWidget(open_doors_button);
open_doors_button->clicked().connect(boost::bind(&Servicemode::doors_open_all, this));

Something along the lines is needed:

open_doors_button->clicked().connect(boost::threaded_bind(&Servicemode::doors_open_all, this));

If I understand question corrently you need to run doors_open_all function in a new thread.

boost::thread constructor is internaly using boost::bind You don't need to be explicit.

So open_doors_button->clicked().connect(boost:: thread(&Servicemode::doors_open_all, this)); should do the job.

Version with boost::thread and boost::bind : open_doors_button->clicked().connect(boost::thread(boost::bind(&Servicemode::doors_open_all, this)));

Edit: You can also try using std::async for this purpouse too.

What does Servicemode::doors_open_all() do?

If I assume that what is taking a long time is just all backend stuff, so no widgets are being created, deleted or modified, then you can spawn a thread inside Servicemode::doors_open_all() to do all of that backend stuff. When it's done you have two possibilities:

  1. Use WServer::post() , passing it the session id of your WApplication and a function, to notify the application that it should update its UI, and do all of your creating, deleting and modifying of widgets in that callback.
  2. Grab the update lock of the WApplication :

     // Assuming that app is a Wt::WApplication* Wt::WApplication::UpdateLock lock(app); // lock is released when it goes out of scope 

    When you have the update lock, you can go ahead and modify the widget tree.

In any case, when doing this, you have to also do these two things:

  1. Enable server push beforehand by calling app->enableUpdates(true) . When the thread is done you can again disable server push with app->enableUpdates(false) .
  2. When you have modified the widget tree, notify Wt that it should push these changes to the client with app->triggerUpdate() .

If what takes a long time is UI-related, then there's not much you can do, since Wt assumes that you only modify a WApplication and its widgets from a single thread at a time, ie when you have the update lock. The update lock is always grabbed automatically when handling an event coming from the client, like WPushButton::clicked() .

Server push is demonstrated in the serverpush example. You can find that under examples/feature/serverpush in the Wt source tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM