[fpc-pascal] Why is cthreads unit not included by default

Graeme Geldenhuys graemeg at opensoft.homeip.net
Thu Aug 27 12:06:07 CEST 2009


I first asked this question in the Lazarus mailing list, but was told it
may be more appropriate here. When you create a new project in Lazarus,
the default uses clause in the *.lpr file looks as follows:

  {$IFDEF UNIX}{$IFDEF UseCThreads}

Question 1:
  Is there an alternative implementation of multi-threading support for
Unix-type systems -- other than the cthreads unit?

Question 2:
  If not, then why do we have the extra "IFDEF UseCThreads" define in
the uses clause?

Why can't we just have the following...? By default Windows programs and
OS/2 programmes have multi-threading support compiled in, why don't we
do the same for Unix-type systems? I often get the "program not compiled
with multi-threading support" error and have to constantly define the
UseCThreads option.


Mattias mentioned that when cthreads unit is included, all string access
needs critial sections which will slow down string access for any
unix-type OS.

Now because under Windows, threading support is on by default, does that
mean the same program will run faster under Linux than under Windows?
Why is threading enabled by default under Windows and not under other

I asked Mattias for a sample project that will show the slowdown, so I
can see by what factor it is slower. He supplied the following program.

program bench1;

{$mode objfpc}{$H+}

  classes, sysutils;

  s1: String;
  s2: String;
  i: Integer;
  s: String;
  t: TThread;
  for i:=1 to 500000000 do begin

Mattias test results are as follows:

Under his Linux machine the above program takes about 7.5 seconds.
Uncomment the cthreads unit: 7.5 seconds

Under OS X the above program takes on his mac book about 15 seconds.
Uncomment the cthreads unit: 21 seconds

On my Linux system I have the following results:
// without cthreads in uses clause
$ time ./bench1

real	0m9.971s
user	0m9.969s
sys	0m0.004s

// with cthreads in uses clause - but no TThread usage.
$ time ./bench1

real	0m9.973s
user	0m9.973s
sys	0m0.000s

So on my system having the cthreads unit included by default will make
no difference at all. This is the point I'm trying to make.

Can anybody explain why we have to explicitly enable threading support
under unix-type OSes, but comes standard with Windows?

  - Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal

More information about the fpc-pascal mailing list