What's the status of RTL and IsMultiThreaded := True?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

What's the status of RTL and IsMultiThreaded := True?

Anthony Walter-3
I did some Googling earlier tonight regarding using the RTL and threads without creating a TThread class. I found some older results from this mailing list such as...

Basically, what I want to know is "HOW DO I..." ensure the RTL memory management is safe if multiple threads are running, but if a TThread class is never instantiated. I am doing some work test/writing pascal demos for SDL 2.0 and it creates threads for you, sometimes automatically, which can call back user defined functions.

I've written a few example programs using just a program unit containing "uses SDL2;" (SDL2.pas only imports functions from a SDL 2.0 dynamic library) which play sounds, use force feedback controllers, accept touch input ect, and should work natively on many devices (Windows, Linux, Android, OSX, iOS, Raspberry Pi) with very little to no need for code modification. But, I want to be proper and ensure things something like the pascal memory manager works with GetMem and FreeMem (or for that matter all other rtl routines) when the SDL sound mixer calls back some of my user defined pascal function from a new thread SDL is managing.

My examples currently weigh in at about 25KB compiled and are quite concise/understandable. I'd like to avoid the baggage of bringing in the SysUtils and Classes units, creating a Classes.TThread, then destroying the thread ... all to ensure the memory manager and the rest of the System unit is aware multiple threads might be executing.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: What's the status of RTL and IsMultiThreaded := True?

Jonas Maebe-2

On 13 Apr 2013, at 11:12, Anthony Walter wrote:

Basically, what I want to know is "HOW DO I..." ensure the RTL memory
management is safe if multiple threads are running, but if a TThread class
is never instantiated. 

You have to start at least one thread before the multithreading functionality is initialised, there is no way around that. You can however also use system.beginthread instead: http://www.freepascal.org/docs-html/rtl/system/beginthread.html


Jonas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: What's the status of RTL and IsMultiThreaded := True?

Anthony Walter-3
Thanks for the prompt reply Jonas.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: What's the status of RTL and IsMultiThreaded := True?

Anthony Walter-3
In reply to this post by Jonas Maebe-2
Oh, I just thought of a followup question. If I have to call BeginThread to make the memory manager/rtl "thread aware", do I then have to also have to check the OS type (and maybe the CPU architecture) and possibly add more units? Is it safe to call BeginThread on all platforms/architectures using only the System unit? 

{ifdef $unix}
{ Do I still need this the line below? 
  Do I need to be concerned with different OSes or CPU architectures in the line below?
  Android ARM, Raspberry Pi ARM, OSX, iOS with whatever CPU ... ? }
uses cthreads; 
{$endif}

function ThreadFunc(Parameter: Pointer): PtrInt;
begin
  EndThread(0);
end;

procedure InitThreadAware;
begin
  BeginThread(@ThreadFunc);
end;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: What's the status of RTL and IsMultiThreaded := True?

Anthony Walter-3
Oh and yet another question ...

What is the minimum that needs to be done on all platforms/cpus to enabled thread local storage (aka threadvar)?

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: What's the status of RTL and IsMultiThreaded := True?

Sven Barth-2
In reply to this post by Anthony Walter-3
On 13.04.2013 16:06, Anthony Walter wrote:
> Oh, I just thought of a followup question. If I have to call BeginThread
> to make the memory manager/rtl "thread aware", do I then have to also
> have to check the OS type (and maybe the CPU architecture)

No, you need to do this for every system. BeginThread is supported
everywhere where threading is supported (and it's used internally by
TThread).

> and possibly
> add more units? Is it safe to call BeginThread on all
> platforms/architectures using only the System unit?
>

On certain platforms (e.g. Unix ones) you need to add the cthreads unit
(or whereever the thread manager is implemented).

It's not depending on the platform (though I wouldn't bet on m68k
supporting threads correctly currently ^^)

> {ifdef $unix}
> { Do I still need this the line below?
>    Do I need to be concerned with different OSes or CPU architectures in
> the line below?
>    Android ARM, Raspberry Pi ARM, OSX, iOS with whatever CPU ... ? }
> uses cthreads;

This line (or a similar one) is needed only on systems where the thread
manager is not included in the system unit (e.g. not on Windows systems)

> {$endif}
>
> function ThreadFunc(Parameter: Pointer): PtrInt;
> begin
> EndThread(0);
> end;
>
> procedure InitThreadAware;
> begin
>    BeginThread(@ThreadFunc);
> end;

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: What's the status of RTL and IsMultiThreaded := True?

Tomas Hajny-2
On 13 Apr 13, at 16:36, Sven Barth wrote:
 .
 .
> This line (or a similar one) is needed only on systems where the thread
> manager is not included in the system unit (e.g. not on Windows systems)

...or OS/2... ;-)

Tomas

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal