[fpc-devel] Threads in Android

Michalis Kamburelis michalis.kambi at gmail.com
Sun Aug 17 20:31:58 CEST 2014

Sergio Flores wrote:
> I'm trying to use threads in Android, but it seems they are not working
> yet in FPC?

I'm using threads on Android successfully with FPC 2.7.1 (various SVN
revisions, since a couple of months) in Castle Game Engine
http://castle-engine.sourceforge.net/ (see unit
castle_game_engine/src/base/android/castleandroidnativeappglue.pas in
sources, heavily modified based on LCL customdrawn-android code).

An important note is that if you want to use threads, make sure that FPC
knows about the fact that your program is threaded, otherwise weird
things (including random crashes) can happen. See
. The cleanest solution is to create all threads using pure Pascal code,
like TThread class, and *not* directly use pthreads. And to use TThread
class, you have to include CThreads unit, and it should be the 1st unit
on uses clause.

> Using TThread requires including cthreads unit right?
> But including this unit makes the app imediately close at startup, silently, with nothing in the log except a message saying the process died.

Yes, using TThread + CThreads should be Ok, and works Ok for me. At
least with FPC 2.7.1. I was not able to get anywhere with FPC 2.6.x on
Android, and finally gave up since everything seemed smooth on FPC
2.7.1, see

So I would try, in this order:

1. See does the problem still occur with FPC 2.7.1?

ZenGL wiki has some patches related to threads with FPC 2.6.1, see
http://zengl.org/wiki/doku.php?id=compilation:android .

2. If the app still fails, maybe the thread creation worked but
something else failed? You probably already use  __android_log_write, so
use it more :) --- maybe it failed in some unit's initialization clause?
For example, I had an issue with some Android models crashing when using
dlopen too early (before activity started).

Hope this helps,

More information about the fpc-devel mailing list