https://github.com/potatomaster101/prethread
Simple pre-threading
https://github.com/potatomaster101/prethread
c99 mit-license threading threads
Last synced: 10 months ago
JSON representation
Simple pre-threading
- Host: GitHub
- URL: https://github.com/potatomaster101/prethread
- Owner: PotatoMaster101
- License: mit
- Created: 2019-09-01T02:09:22.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2021-11-26T12:47:03.000Z (over 4 years ago)
- Last Synced: 2025-02-21T09:48:48.065Z (over 1 year ago)
- Topics: c99, mit-license, threading, threads
- Language: C
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Pre-Thread
Simple POSIX threads pre-threading in C99. Originally used in uni assignments.
## Compile
Compile with `-lpthread` flag.
```
gcc *.c -lpthread -std=c99
```
## Example
```c
#include
#include
#include "prethd.h"
prethd_t *pool = NULL;
void *print(void *arg) {
prethd_lock(pool, 0); // locks first (and only) mutex
fputc('H', stdout);
fputc('e', stdout);
fputc('l', stdout);
fputc('l', stdout);
fputc('o', stdout);
fputc(' ', stdout);
fputc('W', stdout);
fputc('o', stdout);
fputc('r', stdout);
fputc('l', stdout);
fputc('d', stdout);
fputc('!', stdout);
fputc('\n', stdout);
prethd_unlock(pool, 0); // unlocks first (and only) mutex
sleep(5);
return NULL;
}
int main(void) {
const size_t NUM_TH = 50; // number of threads
const size_t NUM_MUT = 1; // number of mutexes
const size_t NUM_COND = 0; // number of conditional variables
pool = prethd_new(NUM_TH, NUM_MUT, NUM_COND);
prethd_all(pool, print, NULL);
prethd_join_free(pool);
printf("Done\n");
return 0;
}
```