Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2014-08

Threads in Android

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sergio Flores

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

I tried two approachs, using FPC TThread and using pthreads, none would work.

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.

Not including cthreads makes the app thrown an exception when creating a new thread, as expected. ("cannot create semaphore" or something like that).

I tried to use pthreads directly instead. However allocating any memory inside the new thread crashes the app imediately (The stack shows that the crash was in the cmem unit).
I guess some setup code has to called at the thread startup to allow the mem manager to work correctly?

I'm using FPC 2.6.1 for Android compilation, maybe this was fixed in later versions?

Reply by email

Jeppe Græsdal Johansen
to Sergio Flores
Sergio Flores wrote 20 lines
I'm trying to use threads in Android, but it seems they are not working yet in FPC? I tried two approachs, using FPC TThread and using pthreads, none would work. 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. Not including cthreads makes the app thrown an exception when creating a new thread, as expected. ("cannot create semaphore" or something like that). I tried to use pthreads directly instead. However allocating any memory inside the new thread crashes the app imediately (The stack shows that the crash was in the cmem unit). I guess some setup code has to called at the thread startup to allow the mem manager to work correctly?

That is correct.

I'm using FPC 2.6.1 for Android compilation, maybe this was fixed in later versions?

I'm not sure but you should probably try a newer version, maybe build from trunk. 2.6.1 is old as far as I remember.

Reply by email

Michalis Kamburelis
to Sergio Flores: I'm trying to use threads in Android, but it seems they are not working yet in FPC? I…
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
http://wiki.freepascal.org/Multithreaded_Application_Tutorial#External_threads . 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
https://sourceforge.net/p/castle-engine/wiki/Android%20development/#fpc-for-android .

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,
Michalis

Reply by email