mmmmh
This commit is contained in:
parent
82aee127b0
commit
a92b119549
99 changed files with 6922 additions and 5723 deletions
49
docs/cthreads.md
Normal file
49
docs/cthreads.md
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
---
|
||||
title = Threads
|
||||
---
|
||||
# Threads
|
||||
----------
|
||||
|
||||
Cross platform threads, mutexes and conditional variables.
|
||||
The api is very similar to pthreads, here is a small example program that uses threads and mutexes.
|
||||
|
||||
```c
|
||||
struct {
|
||||
bool exit;
|
||||
cmutex_t mtx;
|
||||
} state = {0};
|
||||
|
||||
int f1(void *) {
|
||||
mtxLock(state.mtx);
|
||||
state.exit = true;
|
||||
mtxUnlock(state.mtx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int f2(void *) {
|
||||
while (true) {
|
||||
bool exit = false;
|
||||
if (mtxTryLock(state.mtx)) {
|
||||
exit = state.exit;
|
||||
mtxUnlock(state.mtx);
|
||||
}
|
||||
|
||||
if (exit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
state.mtx = mtxInit();
|
||||
|
||||
cthread_t t1 = thrCreate(f1, NULL);
|
||||
thrDetach(t1);
|
||||
|
||||
cthread_t t2 = thrCreate(f2, NULL);
|
||||
thrJoin(t2, NULL);
|
||||
|
||||
mtxFree(state.mtx);
|
||||
}
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue