#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Glib;
use Queue;

=head1 Signaling across threads to a Glib loop

Looks like the best way is to use a pipe

=cut

my $q=Glib::Thread::Queue->new();

threads->create(
    sub{
        print "sleeping\n";
        sleep 2;
        print "sending boo\n";
        $q->enqueue('boo');
        sleep 2;
        print "inserting bar & foo\n";
        $q->insert(0,'bar');
        $q->insert(1,'foo');
        print "signaling\n";
        $q->signal();
        print "signaling\n";
        $q->signal();
        sleep 2;
        print "sending quit";
        $q->enqueue('quit');
});

my $loop=Glib::MainLoop->new();
$q->add_watch(sub{
                  print "waking up\n";
                  my $foo=$q->dequeue();
                  print "got $foo\n";
                  if ($foo eq 'quit') {
                      print "quitting\n";
                      $loop->quit;
                  }
                  return Glib::SOURCE_CONTINUE;
              },
          );
$loop->run;
