std::condition_variable waitforme;
std::mutex block;
std::atomic<bool>check = false;
void cpu_show_answer(std::vector<int>& v1, const std::vector<std::string>& v2) {
while (check == false) {
}
for (int i = 0; i < v1.size(); i++) {
if (v1[i] != NULL) {
cout << "Found your word: " << v2[v1[i]] << " at index " << v1[i] + 1 << endl;
}
}
}
void cpu_parallel_search(const int begin, const int end, std::string search_term, std::vector<int>& v1, const std::vector<std::string>& v2) {
for (int i = begin; i < end && check==false; i++) {
if (search_term == v2[i]) {
std::unique_lock<std::mutex> l(block);
v1.push_back(i);
check=true;
}
}
}
main(){
int threads=0;
cout << " THREADS: ";
std::cin >> threads;
int begin = 0;
int finish = (words.size() / threads) - 1;
std::string search_term = "";
while (search_term == "") {
cout << "Please enter the word you want to find: ";
std::cin >> search_term;
}
the_amp_clock::time_point start1 = the_amp_clock::now();
std::vector<std::thread> myt;
for (int x = 0; x < threads; x++) {
myt.emplace_back(cpu_parallel_search, begin, finish, search_term, looking, words);
begin = begin + words.size() / threads;
finish = finish + words.size() / threads;
}
cpu_show_answer(looking, words);
for (auto& x : myt) {
x.join();
}
the_amp_clock::time_point end1 = the_amp_clock::now();
auto time_taken1 = duration_cast<milliseconds>(end1 - start1).count();
cout << "Finding the word took " << time_taken1 << " ms. Using a parallel cpu search.\n" << endl;
}
Привет, поэтому моя программа просматривает вектор слов и видит, есть ли совпадение, если есть совпадение, тогда я нашел слово и хочу показать пользователю, что у меня есть его слово, и сообщить ему индекс в списке. где слово было найдено. Моя проблема в том, что функция cpu_show_answer () фактически никогда не запускает фрагмент кода, где она отображает слово и индекс (цикл for в cpu_show_answer). Причина, по которой он не запускает код, состоит в том, что даже если я нашел слово, вектор, содержащий размер найденных слов, всегда равен нулю в функции cpu_show_answer. Я не знаю, почему это так.