Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNosiriN committed Aug 16, 2021
1 parent 92f53a2 commit 63b795b
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 125 deletions.
1 change: 1 addition & 0 deletions CMakeFiles/Progress/1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
empty
1 change: 1 addition & 0 deletions CMakeFiles/Progress/count.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2
Binary file modified CMakeFiles/app.dir/main.cpp.obj
Binary file not shown.
122 changes: 0 additions & 122 deletions CMakeFiles/app.dir/main.cpp.obj.d

This file was deleted.

Binary file modified CMakeFiles/app.dir/vc140.pdb
Binary file not shown.
165 changes: 163 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,163 @@
# HexoThreading
Tiny Header-only Threading Library. Made in C++14
# Hexo Threading
Tiny Header-only Threading Library (Alpha). Made in C++14


## Usage

Every function is called by from the Threading Engine.

```c++
using namespace Hexo;

ThreadingEngine hxt = ThreadingEngine();
```

\
All spawned threads are tracked and linked with the life of ThreadingEngine through a `Hexo::ResourceList`. But the Thread pools are not tracked and should be destroyed manually.



**There are 5 spawnable components in the library**

****
- **Immediate Threads**: They are used as normal threads. They take in data and function to run at construction. They can be joined or left alone to run asynchronously. They destroy when the task is finished.

```c++
int num = 1645;

HXImmediateThread t = hxt.SpawnImmediateThread(num,
[&num](void* data){
num *= 10;
}
);

hxt.JoinThread(t);
std::cout << "Immediate thread: " << num << '\n';
```
****
- **Worker Threads**: They are threads live until explicitly destroyed. They take in data and function to run at every task you submit. They cannot be joined but they can be waited on by measuring it's taskQueue size.
```c++
HXWorkerThread t = hxt.SpawnWorkerThread();
for (int i=0; i<10; i++){
int num = 1645 + i;
hxt.SubmitTask(t, num,
[](void* data){
int num = *(reinterpret_cast<int*>(data)) * 10;
std::cout << "Worker thread: " << num << '\n';
}
);
}
//wait for worker thread to callback
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}
hxt.DestroyThread(t);
```


****
- **Dedicated Threads**: They are threads that live on until explicitly destroyed. They run on just one function which is specified at construction. They take in only data at every task you submit. They cannot be joined but they can be waited on by measuring it's taskQueue size.

```c++
HXDedicatedThread t = hxt.SpawnDedicatedThread(
[](void* data){
int num = *(reinterpret_cast<int*>(data)) * 10;
std::cout << "Dedicated thread: " << num << '\n';
}
);

for (int i=0; i<10; i++){
int num = 1645 + i;
hxt.SubmitTask(t, num);
}

//wait for dedicated thread to callback
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}


hxt.DestroyThread(t);
```


****
- **Worker Thread Pools**: They are a pool of Worker Threads. They are given their number of threads at construction and take in the same parameters at Worker Threads at every task. They cannot be joined but they can be waited on by measuring it's taskQueue size.

```c++
HXWorkerThreadPool t = hxt.SpawnWorkerPool(4);

int* numarray = new int[10];

for (int i=0; i<10; i++){
numarray[i] = 1645 + i;

hxt.SubmitTask(t, i,
[&numarray](void* data){
int i = *(reinterpret_cast<int*>(data));
numarray[i] *= 10;
}
);
}

//wait for worker thread pool to finish
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}

hxt.DestroyPool(t);

for (size_t i = 0; i < 10; i++) {
std::cout << "Worker pool: " << numarray[i] << '\n';
}
delete[] numarray;
```


****
- **Dedicated Thread Pools**: They are a pool of Dedicated Threads. They are given their number of threads at construction and take in the same parameters at Dedicated Threads at every task. They cannot be joined but they can be waited on by measuring it's taskQueue size.

```c++
HXDedicatedThreadPool t = hxt.SpawnDedicatedPool(4,
[](void* data){
**(reinterpret_cast<int**>(data)) *= 10;
}
);

int* numarray = new int[10];

for (int i=0; i<10; i++){
numarray[i] = 1645 + i;
int* ptr = numarray+i;
hxt.SubmitTask(t, ptr);
}

//wait for dedicated thread pool to finish
while (true){
volatile size_t s = hxt.GetQueueSize(t);
if (!s)break;
}

hxt.DestroyPool(t);

for (size_t i = 0; i < 10; i++) {
std::cout << "Dedicated pool: " << numarray[i] << '\n';
}
delete[] numarray;
```


****
## Future Plans
This library is still in its early alpha stages. It was originally planned to be part of the Hexo Engine, and will still be part, but now I'm making this independently along with some other modules of Hexo. I will continue developing it and adding more functionality and optimizations in the future.
Binary file modified app.exe
Binary file not shown.
Binary file modified app.ilk
Binary file not shown.
Binary file modified app.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


using namespace Hexo;
using namespace Hexo::Threading;



Expand Down Expand Up @@ -182,6 +181,7 @@ int main() {
for (size_t i = 0; i < 10; i++) {
std::cout << "Dedicated pool: " << numarray[i] << '\n';
}
delete[] numarray;

};
/////
Expand Down

0 comments on commit 63b795b

Please sign in to comment.