Showing
6 changed files
with
101 additions
and
11 deletions
... | @@ -52,7 +52,8 @@ | ... | @@ -52,7 +52,8 @@ |
52 | "optional": "cpp", | 52 | "optional": "cpp", |
53 | "string_view": "cpp", | 53 | "string_view": "cpp", |
54 | "cstdarg": "cpp", | 54 | "cstdarg": "cpp", |
55 | - "iomanip": "cpp" | 55 | + "iomanip": "cpp", |
56 | + "string": "cpp" | ||
56 | }, | 57 | }, |
57 | "python.pythonPath": "/usr/bin/python3", | 58 | "python.pythonPath": "/usr/bin/python3", |
58 | "editor.formatOnSave": true, | 59 | "editor.formatOnSave": true, | ... | ... |
This diff is collapsed. Click to expand it.
1 | #ifndef MOTIF_H_ | 1 | #ifndef MOTIF_H_ |
2 | #define MOTIF_H_ | 2 | #define MOTIF_H_ |
3 | 3 | ||
4 | +#include <mutex> | ||
4 | #include <string> | 5 | #include <string> |
5 | #include <vector> | 6 | #include <vector> |
6 | 7 | ||
... | @@ -8,6 +9,16 @@ using std::pair; | ... | @@ -8,6 +9,16 @@ using std::pair; |
8 | using std::string; | 9 | using std::string; |
9 | using std::vector; | 10 | using std::vector; |
10 | 11 | ||
12 | +class Motif; // forward declaration | ||
13 | + | ||
14 | +typedef struct args_ { | ||
15 | + const string& descfile; | ||
16 | + string rna; | ||
17 | + vector<Motif>& final_results; | ||
18 | + std::mutex& posInsertionSites_mutex; | ||
19 | + args_(const string& descfile_, string rna_, vector<Motif>& vector_, std::mutex& mutex_) : descfile(descfile_), rna(rna_), final_results(vector_), posInsertionSites_mutex(mutex_){} | ||
20 | +} args_of_parallel_func; | ||
21 | + | ||
11 | typedef struct Comp_ { | 22 | typedef struct Comp_ { |
12 | pair<uint, uint> pos; | 23 | pair<uint, uint> pos; |
13 | size_t k; | 24 | size_t k; |
... | @@ -25,15 +36,15 @@ class Motif | ... | @@ -25,15 +36,15 @@ class Motif |
25 | public: | 36 | public: |
26 | Motif(); | 37 | Motif(); |
27 | Motif(const vector<Component>& v, string PDB); | 38 | Motif(const vector<Component>& v, string PDB); |
28 | - void load_from_csv(string csv_line); | 39 | + void load_from_csv(string csv_line); |
29 | - static vector<Motif> build_from_desc(const string& descfile, string rna); | 40 | + static void build_from_desc(args_of_parallel_func arg_struct); |
30 | - static char is_valid_DESC(const string& descfile); | 41 | + static char is_valid_DESC(const string& descfile); |
31 | - string pos_string(void) const; | 42 | + string pos_string(void) const; |
32 | - string get_origin(void) const; | 43 | + string get_origin(void) const; |
33 | - string get_identifier(void) const; | 44 | + string get_identifier(void) const; |
34 | - vector<Component> comp; | 45 | + vector<Component> comp; |
35 | - double score_; | 46 | + double score_; |
36 | - bool reversed_; | 47 | + bool reversed_; |
37 | 48 | ||
38 | private: | 49 | private: |
39 | static vector<vector<Component>> find_next_ones_in(string rna, uint offset, vector<string> vc); | 50 | static vector<vector<Component>> find_next_ones_in(string rna, uint offset, vector<string> vc); | ... | ... |
cppsrc/Pool.cpp
0 → 100644
1 | +#include "Pool.h" | ||
2 | + | ||
3 | +Pool::Pool() : m_function_queue(), m_lock(), m_data_condition(), m_accept_functions(true) | ||
4 | +{ | ||
5 | +} | ||
6 | + | ||
7 | +Pool::~Pool() | ||
8 | +{ | ||
9 | +} | ||
10 | + | ||
11 | +void Pool::push(std::function<void()> func) | ||
12 | +{ | ||
13 | + std::unique_lock<std::mutex> lock(m_lock); | ||
14 | + m_function_queue.push(func); | ||
15 | + // when we send the notification immediately, the consumer will try to get the lock , so unlock asap | ||
16 | + lock.unlock(); | ||
17 | + m_data_condition.notify_one(); | ||
18 | +} | ||
19 | + | ||
20 | +void Pool::done() | ||
21 | +{ | ||
22 | + std::unique_lock<std::mutex> lock(m_lock); | ||
23 | + m_accept_functions = false; | ||
24 | + lock.unlock(); | ||
25 | + // when we send the notification immediately, the consumer will try to get the lock , so unlock asap | ||
26 | + m_data_condition.notify_all(); | ||
27 | + //notify all waiting threads. | ||
28 | +} | ||
29 | + | ||
30 | +void Pool::infinite_loop_func() | ||
31 | +{ | ||
32 | + std::function<void()> func; | ||
33 | + while (true) | ||
34 | + { | ||
35 | + { | ||
36 | + std::unique_lock<std::mutex> lock(m_lock); | ||
37 | + m_data_condition.wait(lock, [this]() {return !m_function_queue.empty() || !m_accept_functions; }); | ||
38 | + if (!m_accept_functions && m_function_queue.empty()) | ||
39 | + { | ||
40 | + //lock will be release automatically. | ||
41 | + //finish the thread loop and let it join in the main thread. | ||
42 | + return; | ||
43 | + } | ||
44 | + func = m_function_queue.front(); | ||
45 | + m_function_queue.pop(); | ||
46 | + //release the lock | ||
47 | + } | ||
48 | + func(); | ||
49 | + } | ||
50 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
cppsrc/Pool.h
0 → 100644
1 | +#pragma once | ||
2 | +#include <atomic> | ||
3 | +#include <cassert> | ||
4 | +#include <condition_variable> | ||
5 | +#include <functional> | ||
6 | +#include <mutex> | ||
7 | +#include <queue> | ||
8 | + | ||
9 | +class Pool | ||
10 | +{ | ||
11 | + | ||
12 | + private: | ||
13 | + std::queue<std::function<void()>> m_function_queue; | ||
14 | + std::mutex m_lock; | ||
15 | + std::condition_variable m_data_condition; | ||
16 | + std::atomic<bool> m_accept_functions; | ||
17 | + | ||
18 | + public: | ||
19 | + Pool(); | ||
20 | + ~Pool(); | ||
21 | + std::mutex& get_mutex_reference(void); | ||
22 | + void push(std::function<void()> func); | ||
23 | + void done(); | ||
24 | + void infinite_loop_func(); | ||
25 | +}; | ||
26 | + | ||
27 | +inline std::mutex &Pool::get_mutex_reference(void) { return m_lock; } | ||
28 | + | ||
29 | +class quit_worker_exception : public std::exception {}; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment