Guidelines on callbacks: sync vs async

554 views
Skip to first unread message

Odev

unread,
May 16, 2020, 2:53:10 AM5/16/20
to Dart FFI
Hi,

I'm about to start trying to interface with a C++ library for use in Flutter, and I'll need to use callbacks from C to functions in Flutter. As I understand it, these callbacks can happen synchronously or asynchronously, with examples in sample_ffi_functions_callbacks.dart and async, respectively. Are there any guidelines for which mechanism to adopt? For example, if you expect the library to return in at most 10 ms, then go for sync, otherwise async? Other considerations?

Thanks!
Odev

Daco Harkes 🔵

unread,
May 18, 2020, 5:03:46 AM5/18/20
to Odev, Dart FFI
Hi Odev,

The synchronous callbacks can only be done on the main Dart thread of an isolate. So when you call into C from Dart, and from that thread you do a callback into Dart.

If you use a threadpool in C, you have to use asynchronous callbacks.

Flutter draws the UI in the main Dart thread of the main isolate. So, if you use the ffi in the main isolate (if you don't start extra isolates and do ffi on those), you will get frameloss if you spend too much time. I'm not sure where the sweetspot is, but 10ms per frame is a good guess.

Kind regards,

--
You received this message because you are subscribed to the Google Groups "Dart FFI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dart-ffi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dart-ffi/b3cab099-2609-448d-84c8-2231c2bd69ae%40googlegroups.com.


--
Daco Harkes
Reply all
Reply to author
Forward
0 new messages