in reply to Event module and threads/fork segfault on Win32

Hi, your code segfaults on linux too. Your create_thread looks funky to me, (maybe because you create the thread and join it all in one sub ?)

Since Event is an event loop system, I looked at it like I would a Tk or Gtk2 threaded program. In Tk, the thread must be created before any event-loop code( i.e. Tk statements ) are made, so the following seems to work for me.

#!/usr/bin/perl use strict; use warnings; use threads; my $thr = async { for(1..10){ warn "in thread $_\n"; sleep 1; } }; sub create_thread { warn "in create_thread() before async()\n"; # my $thr = async { warn "in thread\n"; }; warn "in create_thread() before join()\n"; sleep 2; $thr->join; warn "in create_thread() before return\n"; return; } use Event; my $timer = Event->timer( cb => \&create_thread, after => 2); warn "starting event loop\n"; Event::loop;

Gtk2 has a thread-safety mechanism, that does allow you to create threads after event-loop code has been written. So you might want to try Gtk2 and it's GLib. However, even some Gtk2 experts say it is still better to create your threads first, before any Gtk2 code is written. See Roll your own Event-loop


I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: Event module and threads/fork segfault on Win32
by gurbo (Sexton) on Jun 05, 2007 at 14:20 UTC
    I create the thread and join it inside the same watcher just to make things simple. In real life, there is a watcher that starts them on demand and there is another that joins them to collect their exit statuses.
    That's why I cannot start the thread at the start of the script. They are created when a client makes a request.

    Thanks for the link to Glib, I'll give a try, but I cannot found Glib nor Gtk2 in ActiveState Perl package manager. Searching in Google I found gtk2-perl Win32 Binary Home. Is this the right place?

      I don't use Win32, but try Install GTK2 or gtk2-win32 support

      Glib is the base library for Gtk2. It does not need a gui and is developed to be thread-safe( as demonstrated by my previous example). I would be willing to bet that Event.pm is not thread-safe. The author talks alot about threads in the Event.pm perldoc, but never mentions the thread-safety of Event.pm

      So I'm willing to bet it acts like Tk, and you have to start the threads before any Tk code is called.... I generally use a "sleeping thread" where I have a thread code block controlled by a $go shared variable. That way you can use Event, by starting the thread before any Event code is written, then in your create_thread callback, just set $go to 1 to get the thread running. If you need help on how to do that, just use the SuperSearch for "Tk threads" and you will find many examples of a sleeping thread.

      This is from the Perl/Gtk2 maillist:

      From: Gabor Szabo <szabgab@gmail.com> For ActiveState users * I assume you already have ActiveState Perl installed on your system. If not, download and install ActiveState Perl from http://activestate.com/Products/ActivePerl/ * Next you need to install the GTK+ 2.x.x runtime environment. The latest version can be downloaded from http://gimp-win.sourceforge.net/stable.html * Ensure that the <INSTALL_PATH>\GTK\2.0\bin directory is in your PATH after this installation. * Next you need to install the Win32 binary packages for gtk2-perl. Download and install the latest version of Gtk2.ppd and Glib.ppd from http://gtk2-perl.sourceforge.net/win32/ppm/ (Another way to install these ppds is as follows C:\Temp> ppm install http://gtk2-perl.sourceforge.net/win32/ppm/Gtk2.p +pd C:\Temp> ppm install http://gtk2-perl.sourceforge.net/win32/ppm/Glib.p +pd ) Your system should be ready to roll !

      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        Thanks a lot! I'll give it a try.