Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » Problem breaking loop (with close button) in main thread
Re: Problem breaking loop (with close button) in main thread [message #59048 is a reply to message #59044] Thu, 20 October 2022 20:10 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1206
Registered: August 2007
Senior Contributor
Hi Lance,

Quote:

probably should not modify GUI from within other than the GUI thread.


You are allowed to modify GUI from within other threads. For this purpose, you need to use

a) EnterGuiMutex() & LeaveGuiMutex();
b) GuiLock, which is basicly the above functions wrapped in a class for convenience:

Rudimentary example:

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

#define LAYOUTFILE <DeadMutex/DeadMutex.lay>
#include <CtrlCore/lay.h>

static std::atomic<bool> fin;

class DeadMutex : public WithDeadMutexLayout<TopWindow> {
	
	typedef DeadMutex CLASSNAME;
	
public:
	DeadMutex()
	{
		CtrlLayout(*this, "Window title");
		start.WhenPush = THISFN(Start);
		stop.WhenPush = [this]{ fin = true; };
		WhenClose = [this]{ ShutdownThreads(); Break(); };
	}
	
	void Start(){
		Thread().Run([=] {
			{
				GuiLock __; // EnterGuiMutex()
				log.Append("Badguy instance running & owned the gui mutex...\n");
				// LeaveGuiMutex();
			}
			while(!fin && !IsShutdownThreads() ){
				Sleep(50);
			}
			GuiLock __;
			log.Append("Stop requested, Badguy instance exiting...\n");
			fin = false;
		});
	}
};

GUI_APP_MAIN
{
	DeadMutex().Run();
}



There is also a GuiUnlock class which basically reverses the order of LeaveGuiMutex and EnterGuiMutex, so you can force the main thread to temporarily unlock its GUI lock.

What you are not allowed to do is, creating windows and prompts within other threads.


Best regards,
Oblivion


[Updated on: Thu, 20 October 2022 20:12]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Dealing with background tasks elegantly in a userinterface
Next Topic: error / memory leak in HttpServer example
Goto Forum:
  


Current Time: Sun May 11 17:39:19 CEST 2025

Total time taken to generate the page: 0.02482 seconds