StrIdx 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/thread_pool.hpp ADDED
@@ -0,0 +1,98 @@
1
+
2
+ // Based on example in https://www.geeksforgeeks.org/thread-pool-in-cpp/
3
+
4
+ #include <condition_variable>
5
+ #include <functional>
6
+ #include <iostream>
7
+ #include <mutex>
8
+ #include <queue>
9
+ #include <thread>
10
+ #include <algorithm>
11
+ #include <iostream>
12
+ #include <fstream>
13
+ #include <vector>
14
+ #include <string>
15
+ #include <chrono>
16
+
17
+ class ThreadPool {
18
+ public:
19
+ // Create a thread pool with given number of threads
20
+ ThreadPool(size_t num_threads) {
21
+
22
+ // Creating worker threads
23
+ for (size_t i = 0; i < num_threads; ++i) {
24
+ workerThreads.emplace_back([this] {
25
+ while (true) {
26
+ std::function<void()> task;
27
+ {
28
+ std::unique_lock<std::mutex> lock(mu_queue);
29
+
30
+ // Waiting until there is a task to execute or the pool is stopped
31
+ cv_.wait(lock, [this] { return !taskQueue.empty() || stop_; });
32
+
33
+ // Exit the thread in case the pool is stopped and there are no tasks
34
+ if (stop_ && taskQueue.empty()) {
35
+ return;
36
+ }
37
+
38
+ // Get the next task from the queue
39
+ task = std::move(taskQueue.front());
40
+ taskQueue.pop();
41
+ }
42
+
43
+ task();
44
+ }
45
+ });
46
+ }
47
+ }
48
+
49
+ // Destructor to stop the thread pool
50
+ ~ThreadPool() {
51
+ {
52
+ std::lock_guard<std::mutex> lock(mu_queue);
53
+ stop_ = true;
54
+ }
55
+
56
+ // Notify all threads
57
+ cv_.notify_all();
58
+
59
+ // Joining all worker threads to ensure they have
60
+ // completed their tasks
61
+ for (auto &thread : workerThreads) {
62
+ thread.join();
63
+ }
64
+ }
65
+
66
+ // Wait until all tasks assigned to the threads have been finished
67
+ void waitUntilDone() {
68
+ while (true) {
69
+ {
70
+ std::lock_guard<std::mutex> guard(mu_queue);
71
+ if (taskQueue.empty()) {
72
+ return;
73
+ }
74
+ }
75
+ std::this_thread::sleep_for(std::chrono::milliseconds(50));
76
+ }
77
+ }
78
+
79
+ // Enqueue task for execution by the thread pool
80
+ void enqueue(std::function<void()> task) {
81
+ {
82
+ std::lock_guard<std::mutex> lock(mu_queue);
83
+ taskQueue.emplace(move(task));
84
+ }
85
+ cv_.notify_one();
86
+ }
87
+
88
+ private:
89
+ std::vector<std::thread> workerThreads;
90
+ std::queue<std::function<void()>> taskQueue;
91
+ std::mutex mu_queue;
92
+
93
+ // Condition variable to signal changes in the state of the tasks queue
94
+ std::condition_variable cv_;
95
+
96
+ // Flag to indicate whether the thread pool should stop
97
+ bool stop_ = false;
98
+ };
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: StrIdx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sami Sieranoja
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-05 00:00:00.000000000 Z
11
+ date: 2024-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -50,15 +50,19 @@ files:
50
50
  - Makefile
51
51
  - README.md
52
52
  - demo.cpp
53
+ - flist.txt
53
54
  - rubyext/extconf.rb
54
55
  - rubyext/ruby_interf.cpp
55
56
  - stridx.hpp
56
57
  - test.rb
58
+ - thread_pool.hpp
57
59
  - unordered_dense.h
58
60
  homepage: https://github.com/SamiSieranoja/stridx
59
61
  licenses:
60
62
  - LGPL-2.0+
61
- metadata: {}
63
+ metadata:
64
+ source_code_uri: https://github.com/SamiSieranoja/stridx
65
+ homepage_uri: https://github.com/SamiSieranoja/stridx
62
66
  post_install_message:
63
67
  rdoc_options: []
64
68
  require_paths: