Multithreading c++;11互斥量向量,来自;“本地”;至;“全球”;同步 我对C++特别是多线程有点陌生。现在我想让事情保持非面向对象。我已经写了下面的代码,它编译了,这让我更害怕,因为我在论坛上读到不能真正使用互斥向量。所以我想知道下面的代码是否正确,以及您会带来什么修改。请记住,我不能从根本上改变代码的设计 #include <iostream> #include <string> #include <vector> #include <thread> #include <mutex> using namespace std; mutex print_mutex; void print_multithread(string message, mutex& print_mutex) { std::lock_guard<std::mutex> guard(print_mutex); cout << message << endl; } void local_function(int function_index, vector<string> & comm, mutex &comm_mutex) { string local_data; while (true) { //read the communication data structure { std::unique_lock<std::mutex> guard(comm_mutex); local_data = comm.at(function_index); } //do stuff... update local_data //write the communication data_structure { std::unique_lock<std::mutex> guard(comm_mutex); comm.at(function_index) = local_data; } }//end while(true) } void global_function(vector<string> & comm, mutex & comm_mutex) { vector<string> global_data; while (true) { //read the communication data structure { std::unique_lock<std::mutex> guard(comm_mutex); global_data = comm; } //do stuff... update global_data //write the communication data_structure { std::unique_lock<std::mutex> guard(comm_mutex); comm = global_data; } } } int main() { vector<string> comm = { "init1", "init2" }; mutex comm_mutex; thread local_fct0(local_function, 0, ref(comm), ref(comm_mutex)); thread local_fct1(local_function, 1, ref(comm), ref(comm_mutex)); thread global_fct(global_function, ref(comm), ref(comm_mutex)); local_fct0.join(); local_fct1.join(); global_fct.join(); cin.ignore(); return 0; }

Multithreading c++;11互斥量向量,来自;“本地”;至;“全球”;同步 我对C++特别是多线程有点陌生。现在我想让事情保持非面向对象。我已经写了下面的代码,它编译了,这让我更害怕,因为我在论坛上读到不能真正使用互斥向量。所以我想知道下面的代码是否正确,以及您会带来什么修改。请记住,我不能从根本上改变代码的设计 #include <iostream> #include <string> #include <vector> #include <thread> #include <mutex> using namespace std; mutex print_mutex; void print_multithread(string message, mutex& print_mutex) { std::lock_guard<std::mutex> guard(print_mutex); cout << message << endl; } void local_function(int function_index, vector<string> & comm, mutex &comm_mutex) { string local_data; while (true) { //read the communication data structure { std::unique_lock<std::mutex> guard(comm_mutex); local_data = comm.at(function_index); } //do stuff... update local_data //write the communication data_structure { std::unique_lock<std::mutex> guard(comm_mutex); comm.at(function_index) = local_data; } }//end while(true) } void global_function(vector<string> & comm, mutex & comm_mutex) { vector<string> global_data; while (true) { //read the communication data structure { std::unique_lock<std::mutex> guard(comm_mutex); global_data = comm; } //do stuff... update global_data //write the communication data_structure { std::unique_lock<std::mutex> guard(comm_mutex); comm = global_data; } } } int main() { vector<string> comm = { "init1", "init2" }; mutex comm_mutex; thread local_fct0(local_function, 0, ref(comm), ref(comm_mutex)); thread local_fct1(local_function, 1, ref(comm), ref(comm_mutex)); thread global_fct(global_function, ref(comm), ref(comm_mutex)); local_fct0.join(); local_fct1.join(); global_fct.join(); cin.ignore(); return 0; },multithreading,c++11,concurrency,reference,pass-by-reference,Multithreading,C++11,Concurrency,Reference,Pass By Reference,我知道这不是做这些事情的最佳方式,但不幸的是我没有时间精通OOP,所以我想保持它的功能形式 提前感谢,我感谢任何批评 欢迎来到Stackoverflow-您最好在codereview.stackexchange.com上发布这个问题,它更适合那里。互斥向量可以工作,只要您不调用任何可能导致vector扩展其存储或移动元素的方法(因为它会发现std::mutex是不可复制或移动的)。但是如果你从来没有调用过这样的方法,那么首先就不清楚你为什么想要vector。你也可以使用普通数组,就像std::m

我知道这不是做这些事情的最佳方式,但不幸的是我没有时间精通OOP,所以我想保持它的功能形式


提前感谢,我感谢任何批评

欢迎来到Stackoverflow-您最好在codereview.stackexchange.com上发布这个问题,它更适合那里。互斥向量可以工作,只要您不调用任何可能导致vector扩展其存储或移动元素的方法(因为它会发现
std::mutex
是不可复制或移动的)。但是如果你从来没有调用过这样的方法,那么首先就不清楚你为什么想要vector。你也可以使用普通数组,就像
std::mutex comm_mutex[2]一样
我之所以使用向量,主要是因为它们是很好的圆形对象,但正如您所指出的@IgorTandetnik,它们的大小不会改变。我的主要问题是,我需要同时传递多个互斥体,我不知道有多少互斥体,但一旦知道它们的数量,这个数字就不会改变。
int main() {
    vector<vector<string>> comm_matrix = { { "init1A", "init2A" } , {"init1B", "init2B"} };
    vector<mutex> comm_mutex(2);

    thread local_fct0A(local_function, 0, ref(comm_matrix.at(0)),   ref(comm_mutex.at(0)));
    thread local_fct1A(local_function, 1, ref(comm_matrix.at(0)), ref(comm_mutex.at(0)));
    thread global_fctA(global_function, ref(comm_matrix.at(0)), ref(comm_mutex.at(0)));

    thread local_fct0B(local_function, 0, ref(comm_matrix.at(1)), ref(comm_mutex.at(1)));
    thread local_fct1B(local_function, 1, ref(comm_matrix.at(1)), ref(comm_mutex.at(1)));
    thread global_fctB(global_function, ref(comm_matrix.at(1)), ref(comm_mutex.at(1)));

    local_fct0A.join();
    local_fct1A.join();
    global_fctA.join();

    local_fct0B.join();
    local_fct1B.join();
    global_fctB.join();

    cin.ignore();
    return 0;
}
void global_global_function(vector<vector<string>> & comm_matrix, vector<mutex> & comm_mutexes) {}
thread global_global_fct(global_global_function, ref(comm_matrix), ref(comm_mutex));
global_global_fct.join();