473,395 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

WxWidgets multi-threading

83
Hi, can I had some expert, detailed, and understandable advise on multi-threading please. I did google loads of stuff, but all I found was stuff on accessing GUI with threads, which Is a bit ahead of what I want to learn. (I want to learn the basic wx threading).
Help is greatly appreciated.
Sep 8 '08 #1
9 4987
weaknessforcats
9,208 Expert Mod 8TB
OK. Here's an overview.

1) a thread is a sequence of instructions to execute. Like main(). Instructions are executed on after another on and on through all the function calls until main is completed. Then the thread dies.
2) You can create you own threads. These are usually functions that have a specified argument list and return type. This varies by operating system. For Windows yout thread function must be:
Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI MyThread(LPVOID);
  2.  
Any function of this format can be a thread.
3) You make an operating system call to create your thread and start is executing. For Windows you would use CreateThread.
4) Your thread exists until the function is complete. Then your thread dies.
5) main() and your thread do no know each other exists. If the thread and
main() use common variables, you will have big trouble unless you install a traffic light (research mutexes, critical sections).
6) your multithreaded program will run slower than your single threaded program. The time used by the thread is part of the time the OS scheduler has allowed for your main(). This will vary by the CPU in your machine. It will also vary by using multiple cores. This part gets deep.
7) you can talk to your thread from main() by using and event.
8) the more threads the slower it goes.
9 creating a thread is not the same a creating a child process. This is a wole other area.

If you are using Windows, read Windows via C/C++ Jeffrey Richter 2007. Multithreading is covered in great detail.

Good luck.
Sep 9 '08 #2
Adam01
83
Thanks, I'l look into it, I do know multithreading with _beginthread(), but being in a wxWidgets app is a bit more complicated.
Sep 9 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
Not really.

What you do is define a class member function to launch the thread.

Inside this function you call a static class member function, which is the actual thread.

What you is define a class static member function. Since this function requires no object you can use it to start your thread.

Inside launch member function you call _beginthread() using this as the argument for the thread and use a static class method for the actual thread. When this static method returns, the thread is complete and dies.

Now you have passed the this pointer as the argument to _beginthread().
Expand|Select|Wrap|Line Numbers
  1. int _beginthread(
  2.     register void ( *start_address)(void *),  <--your class static function name
  3.     void *stack_bottom,
  4.     unsigned stack_size,
  5.     void *arglist );                  <-the this pointer of the object starting the thread
  6.  
Inside the static member function (which is the thread) you typecast the arglist back to the class type that launched the thread. That is, a reinterpret_cast of arglist to the class name containing the static funciton.

Now you can use the typecast pointer to call member functions on the object.
Sep 11 '08 #4
Adam01
83
I havnt got time to test this at the moment, but doing this, would not return a type match error? something like:
"(void (*) makethread(void)) does not match (frameclass:: makethread(void*)"
Sep 11 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
"(void (*) makethread(void)) does not match (frameclass:: makethread(void*)"
All threads need the same function prototype. Look at my previous post:
Expand|Select|Wrap|Line Numbers
  1. int _beginthread(
  2.     register void ( *start_address)(void *),  <--your class static function name
  3.     void *stack_bottom,
  4.     unsigned stack_size,
  5.     void *arglist );                  <-the this pointer of the object starting the thread
  6.  
Your thread function must take a void* argument and return void. No other function can be used. That means void (*) makethread(void) cannot be used as a pointer to a thread function because it does not have a void* argument.
Sep 12 '08 #6
Adam01
83
Ok, what about calling other stuff from the static thread, like:

Expand|Select|Wrap|Line Numbers
  1. void frameclass::startthread(void*){
  2. thread();
  3. }
  4.  
  5. int frameclass::thread(){
  6. wxThing * wxobject = wxThing1;
  7. wxobject->SetValue(...);
  8. }
Just to make sure, that would all be ok if called by the thread starter?
Thanks for your help as well.
Sep 12 '08 #7
weaknessforcats
9,208 Expert Mod 8TB
void frameclass::startthread(void*){
thread();
}

int frameclass::thread(){
wxThing * wxobject = wxThing1;
wxobject->SetValue(...);
}
The void* argument is the this pointer of the frameclass object that was used to create this thread. You now cast the void* to a frameclass* and go from there:

Expand|Select|Wrap|Line Numbers
  1. void frameclass::startthread(void* arg){
  2. reinterpret_cast<frameclass*> (arg)->thread();
  3. }
  4.  
  5. int frameclass::thread(){
  6. wxThing * wxobject = wxThing1;
  7. wxobject->SetValue(...);
  8. }
  9.  
When frameclass::thread() is complete then frameclass::startthread() will be complete and the thread dies. Remember, as far as your program is concerned, frameclass::startthread is the thread. That typecast just gets you back to being able to call your class methods again.
Sep 13 '08 #8
Adam01
83
Ok, I see now, I havnt used the < > stuff yet.
Sep 13 '08 #9
Adam01
83
oh yes!, It works!
Thanks a million weaknessforcats.
Sep 13 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Nomen Nescio | last post by:
PLEASE USE WXWIDGETS Please use wxWidgets, the multiplatform C++ class library. It's free, open-source and non-commercial. It's modelled after MFC but it produces code which can be compiled...
5
by: py | last post by:
i need to design a GUI for my python app. i heard of wxWidgets and was going to look into that, but then I saw wxPython. Why would I use wxPython over wxWidgets? thanks
1
by: sc | last post by:
I'm hoping to hear from anyone who has had extensive experience with writing cross-platform GUI apps using wxWidgets? I'm particularly interested in "advanced" features such as Outlook or Google...
12
by: Simon Hibbs | last post by:
I have a simple form with some input values and some calculated values in TextCtrl widgets. What I would like to do is have the display update automaticaly when the user changes one of the input...
3
by: Alfay | last post by:
Hi everybody! I've got a problem: I want to compile a lot of mini-utilities with WxWidgets, but every executable is about 3 or 5 MB. Then I want to give separately the wxwidgets dlls, but I don't...
1
by: samuraisam | last post by:
Are there any MVC-framework-like libraries for wxWidgets or Tkinter for Python? If so, any experiences with using them? I need to develop a desktop application *fast* and experiences with the likes...
8
by: Nexcet | last post by:
Experience users with it; cons and pros? And is there anything else out there better then it? I'll be starting some research developement using it and i'm just curious for the most part. ...
1
DeMan
by: DeMan | last post by:
I built a project using wxWidgets (2.8.3) using codeBlocks, with wxWidgets built MOLITHIC=1, SHARE=0, BUILD=debug and everything was fine and dandy (asside from the 25MB exe). I tried the same by...
2
by: Hans Mull | last post by:
Hi! How can I change the background color of a wxWidgets frame? Thanks
1
by: Marcin Kalicinski | last post by:
I have an application written in C++ that uses wxWidgets for GUI. I use embedded Python for scripting inside of this application. I want to use wxPython to add GUI to my scripts - because my C++...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.