add kotlin-playground styles to CONCURRENCY.md (#2071)
This commit is contained in:
committed by
Nikolay Igotti
parent
84bdf3e046
commit
4f750a26c5
+19
-1
@@ -23,6 +23,9 @@
|
||||
Once a worker is started with the `Worker.start` function call, it can be addressed with its own unique integer
|
||||
worker id. Other workers, or non-worker concurrency primitives, such as OS threads, can send a message
|
||||
to the worker with the `execute` call.
|
||||
|
||||
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||
|
||||
```kotlin
|
||||
val future = execute(TransferMode.SAFE, { SomeDataForWorker() }) {
|
||||
// data returned by the second function argument comes to the
|
||||
@@ -39,6 +42,9 @@
|
||||
result -> println("result is $result")
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
The call to `execute` uses a function passed as its second parameter to produce an object subgraph
|
||||
(i.e. set of mutually referring objects) which is then passed as a whole to that worker, it is then no longer
|
||||
available to the thread that initiated the request. This property is checked if the first parameter
|
||||
@@ -92,7 +98,10 @@
|
||||
mentioned above it is possible to build popular data structures, like concurrent hashmap or shared cache with
|
||||
Kotlin/Native. It is possible to rely upon shared C data, and store in it references to detached object subgraphs.
|
||||
Consider the following .def file:
|
||||
```
|
||||
|
||||
<div class="sample" markdown="1" theme="idea" mode="c">
|
||||
|
||||
```c
|
||||
package = global
|
||||
|
||||
---
|
||||
@@ -103,14 +112,23 @@ typedef struct {
|
||||
|
||||
SharedData sharedData;
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
After running the cinterop tool it can share Kotlin data in a versionized global structure,
|
||||
and interact with it from Kotlin transparently via autogenerated Kotlin like this:
|
||||
|
||||
<div class="sample" markdown="1" theme="idea" data-highlight-only>
|
||||
|
||||
```kotlin
|
||||
class SharedData(rawPtr: NativePtr) : CStructVar(rawPtr) {
|
||||
var version: Int
|
||||
var kotlinObject: COpaquePointer?
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
So in combination with the top level variable declared above, it can allow looking at the same memory from different
|
||||
threads and building traditional concurrent structures with platform-specific synchronization primitives.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user