1

I have a app running 2 isolates - the main and a processing isolate (lets' call it decoder). The processing isolates receives messages from the main threads and also send data for some processing on main thread. Of course these communications are async (as mandated). The decoder isolate loads the native library and make appropriate FFI calls.

The problem While investigating an issue, I found that native code is being run in multiple threads which creates havoc in the environment in which it runs.

I have analyzed that the isolate is a single thread (using dart:io pid getter). For the native code I have std::this_thread::get_pid() to extract thread identifier. They are then being time sliced across invocations, and there is a sort of round-robin call on each native thread.

Question What could be triggering creation of new threads in native? I use a mix of Isolate & Compute in the program.


UPDATE

I have now added a Singleton class in native code whose constructor sets a epoch time. This is now being added in native logs being printed along with the PID.

I/flutter ( 5064): │  [5064] *NativeLog*: 14:34:48: TID: 488469650624 - 1674119086666 - readQRCode: barcode/qrcode read xxx

I/flutter ( 5064): │  [5064] *NativeLog*: 14:34:48: TID: 488470723776 - 1674119086666 - readQRCode: barcode/qrcode read xxx

I/flutter ( 5064): │  [5064] *NativeLog*: 14:34:48: TID: 488469650624 - 1674119086666 - readQRCode: barcode/qrcode read xxx

5064 - Isolate Thread Id (flutter)

TID: 488xxx - Native Thread Id

1674xxxx6666 - Singleton indicating epochtime (this is created the first time the DynamicLibrary is opened on flutter side.

Observations:

The epoch time (using Singleton) and Isolate Thread Id (as expected) is same across different TID.

  • Am I looking at wrong value of std::this_thread::get_pid() (I understand this is implementation dependent)?
  • What is the correct approach to ascertain that a single thread is running in native code?
banerjk
  • 335
  • 2
  • 15

0 Answers0