-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
160 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using NonsPlayer.Core.Services; | ||
using System.Collections.Concurrent; | ||
|
||
namespace NonsPlayer.Services; | ||
|
||
public class GlobalThreadPool | ||
{ | ||
public static GlobalThreadPool Instance { get; } = new(16); | ||
private readonly BlockingCollection<Action> _taskQueue = new(); | ||
private readonly Thread[] _workers; | ||
|
||
public GlobalThreadPool(int numberOfThreads) | ||
{ | ||
_workers = new Thread[numberOfThreads]; | ||
for (int i = 0; i < numberOfThreads; i++) | ||
{ | ||
_workers[i] = new Thread(Worker); | ||
_workers[i].Start(); | ||
} | ||
} | ||
|
||
private void Worker() | ||
{ | ||
foreach (var task in _taskQueue.GetConsumingEnumerable()) | ||
{ | ||
task(); | ||
} | ||
} | ||
|
||
public void Enqueue(Action task) | ||
{ | ||
_taskQueue.Add(task); | ||
} | ||
|
||
public void Shutdown() | ||
{ | ||
_taskQueue.CompleteAdding(); | ||
foreach (var worker in _workers) | ||
{ | ||
worker.Join(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters