File: threads.md

package info (click to toggle)
libdex 1.1~alpha-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: ansic: 15,259; python: 550; asm: 226; xml: 39; makefile: 15; sh: 6; javascript: 6
file content (24 lines) | stat: -rw-r--r-- 1,164 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Title: Threads for Blocking APIs

There are often needs to integrate with blocking APIs that do not fit well into the async or future-based models of the GNOME ecosystem.
In those cases, you may want to use a dedicated thread for blocking calls so that you do not disrupt main loops, timeouts, or fiber scheduling.

# Creating a Dedicated Thread

Use the [func@Dex.thread_spawn] function to spawn a new thread.
When the thread completes the resulting future will either resolve or reject.

```c
typedef DexFuture *(*DexThreadFunc) (gpointer user_data);

DexFuture *future = dex_thread_spawn ("[my-thread]", thread_func, thread_data,
                                      (GDestroyNotify)thread_data_free);
```

# Waiting for Future Completion

Since dedicated threads do not have a [class@Dex.Scheduler] on them and are not a fiber, you may not await futures.
Awaiting would suspend a fiber stack but there is no such fiber to suspend.

To make integration easier, you may use [func@Dex.thread_wait_for] to wait for a future to complete.
The mechanism used in this case is a mutex and condition variable which will be signaled when the dependent future completes.