Consider the following code, used with Fibers 1.0.0 as packaged in Guix System:
(use-modules (fibers)
(fibers conditions))
(define some-fluid (make-thread-local-fluid 1234567))
(format #t "Main thread initial value: ~A~%" (fluid-ref some-fluid))
(fluid-set! some-fluid 1337)
(join-thread
(call-with-new-thread
(lambda ()
(format #t "Guile thread value: ~A~%" (fluid-ref some-fluid)))))
(run-fibers
(lambda ()
(let ((condition (make-condition)))
(spawn-fiber (lambda ()
(format #t "Fiber 1 value: ~A~%" (fluid-ref some-fluid))
(fluid-set! some-fluid 42)
(signal-condition! condition)))
(spawn-fiber (lambda ()
(wait condition)
(format #t "Fiber 2 value: ~A~%" (fluid-ref some-fluid))))))
#:drain? #t)
(format #t "Main thread final value: ~A~%" (fluid-ref some-fluid))
It produces this output:
Main thread initial value: #f
Guile thread value: #f
Fiber 1 value: 1337
Fiber 2 value: 42
Main thread final value: 42
There are a number of things going wrong here.
- The default value of some-fluid, set when it is created, seems to be ignored in favor of
#f. This is a bug in guile I've reported as bug 36915.
- In the fiber, the value of
some-fluid is inherited. This is the opposite of what might be expected to happen based on section 6.22.2 of the Guile manual, but is in line with section 2.1 of the Fibers manual, where it says "The fiber will inherit the fluid-value associations (the dynamic state) in place when 'spawn-fiber' is called". It would be nice to have the ability to use thread-local variables in fibers, though.
- When the value of
some-fluid is set from within the fiber, the change takes effect outside of that fiber, which is the opposite of what might be expected to happen based on section 2.1 of the Fibers manual. There it says: "Any 'fluid-set!' or parameter set within the fiber will not affect fluid or parameter bindings outside the fiber".
Consider the following code, used with Fibers 1.0.0 as packaged in Guix System:
```
(use-modules (fibers)
(fibers conditions))
(define some-fluid (make-thread-local-fluid 1234567))
(format #t "Main thread initial value: ~A~%" (fluid-ref some-fluid))
(fluid-set! some-fluid 1337)
(join-thread
(call-with-new-thread
(lambda ()
(format #t "Guile thread value: ~A~%" (fluid-ref some-fluid)))))
(run-fibers
(lambda ()
(let ((condition (make-condition)))
(spawn-fiber (lambda ()
(format #t "Fiber 1 value: ~A~%" (fluid-ref some-fluid))
(fluid-set! some-fluid 42)
(signal-condition! condition)))
(spawn-fiber (lambda ()
(wait condition)
(format #t "Fiber 2 value: ~A~%" (fluid-ref some-fluid))))))
#:drain? #t)
(format #t "Main thread final value: ~A~%" (fluid-ref some-fluid))
```
It produces this output:
```
Main thread initial value: #f
Guile thread value: #f
Fiber 1 value: 1337
Fiber 2 value: 42
Main thread final value: 42
```
There are a number of things going wrong here.
1. The default value of some-fluid, set when it is created, seems to be ignored in favor of `#f`. This is a bug in guile I've reported as [bug 36915](https://debbugs.gnu.org/cgi/bugreport.cgi?bug=36915).
2. In the fiber, the value of `some-fluid` is inherited. This is the opposite of what might be expected to happen based on section 6.22.2 of the Guile manual, but is in line with section 2.1 of the Fibers manual, where it says "The fiber will inherit the fluid-value associations (the dynamic state) in place when 'spawn-fiber' is called". It would be nice to have the ability to use thread-local variables in fibers, though.
3. When the value of `some-fluid` is set from within the fiber, the change takes effect outside of that fiber, which is the opposite of what might be expected to happen based on section 2.1 of the Fibers manual. There it says: "Any 'fluid-set!' or parameter set within the fiber will not affect fluid or parameter bindings outside the fiber".