Recent

Author Topic: The creation of a Thread immediately kills the application in Android  (Read 10837 times)

Kostas

  • New Member
  • *
  • Posts: 30
I tried to create a thread and the application crashes immediately in Android.
In Win32 everything runs right.

Is this a known bug or should i take care of something else when i need threads in my application?

Thanks in advance
Kostas

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
No, it is not known. What code are you running in the thread? Try an empty execution code first to see if the problem is really that.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11351
  • FPC developer.
Unit cthreads?

Kostas

  • New Member
  • *
  • Posts: 30
I added the unit Classes and wrote a small descendant of a TThread first for test purposes.

The program crashes immediately when i call:
aThread := TMyThread.Create(true);

Nothing runs in the thread. It is at the creation.

The class TMyThread has nothing special and everything runs right in Win32/Win64.

Should i add cthreads.pas in the units??

Kostas

« Last Edit: July 02, 2012, 10:38:51 am by Kostas »

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
inside you project .lpr file you have this:

Code: [Select]
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF} 

So you need to define the UseCThreads somewhere, maybe in project option -> compiler opt. -> other -> custom opt. -> -dUseCThreads  ;)

or you can comment the {$IFDEF UseCThreads} and the second {$ENDIF} :-X

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Should i add cthreads.pas in the units??

Yes, add it as the first unit in the uses clause of your main library source code file.

Andru

  • Full Member
  • ***
  • Posts: 112
Yes, this bug is known. And cthreads won't help if you use precompiled FreePascal 2.5.1(or with some magic - original FreePascal 2.6.x without patches), because all pthread_* functions loading from libpthread.so instead of libc.so(and this is not the only reason). libpthread just doesn't exist on Android. So you need a patched version of FreePascal.

Bugs in bugtracker:
http://bugs.freepascal.org/view.php?id=21525
http://bugs.freepascal.org/view.php?id=21853
« Last Edit: July 02, 2012, 05:07:50 pm by Andru »

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Android is right, and I think that I haven't merged threading fixes to my 2.5.1 branch:

http://wiki.lazarus.freepascal.org/Custom%20Drawn%20Interface/Android#FPC4Android_2.5.1

I don't use thread support in my Android apps, so am not 100% up to date about it...

Kostas

  • New Member
  • *
  • Posts: 30
I don't use thread support in my Android apps, so am not 100% up to date about it...

My problem is that i have a backgammon engine (like a chess engine) that runs and can take some time depending on the searching depth. The algorithm runs full recursive and it is not possible to break it in smaller pieces that run over a timer.

I need at least the following:
1. The GUI must react, so that some search statistics can be shown.
2. The user should have the possibility to stop the engine using a button while the engine is running...

The only easy way that i thought was to run the engine in a separate thread. This is easy and works fine in Win32.

But how to implement this without threads?
Any idea?

Yes, this bug is known. And cthreads won't help if you use precompiled FreePascal 2.5.1(or with some magic - original FreePascal 2.6.x without patches), because all pthread_* functions loading from libpthread.so instead of libc.so(and this is not the only reason). libpthread just doesn't exist on Android. So you need a patched version of FreePascal.
Is there such a patched FreePascal already done?

Regards
Kosats
« Last Edit: July 02, 2012, 06:51:13 pm by Kostas »

Andru

  • Full Member
  • ***
  • Posts: 112
Quote
Is there such a patched FreePascal already done?
You can try to use my patch from one of the links which I mentioned and compile your own FPC 2.5.1, but I can't say if it will work, because I use FPC 2.6.x and patch may not apply correctly to older version of FPC.
« Last Edit: July 02, 2012, 08:25:57 pm by Andru »

Kostas

  • New Member
  • *
  • Posts: 30
Re: The creation of a Thread immediately kills the application in Android
« Reply #10 on: July 22, 2012, 08:27:17 am »
I have applied the andrus patches to 2.5.1 android branch.
I put cthreads as the first unit in the project file.

Now i can create a thread (a TThread-Object) but when i call resume or start nothing happens.
The execute method of the thread is never called.

Any idea what i could do?

Maybe the TThread object still has some issues.
How can i use the beginthread etc. functions in order to start a thread?

Kostas
« Last Edit: July 22, 2012, 02:48:19 pm by Kostas »

Kostas

  • New Member
  • *
  • Posts: 30
Re: The creation of a Thread immediately kills the application in Android
« Reply #11 on: July 22, 2012, 10:37:23 am »
Using the BeginThread function everything works right...

So the problem it should be somewhere in the TThread Object...

tommy_rusev

  • Newbie
  • Posts: 5
Re: The creation of a Thread immediately kills the application in Android
« Reply #12 on: January 23, 2018, 08:39:18 pm »
Can give an example of how to implement the Synchronize method if the thread is created with the BeginThread function ?

 

TinyPortal © 2005-2018