unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Threads and lexical scope
@ 2014-01-14 13:52 Panicz Maciej Godek
  2014-01-14 14:01 ` Panicz Maciej Godek
  0 siblings, 1 reply; 4+ messages in thread
From: Panicz Maciej Godek @ 2014-01-14 13:52 UTC (permalink / raw)
  To: guile-user@gnu.org

[-- Attachment #1: Type: text/plain, Size: 558 bytes --]

Hi,
I've been investigating the behaviour of threads created in the same
lexical scope, and I have to admit that I'm a little puzzled.
The documentation states, that the "call-with-new-thread" procedure calls
its argument in a new thread and with a new dynamic state, the latter being
described in more detail with the introduction of the notion of fluids.

Nothing is being said regarding the behaviour of new thread with respect to
the current lexical scope.

The test program below shows, that each thread obtains its own copy of a
current lexical scope:

[-- Attachment #2: Type: text/html, Size: 670 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Threads and lexical scope
  2014-01-14 13:52 Threads and lexical scope Panicz Maciej Godek
@ 2014-01-14 14:01 ` Panicz Maciej Godek
  2014-01-14 14:10   ` Panicz Maciej Godek
  0 siblings, 1 reply; 4+ messages in thread
From: Panicz Maciej Godek @ 2014-01-14 14:01 UTC (permalink / raw)
  To: guile-user@gnu.org

[-- Attachment #1: Type: text/plain, Size: 1254 bytes --]

(sorry, I accidentally pressed ctrl+return, which caused my mail client to
send the message).
So here's the code:

(for-each
    (lambda(i)
      (let ((thread (call-with-new-thread
                     (lambda ()
                       (sleep (+ (random 10) 2))
                       (format #t "woke ~a: ~a\n" (current-thread) i)))))
        (format #t "created ~a: ~a\n" thread i)))
    (iota 5))

It displays something like this (with the "woke" messages being displayed
at some random instances of time, of course):

created #<thread 140077961983744 (d57000)>: 0
created #<thread 140077970376448 (e36400)>: 1
created #<thread 140077978769152 (e40c00)>: 2
created #<thread 140077995554560 (e40a00)>: 3
created #<thread 140077987161856 (e40800)>: 4
woke #<thread 140077970376448 (e36400)>: 1
woke #<thread 140077961983744 (d57000)>: 0
woke #<thread 140077978769152 (e40c00)>: 2
woke #<thread 140077995554560 (e40a00)>: 3
woke #<thread 140077987161856 (e40800)>: 4

So it seems that each thread keeps the value of the variable from the
moment of its creation. This is fine (and perhaps that's the most
reasonable behaviour), but I'm a little worried that I didn't manage to
find it in the documentation, so I'm just writing to receive confirmation ;]

[-- Attachment #2: Type: text/html, Size: 1705 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Threads and lexical scope
  2014-01-14 14:01 ` Panicz Maciej Godek
@ 2014-01-14 14:10   ` Panicz Maciej Godek
  2014-01-14 14:28     ` Panicz Maciej Godek
  0 siblings, 1 reply; 4+ messages in thread
From: Panicz Maciej Godek @ 2014-01-14 14:10 UTC (permalink / raw)
  To: guile-user@gnu.org

[-- Attachment #1: Type: text/plain, Size: 143 bytes --]

Oh, I just realized that each thread was created in a new closure anyway,
so they weren't able to share the variable. Sorry for the confusion.

[-- Attachment #2: Type: text/html, Size: 235 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Threads and lexical scope
  2014-01-14 14:10   ` Panicz Maciej Godek
@ 2014-01-14 14:28     ` Panicz Maciej Godek
  0 siblings, 0 replies; 4+ messages in thread
From: Panicz Maciej Godek @ 2014-01-14 14:28 UTC (permalink / raw)
  To: guile-user@gnu.org

[-- Attachment #1: Type: text/plain, Size: 286 bytes --]

2014/1/14 Panicz Maciej Godek <godek.maciek@gmail.com>

> Oh, I just realized that each thread was created in a new closure anyway,
> so they weren't able to share the variable. Sorry for the confusion.
>

It was a confusion, because the lexical scopes of threads are obviously
shared.

[-- Attachment #2: Type: text/html, Size: 669 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-01-14 14:28 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-01-14 13:52 Threads and lexical scope Panicz Maciej Godek
2014-01-14 14:01 ` Panicz Maciej Godek
2014-01-14 14:10   ` Panicz Maciej Godek
2014-01-14 14:28     ` Panicz Maciej Godek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).