#634 portable Tcl_CreateThread inherit of fp settings

Don Porter

The unix implementation of Tcl threading support
is built on the pthread library, and the routine
pthread_create() is at the core of all Tcl thread
creation on unix.

pthread_create() documents how it manages the
initialization of the floating point environment in
the created thread:

"The floating-point environment shall be inherited from the creating

This is a very useful default behavior for programs
performing multi-threaded calculations, keeping all
threads computing in a consistent way.

Empirically, we do not see this desirable behavior
from Tcl_CreateThread() on Windows. In particular,
when the master thread is computing using
80-bit extended precision computations, all
threads created by T_CT() do their computations
using only the default 64-bit precision operations.
Even code explicitly written using C "long double"
variables gets only 64-bit precision function when
run in a Tcl non-master thread. The consequence
is that computed results are not portable.

Tcl_CreateThread() takes an argument for a thread
initialization procedure, so those programs that care
about this detail have the ability to use that routine
to workaround this problem. The disadvantage is that
the code to query and set the floating point environment
is a nonportable mess, depending on hardware and
OS and compiler and other details. Coding a portable
solution to forcing fp environment inheritence is a big,
thorny problem. It's exactly the kind of portability
chore that programs rely on the Tcl library to solve
in other scenarios.

It's a tall order, but I'd like it if Tcl_CreateThread() aimed
at making the fp environment inheritance of pthread_create()
a cross-platform feature as much as possible.


  • Joe Mistachkin

    Joe Mistachkin - 2012-09-12

    I'll investigate doing this for MinGW and MSVC. Do we need to support other compilers on Windows for this feature?

  • Joe Mistachkin

    Joe Mistachkin - 2012-09-13

    I assume that these changes are targeted towards Tcl 8.4 and higher? Also, do you have any tests in the suite that demonstrate the floating point problem(s) with created threads?

  • Don Porter

    Don Porter - 2012-09-13

    I'll leave the extent of backporting to be determined later.
    Just getting something working on more platforms than
    pthreads is an improvement. Every additional port helps.
    MinGW + MSVC appears to take care of the vast majority
    of the issue.

    I'll see if I can collect something to demonstrate the issue
    from the original reporter. The problem happens on windows
    with programs built with gcc, then linked against Active Tcl
    libraries (built with MSVC). Since I don't work on windows
    myself, I don't have the demo programs at my fingertips.

  • Joe Mistachkin

    Joe Mistachkin - 2012-09-13

    I think the code on the new Fossil branch "bug-3567063" will do what you need. Is there some way we can work on validating this?

  • Don Porter

    Don Porter - 2012-11-13

    merged to dev branches