Threads, Timer or Mainloop...Which one?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Threads, Timer or Mainloop...Which one?

Post by Ravilj » Tue Sep 13, 2005 11:11 pm

I currently have a pretty gui program programmed and working, the gui is required to display data that it gets from a video feed.

Now I am trying to work out what is the best method to read in the info, process it and than output it to the gui. I would assume my options are as follows:
1. Threading
2. Timer
3. Mainloop

Now I dont think mainloop will work because I assume the program will freeze until the processing is complete.

I could use the time class to generate an event to sample the data and than process it and output it.

The other thought I had was to thread the data acquisition and let that continually update the gui.

I dont have enough experience to make a choice so I am asking for a little guidance here. I am currently leaning toward the threading option, will be challenging but I think it will do the job nicely.

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Wed Sep 14, 2005 5:53 am

I go with the thread to do all the processing and then fire some event to update the GUI, the data will be locked by the GUI and the GUI will be updated, then release the lock.

RogerIrwin
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Aug 25, 2005 11:51 am

How does it get data?

Post by RogerIrwin » Wed Sep 14, 2005 7:24 am

Maybe the video source could allready generate events for you (for example sockets).

If not then you have the option of polling it on a timer or using a thread. The thread option is generally the best for this type of work **but** threads are generally best avoided if not strictly necessary, especially for cross-platform work.

The polled timer option is often considered messy because if you set the timer period short you can be wasting a lot of time polling when there is no data. If you set the timer high then you will have a low latency and when you do get you data you could have a big chunk to process and that impacts the GUI.

However, in **your** application (video) the data feed should be more or less constant, so the polled option should be quite workable, and hence prefferable.

Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj » Wed Sep 14, 2005 10:49 am

Hmm, I think I am going to go with the threading option though the timer may be easier.

Now for a couple questions regarding how I go about implementing it.

I have the current layout:

App
|
Frame
|___________________________
|....................................................|
Splitter...........................................Video Feed Dialog
|___________________________
|.....................................................|
Notebook.........................................OpenGLCanvas
|____________
|..........................|
Display page.........Diag page


I currently have a class that does the calculations. It has a pointer to a class that does the image processing. It will also require a pointer to the Video feed dialog to sample the images.

The calculations class will need to update the Display Page and the OpenGLCanvas class with the results. While the diagnosis page will need to be able to access the information from the calulations class.

Any suggestions as to the best way to implement the above using threading?
Should the calculations class inherit from the thread class?
Where would I define the thread in the program structure?

Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj » Wed Sep 14, 2005 10:04 pm

I see there is also an idle func event but I still think threading will be best suited for the application. So the question in the post above still stands :P

priyank_bolia
wxWorld Domination!
wxWorld Domination!
Posts: 1339
Joined: Wed Aug 03, 2005 8:10 am
Location: BANGALORE, INDIA
Contact:

Post by priyank_bolia » Thu Sep 15, 2005 5:03 am

Ya, agreed there is an idle event processing also, but it will be called if there are other events in the event loop and after they all are processed, also to do partial processing is a difficult task, as vedio processing is a huge task, so you can't do all in one shot. Also the management and priority makes the thread best thing, but as spiderman says "great power comes with great responsibility", managing threads and synchronsing data is a great responsibility.

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Thu Sep 15, 2005 6:09 am

Hehe actually it was "Uncle Ben" who said that to Spiderman ;-)
Spiderman used to be one of my favorite comics to watch..

- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

kaffee
In need of some credit
In need of some credit
Posts: 3
Joined: Sun Aug 29, 2004 9:44 am
Location: Finland

Post by kaffee » Sat Sep 17, 2005 10:25 am

One vote for the background thread. This way your user interface doesn't freeze if the processing takes a bit longer than expected and the user has the possibility to cancel the processing by closing window etc.

Ravilj
Knows some wx things
Knows some wx things
Posts: 40
Joined: Mon Aug 22, 2005 3:49 pm

Post by Ravilj » Sun Sep 18, 2005 6:25 pm

Yup went the threading way and so far so good no problems, I have still to add the majority of the processing but the thread and such is working well :D

Post Reply