wanted: DEFINE-THREAD-LOCAL-VARIABLE

Bug #310204 reported by Nikodemus Siivola
8
Affects Status Importance Assigned to Milestone
SBCL
Incomplete
Wishlist
Gábor Melis

Bug Description

A way to initialize thread local variables.

Discussed a number of times on sbcl-devel.

Collect links to those discussions /here/.

This thread on sbcl-devel starts with a proposed patch and ends with another:

http://thread.gmane.org/gmane.lisp.steel-bank.devel/7351/focus=8794

Dealing with already running threads is not implemented.

Changed in sbcl:
status: New → Incomplete
importance: Undecided → Wishlist
Revision history for this message
Tobias C. Rittweiler (tcr) wrote :

Another way would be to introduce a special form THREAD-LOCAL-VALUE that is
inspired by LOAD-TIME-VALUE.

    (THREAD-LOCAL-VALUE expr) reserves space in thread-local memory, initialized
                  by the result of evaluating EXPR at load-time (or at execution time, but
                  guaranteed to be evaluated only once.)

This way DEFINE-THREAD-LOCAL-VARIABLE could be implemented as

  (DEFGLOBAL FOO (THREAD-LOCAL-VALUE expr))

or similiarly. The advantage is that this would also allow C-style static variables:

  (defun quux (x)
     (let ((count (thread-local-value 0)))
       (incf count)
       (list :x :count count)))

Revision history for this message
Paul Khuong (pvk) wrote :

I already have code hanging around to set the value of a special binding, adding a thread-local one if necessary. THREAD-LOCAL-VALUE could easily be implemented on top of that. See <http://paste.lisp.org/display/63257>.

Gábor Melis (melisgl)
description: updated
Gábor Melis (melisgl)
Changed in sbcl:
assignee: nobody → melisgl
Revision history for this message
Tobias C. Rittweiler (tcr) wrote :

I was about to say that the THREAD-LOCAL-VALUE special form may be
a succinct way to express what currently *zap-array-data-temp* and
*merge-sort-temp-vector* are for. However, there also come interrupts
into play -- and THREAD-LOCAL-VALUE would enable useres to write
non-reentrant functions very easily, wouldn't it?

I was thinking of THREAD-LOCAL-VALUE as "static variables but done
right" -- I guess that belief was mistakenly naive?

Revision history for this message
Gábor Melis (melisgl) wrote :

I thought of what might be a better way to deal with the thread local problem. Implementation is here:
https://github.com/melisgl/sbcl/commits/thread-local

There is no way to inherit from the parent thread, but it handles existing threads gracefully. See the documentation diff for a tutorialish treatment.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.