I want to create a vector of threads which will doing same job, in my case I just want to take information from a vector of integers. For example; I will have got 4 threads and 10 integers. My threads will get the integers' values. That's all. So I've coded this part but I couldn't solve how can I manage thread vector. How can I read values from this vector by waiting for each thread for the other thread? I would appreciate it if you explain with a sample code snippet.
std::condition_variable cond;
std::mutex m_mutex;
std::atomic<int> position_holder {0};
void access_fonksiyon(std::vector<int> myvec){
std::unique_lock<std::mutex> locked {m_mutex};
position_holder++;
std::cout << "Locked." << std::endl;
std::cout << "Position holder value : "
<< position_holder
<< "thread ID: "
<< std::this_thread::get_id()
<< std::endl;
}
int main(){
std::vector<int> myvec = {1,2,3,4,5,6,7,8,9};
std::vector<std::thread> thread_vec;
for(int i = 0; i < 2; i++){
thread_vec.push_back(std::thread(access_fonksiyon, std::ref(myvec)));
}
for (std::thread &reader : thread_vec){
reader.join();
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire