I am trying to use another thread to update MyGUI widgets. But this sometimes causes the program crash. Anybody know how to solve this problem? Can I let MyGUI stop rendering widgets when I update gui from another thread, then let MyGUI start rendering widgets again?
Here is the example code:
void ThreadList(void *param)
MyGUI::ListPtr lst = (MyGUI::ListPtr)param;
bool add = true;
for (int i = 1; i <= 50; i++)
sprintf(buff, "Item %d", i);
//Sleep(50); //Uncomment this line, it works better but somtimes still crashes
add = !add;
MyGUI::ListPtr lst = mGUI->createWidget<MyGUI::List>("List", 50, 50, 300, 500, MyGUI::Align::Default, "Main");
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadList, lst, 0, NULL);
MyGUI isn't thread safe. You have to set locks at start and end of frame and in all your functions, where you call MyGUI
I use the framework of demo MyGUI:
class demo::DemoKeeper inherits base::BaseManager
I override method DemoKeeper::createScene() and create thread in this method. You can see the code in my post.
You said that: "You have to set locks at start and end of frame and in all your functions". Can you tell me what exactly I should do?
Under "lock" I mean set mutexes or crititical sections. I haven't use threads in C++, but I think you can look into defines definion of OGRE_*_MUTEX at http://www.ogre3d.org/docs/api/html/Ogr
I know what you want to say. I should set critical section at place where frame is rendered. But I don't know exactly where I should set critical section. Because I am a newbie in using Ogre. Can you tell me where I should set critical section?
I also have some ideas to solve this problem but I don't know if it is possible with Ogre:
- Sol #1: Stop rendering frames, update data of GUI, start rendering frames.
- Sol #2: Invoke MyGUI to update GUI later (but we don't update GUI directly)
Anybody know about this, please help me!
Where to enter and leave mutex?