https://github.com/mayank-02/multithreading-library
A lightweight C library based on one-one and many-one model for threading.
https://github.com/mayank-02/multithreading-library
c conditional-variables many-to-one multithreading mutex one-to-one semaphore spinlock synchronization
Last synced: 5 months ago
JSON representation
A lightweight C library based on one-one and many-one model for threading.
- Host: GitHub
- URL: https://github.com/mayank-02/multithreading-library
- Owner: mayank-02
- License: gpl-3.0
- Created: 2020-03-10T07:04:22.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-02T16:46:49.000Z (over 1 year ago)
- Last Synced: 2023-10-02T22:40:12.195Z (over 1 year ago)
- Topics: c, conditional-variables, many-to-one, multithreading, mutex, one-to-one, semaphore, spinlock, synchronization
- Language: C
- Homepage:
- Size: 664 KB
- Stars: 18
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/mayank-02/multithreading-library)

[](https://opensource.org/licenses/GPL-3.0)A lightweight C library based on one-one and many-one model for threading.
## Contents
+ [Building](#building)
+ [Using mthread in your project](#using-mthread-in-your-project)
+ [Usage and examples](#usage-and-examples)
+ [API documentation](#api-documentation)
+ [Running tests](#running-tests)
+ [Time and space complexity](#time-and-space-complexity)
+ [Development and contributing](#development-and-contributing)
+ [Acknowledgements](#acknowledgements)## Building
mthread uses make to build libraries (static and shared) and binaries (for tests).
Execute following commands to build mthread using make:``` bash
make
```This will create binaries in `bin/` directory and libraries (static and shared) in the current directory.
Optionally, you can run `sudo make install` to install mthread library on your machine (on Linux, this will usually install it to `usr/local/lib` and `usr/local/include`).
## Using mthread in your project
You can use mthread in you project by either directly copying header and source files from [one-one/](one-one/), or by linking mthread library (see [Building](#building) for instructions how to build mthread libraries).
In any case, only thing that you have to do in your source files is to include `mthread.h`.To get you started quickly, let's take a look at a few ways to get a simple Hello World project working.
Our Hello World project has just one source file, `example.c` file, and it looks like this:
```c
#include
#include "mthread.h"void worker(void) {
printf("Hello World!\n");
mthread_exit(NULL);
}int main() {
mthread_t tid;
mthread_init();
mthread_create(&tid, NULL, worker, NULL);
mthread_join(tid, NULL);
return 0;
}
```### Approach #1: Copying mthread header file and static library
Instead of copying mthread source files, you could copy the static library or a shared object (check [Building](#building) on how to create static library / shared object). We also need to copy mthread header files. We get following project structure:
In case of a static library:```
example.c -> your program
mthread.h -> copied from mthread
libmthread.a -> copied from mthread
```In case of shared object:
```
example.c -> your program
mthread.h -> copied from mthread
libmthread.so -> copied from mthread
```Now you can compile it with
```bash
gcc example.c -o example -L. -llibmthread.
```### Approach #2: Install mthread library on machine (TODO)
Alternatively, you could avoid copying any mthread files and instead install libraries by running `sudo make install` (check [Building](#building)). Now, all you have to do to compile your project is `gcc example.c -o example -llibmthread`.
If you get error message like `cannot open shared object file: No such file or directory`, make sure that your linker includes path where mthread was installed.## Usage and examples
To know more about how to use mthread library, check out the various tests written in the `test` directory of each model.
## API documentation
+ Types are named **mthread\_[type]\_t** (examples: mthread_t, mthread_cond_t, etc.)
+ Functions are called **mthread\_[type]\_[action]** with a few exceptions that are mthread_[action] and pertain to the API in whole and not a specific type.
+ Constants are named **MTHREAD\_[NAME]**The mthreads API is inherently simple. Not in the sense that it makes multi-threaded (MT) programming a breeze (I doubt this is possible), but in the sense that it provides everything that's needed to write MT programs, and only that.
To generate the latest API documentation yourself from the source, you need to have [doxygen](www.doxygen.org) installed.
Position yourself in the root directory of the model you are interested in.
Then run `make docs`. This will output a `/docs/html` and `/docs/latex` folder.
Then open `docs/html/index.html` file with your favorite browser.Alternatively, you can directly check [mthread.h](one-one/include/mthread.h) for one-one and [mthread.h](many-one/include/mthread.h) for many-one
## Running tests
Check [Building](#building) to see how to build binaries.
To run each test, just run `./run_tests` from the root directory of both models.
Default values are used for tests requiring command line arguments.## Time and space complexity
The library maintains a queue for internal book keeping. Thus, the functions have different time complexities. They have a best case time complexity of `O(1)` and worst case time complexity of `O(N)`, N being the number of threads spawned. Space complexity is `O(N)`.
## Implementation Details
+ To know the implementation details of one-one threading model, please check out it's [README](one-one/README.md)
+ To know the implementation details of many-one threading model, please check out it's [README](many-one/README.md)
## Development and contributing
Feel free to send pull requests and raise issues.