--0F1p//8PRICkK4MW
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

I'm working on a use case where I need to spawn a thread that handles a
blocking piece of communication and have it call back into a lamba
function when the call completes. All without blocking the main
application thread.

The code I have is:

class Foo

  def sync(args =3D {}, &callback)
    block =3D args[:block] || false
    if block
      Thread.new do
        # passing in true says to block until the underlying code ends
        @underlying_impl.sync true
        callback.call
      end
    else
      @underlying_impl.sync false
      callback.call
    end
  end

end

What I _want_ to have happen is that, if I call this with :block =3D>
true, the blocking call would get spawned off on a separate thread and
run in parallel to the main thread. And, of course, if called with
:block =3D> false (or without :block at all) that the calling thread would
block until the sync exits.

However, what I'm seeing is that, even with :block =3D> true, the new
thread is created but the main thread is blocking. If I put a
Thread.pass call in just before calling @underlying_impl.sync then the
main thread updates but the other thread never executes.

Am I misunderstanding Threading in Ruby? In Java spawning a Thread has
that other thread run in parallel without interaction. Does Ruby not
work the same way?

--=20
Darryl L. Pierce <mcpierce / gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"

--0F1p//8PRICkK4MW
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAk4Imy8ACgkQtddqM0QFuLJOQACgzKo/utxayBcH8XPvhFctyGLl
rpsAoN+v8sL0lnmHJalOFryNRlFTVOcG
=LZah
-----END PGP SIGNATURE-----

--0F1p//8PRICkK4MW--

I'm working on a use case where I need to spawn a thread that handles a
blocking piece of communication and have it call back into a lamba
function when the call completes. All without blocking the main
application thread.

The code I have is:

class Foo

  def sync(args =3D {}, &callback)
    block =3D args[:block] || false
    if block
      Thread.new do
        # passing in true says to block until the underlying code ends
        @underlying_impl.sync true
        callback.call
      end
    else
      @underlying_impl.sync false
      callback.call
    end
  end

end

What I _want_ to have happen is that, if I call this with :block =3D>
true, the blocking call would get spawned off on a separate thread and
run in parallel to the main thread. And, of course, if called with
:block =3D> false (or without :block at all) that the calling thread would
block until the sync exits.

However, what I'm seeing is that, even with :block =3D> true, the new
thread is created but the main thread is blocking. If I put a
Thread.pass call in just before calling @underlying_impl.sync then the
main thread updates but the other thread never executes.

Am I misunderstanding Threading in Ruby? In Java spawning a Thread has
that other thread run in parallel without interaction. Does Ruby not
work the same way?

--=20
Darryl L. Pierce <mcpierce / gmail.com>
http://mcpierce.multiply.com/
"What do you care what people think, Mr. Feynman?"
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAk4Imy8ACgkQtddqM0QFuLJOQACgzKo/utxayBcH8XPvhFctyGLl
rpsAoN+v8sL0lnmHJalOFryNRlFTVOcG
=LZah
-----END PGP SIGNATURE-----