Работаю с потоками первый раз. Подскажите как решить проблему. Мне нужно в первом потоке сгенерировать строку и записать ее в list, а в другом принять эту строку, отсортировать и записать в другой list. Проблема в том, что программа работает не всегда корректно, то createList
сработает два раза, а потом только sortList
, то еще как-то. Как можно это решить?
#include<iostream>
#include<cstdlib>
#include<vector>
#include<mutex>
#include<thread>
#include<algorithm>
#include<string>
#include<list>
using namespace std;
const int elementsCount = 10;
void PrintList(list<string> list_)
{
cout << "LIST" << endl;
for (list<string>::iterator it = list_.begin(); it != list_.end(); it++)
{
cout << *it << ' ';
}
cout << endl;
}
void createList(list<string> &l, mutex& m_arr, mutex& m_out) {
srand(time(NULL));
int count;
string str;
char str_char[1];
for (int i = 0; i < elementsCount; i++) {
m_arr.lock();
count = rand() % 8 + 1;
for (size_t i = 0; i < count; i++)
{
str_char[0] = char(rand() % 26 + 0x61);
str += string(str_char, sizeof(str_char));
}
l.push_back(str);
str.clear();
m_arr.unlock();
m_out.lock();
cout << "1 ------ Create " << l.back() << "\n";
m_out.unlock();
}
}
void sortList(list<string> &l, list<string> &l2, mutex& m_arr, mutex& m_out) {
int i = 0,j=0;
string str;
while (i < elementsCount) {
m_arr.lock();
if (l.size() > j) {
str = l.back();
sort(str.begin(), str.end());
l2.push_back(str);
j++;
m_out.lock();
cout << " Sort " << l2.back() << "\n";
m_out.unlock();
i++;
}
m_arr.unlock();
}
}
int main() {
mutex m_arr, m_out;
list<string> l1, l2;
thread push_thread(createList, ref(l1), ref(m_arr), ref(m_out));
thread pop_thread(sortList, ref(l1), ref(l2), ref(m_arr), ref(m_out));
if (push_thread.joinable()) push_thread.join();
if (pop_thread.joinable()) pop_thread.join();
PrintList(l1);
PrintList(l2);
system("pause");
return 0;
}