GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 7:04 am

All times are UTC




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Should I use multithreading or not?
PostPosted: Tue Jan 15, 2008 4:15 pm 
Offline
Familiar Face

Joined: Tue Jan 08, 2008 7:39 pm
Posts: 9
I'm working on a program which will basically do a scan when the user presses a button. This scan will continue until they press the button again.

Would it be better/easier to create a thread once the button is pressed and then kill the thread once it is pressed again?

What are your thoughts?


Brian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 5:51 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
If you dont use threads then once you press that button your app will probly lockup untill the scan is finished.

So i would say yes create a thread when the button is pressed and kill it when your done with your scan.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 6:37 pm 
Offline
GTK+ Guru

Joined: Fri Jan 04, 2008 3:17 pm
Posts: 183
Location: France (92340 Bourg La Reine)
Multi-threading is not always necessary but may be helpful. The issue is synchronisation.

However, you might perhaps consider programming your scan in small steps, each called in an idle routine. (if you are interested in the theoretical buzzwords, learn about continuation-passing style which formalizes this intuition).

It actually depends of the kind of scan you are doing.

_________________
Basile Starynkevitch (France)
http://starynkevitch.net/Basile/


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 7:38 pm 
Offline
Familiar Face

Joined: Tue Jan 08, 2008 7:39 pm
Posts: 9
Alright, I believe I'm going to just create and destroy threads when the button is pressed.

I began looking into continuation passing style which seemed interesting and could work, but I feel with my lack of knowledge of CPS I'd be better off with threads.

Also, the scan looks for local bluetooth devices.

Thanks, Brian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 15, 2008 9:18 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
If the only thing the GUI needs to respond to is a "Cancel" button press, what I usually do is have my scan routine periodically call a function which updates a progress bar and gets the GUI to "catch up" using:

Code:
while (gtk_events_pending()) gtk_main_iteration();


This allows the progress bar update (or label or statusbar) to take effect and also runs any callbacks such as the user clicking a cancel button. The cancel button callback will trip a "cancel" flag which is a static boolean variable which the scan function checks each iteration.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 18, 2008 3:22 pm 
Offline
Familiar Face

Joined: Tue Jan 08, 2008 7:39 pm
Posts: 9
Thats very interesting. I don't think it would work in my situation because I am using a scan to verify if a bluetooth device is still in range.

What happens if you allocate memory in a thread and kill the thread before the allocated memory is freed? Will that cause memory leaks?

Brian


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 21, 2008 4:41 pm 
Offline

Joined: Mon Jan 21, 2008 9:03 am
Posts: 4
Memory allocation is a global thing, memory allocated in thread A can be used in thread B. So it doesn't mention that you kill thread before freeing memory allocated by it. You can still free this memory from other thread if you hold pointer to this memory. If you won't free it at all it will simply leak ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group