Thread: using pthreads with an API that uses pthreads

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    using pthreads with an API that uses pthreads

    I am brand new to pthreads but fairly competant with gtk (which I presume uses pthreads internally).

    Anyway, I was happy this morning when I tried putting a gtk_main_loop in a function started as a thread and it worked -- for one instance. I can kind of vaguely intuit why, and was wondering if there really is no way around that, or if anyone had some pthread related tip (pretty much all of my pthreading knowledge is demonstrated here, ie. it's close to null).

    Of course, I only really need one gtk thread, since I could make as many windows as I want from there. But it would actually be easier, and more satisfying to my sense of freedom, to spawn them seperately.
    Code:
    #include <gtk/gtk.h>
    #include <pthread.h>
    
    typedef char string_[256];
    
    string_ Message="hello world";
    
    void *gtkthing (void *title) {
    	GtkWidget *window, *vbox, *info, *update;
    	printf("gtkthing()...\n");
    	
    	gtk_init(NULL,NULL);
    	window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    	gtk_window_set_title(GTK_WINDOW(window),(const gchar*)title);
    	gtk_window_set_default_size(GTK_WINDOW(window),300,400);
    
    	vbox=gtk_vbox_new(FALSE,0);
    	gtk_container_add(GTK_CONTAINER(window),vbox);
    	info=gtk_label_new((const gchar*)Message);
    	gtk_box_pack_start(GTK_BOX(vbox),info,TRUE,TRUE,0);
    
    	gtk_widget_show_all(window);
    	gtk_main();	
    	pthread_exit(NULL);
    }
    
    int main() {
    	string_ titles[2]={"first","second"};
    	pthread_t th[3];
    	
    	if (pthread_create(&th[0],NULL,gtkthing,(void*)titles[0])) puts("ERROR");
    /*	if (pthread_create(&th[1],NULL,gtkthing,(void*)titles[1])) puts("ERROR"); */
    	
    	pthread_exit(NULL);
    	return 0;			
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Not familiar with threading + GTK, but a quick google revealed: http://developer.gimp.org/api/2.0/gdk/gdk-Threads.html

    From just a pthread-usage perspective of your code, you should create your thread detached, or call pthread_detach(), or join with the thread instead of calling pthread_exit().

    gg

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Codeplug View Post
    Not familiar with threading + GTK, but a quick google revealed: http://developer.gimp.org/api/2.0/gdk/gdk-Threads.html
    From the look of the example, I would say they are still intending you to run a single gtk loop in main(). "gdk_threads" may be what gtk actually uses (and not pthreads); glib is HUGE and has a lot of general purpose datatypes (eg, hashes), etc. But thanks for pointing this out.

    What I'm actually trying to do (sucessfully thus far!) is use a gtk interface to control some openGL experiments. Unfortunately, I'm using GLUT and it segfaults if you don't start it in main(). But gtk obviously does not, so the way I'm proceeding now is to use main() for the openGL, launching a gtk thread first.

    From just a pthread-usage perspective of your code, you should create your thread detached, or call pthread_detach(), or join with the thread instead of calling pthread_exit().
    I certainly don't want to join it, or there wouldn't be much point, would there? Anyway, if I understand the API correctly, the purpose of pthread_detach is just to ensure there will be no leak when the thread ends? Or is there some greater significance (the whole API makes me really paranoid, honestly)? In this case there are only two threads, main() and the gtkthing, which both must last the duration anyway -- so should I use _detach() just for good form?

    My guess about why I can only have one thread with a gtk_main() loop is that it uses some opaque globals or something.
    Last edited by MK27; 03-06-2009 at 12:33 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> so should I use _detach() just for good form?
    Yeah. Posix says you should.

    It's kind like calling free() for every malloc() - it gives the library a chance to do whatever it needs to do. The side effects of not calling join/detach may simply be leaked memory - or something worse - or nothing at all.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Want to learn Windows API for Game Programming
    By George M. in forum Windows Programming
    Replies: 15
    Last Post: 09-28-2008, 10:26 AM
  2. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 07:46 PM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. Is it foolish to program with Win32 API ?
    By Kelvin in forum Windows Programming
    Replies: 2
    Last Post: 07-09-2002, 02:03 PM
  5. pthread api vs win32 thread api
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 11-20-2001, 08:55 AM

Tags for this Thread