https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

damian

08/20/2019, 3:44 PM
Untitled.kt
m

Marko Mitic

08/20/2019, 4:00 PM
I guess some lock would work but it might be more appropriate for component itself to suspend until ready for more work, at least in given example
d

damian

08/20/2019, 4:03 PM
The other component is a C++ dynamic lib via JNI interface
m

Marko Mitic

08/20/2019, 4:05 PM
do you get a callback when it's ready for more data or do you have to check a flag?
d

damian

08/20/2019, 4:05 PM
I get a callback
m

Marko Mitic

08/20/2019, 4:06 PM
You could use a Mutex or Semaphore
d

damian

08/20/2019, 4:14 PM
Thanks! I will look into that
m

Marko Mitic

08/20/2019, 4:19 PM
Copy code
channel.consumeEach {
        //bit strange but avoids potential deadlock if callback happens on this thread (as Mutex is not reentrant lock)
        mutex.lock()
		mutex.unlock()

        processResults(it)
    }
	
onNotReady() {
	mutex.lock()
}

onReady() {
	mutex.unlock()
}
2 Views