in reply to Threads and Package differences between Linux and Windows

I don't have a perfect solution, nor even a good answer for you, but I do have some observations & speculations.

The freeze occurs when attempting to create the second thread.

If you comment out the recv call in the first thread, the freeze does not occur.

What the subroutine being started in that second thread, is or does, is irrelevant.

It never reaches the point of being called, nor even the (C) thread being created.

This I believe can only mean that the OS is deferring the creation of the new thread until the recv completes.

This does not happen with normal sockets created by IO::Socket.

As the socket created by IO::Socket::Multicast *is* an IO::Socket instance, the only real difference is that the it has had

setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(void*) &mreq,sizeof(mreq)) + < 0)

called upon it.

I suspect that the difference 'tween *nix & Windows lies in the implementation of that socket option. I also suspect that using recv on a blocking socket may be an underlying problem

This latter suspicion is "confirmed" by the following version of your code that sets the socket non-blocking which allows it to run to completion:

package T; use strict; use threads; use IO::Socket::Multicast; our $sock = undef; sub createsock { my $create = shift @_; my $noBlock == 1; if ($create) { $sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>220 +0); $sock->mcast_add('226.1.1.2') || die "Couldn't set group: $!\n +"; ioctl( $sock, 0x8004667e, \$noBlock ); } } sub mysub1 { my ($sub, $msg) = shift @_; my $noBlock == 1; print "start sub $sub\n"; #my $key = getc(STDIN); my $data; if ($sock == undef) { $sock = IO::Socket::Multicast->new(Proto=>'udp',LocalPort=>220 +0); $sock->mcast_add('226.1.1.2') || die "Couldn't set group: $!\n +"; ioctl( $sock, 0x8004667e, \$noBlock ); } $sock->recv($data,1024); print "end sub $sub\n"; } sub mysub2 { my ($sub, $msg) = shift @_; print "start sub $sub\n"; print "msg from sub $sub: $msg\n"; print "end sub $sub\n"; } package main; T::createsock(1); my $t1 = threads->create( \&T::mysub1, 1, "test 1"); select(undef, undef, undef, .25); print "above mysub2\n"; my $t2 = threads->create( sub{ warn }, 2, "test 2"); print "below mysub2\n"; $t2->join();

Whether this can work with your application ...


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Threads and Package differences between Linux and Windows
by digitalhack (Novice) on Oct 14, 2011 at 12:56 UTC
    Thank you for sharing these observations.