Scope for static function in button click function
Added by jupiter hce about 3 years ago
Hi,
Sorry I am new not just to Wt but to web development, my main C++ development is limited to Linux applications and embedded system, things worked in Linux applications are no longer work in Web application. I have a static object works all my normal Linux applications, but it did not work in my WebApplication class, it starts initializing a static instance:
::myLibrary::StaticObject::Instance().Start();
.....
cleanButton->clicked().connect(this, &WebApplication::Clean);
The WebApplication::Clean calls the StaticObject, but when I clicked button, it called WebApplication::Clean() and the program was crashed because that static instance object was no longer available in WebApplication::Clean.
It seems that the button click and connect created another process / thread?
Replies (2)
RE: Scope for static function in button click function - Added by Korneel Dumon about 3 years ago
Hi jupiter,
the most common use of Wt is with a thread-pool. The event-handling is assigned to a random thread whenever the server receives a request, so you would need to manage access to your StaticObject somehow.
It is also possible to build Wt without threading, by setting MULTI_THREADED to false. (see also https://www.webtoolkit.eu/wt/doc/reference/html/InstallationUnix.html)
RE: Scope for static function in button click function - Added by jupiter hce about 3 years ago
Thanks Korneel, running with --threads 1 works fine.
Thank you very much.
Kind regards.