我试图通过为每个向量分派N个线程来并行排序向量的NxN向量。我希望每次向量矢量中的每个矢量被排序时显示矢量矢量。请参阅下面的示例。
首先
2,1,3,4
1,3,2,4
3,4,1,2
3,2,1,4
排序 。 。 显示
1,2,3,4
1,2,3,4
1,4,3,2
2,3,1,4
。 。 排序 。
1,2,3,4
1,2,3,4
1,3,4,2
2,1,3,4
......等等..
我有一些可执行代码来执行此操作,我尝试使用条件变量来完成此操作,但我根本无法使用条件变量。
下面是顺序代码,它在排序矢量矢量方面起作用,但它不能产生我想要的显示。
#include<iostream>
#include<vector>
#include <ctime>
#include <random>
std::vector<int> row;
std::vector<std::vector<int>> block;
int cols = 10;
auto rng = std::default_random_engine{};
void init()
{
srand((unsigned)time(0));
for (int i = 0; i < 10; i++)
{
int j;
j = (rand() % 100) + 1;
row.push_back(j);
}
for (int i = 0; i < 10; i++)
{
std::vector<int> y;
std::shuffle(std::begin(row), std::end(row), rng);
y = row;
block.push_back(y);
}
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << block[i][j] << ", ";
}
std::cout << "\n";
}
}
void Sort(std::vector<int> &Row)
{
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < i; j++)
{
if (Row[i] < Row[j])
{
int temp = Row[i];
Row[i] = Row[j];
Row[j ] = temp;
}
}
}
}
void display()
{
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < cols; j++)
{
std::cout << block[i][j] << ", ";
}
std::cout << "\n";
}
}
int main() {
std::cout << "test\n";
init();
std::cout << "\n";
std::cout << "Sorting";
std::cout << "\n";
for(int i =0; i < cols; i++)
Sort(block[i]);
std::cout << "\n";
display();
std::cout << "Sorted";
getchar();
}
以上代码的输出如下
98, 15, 13, 10, 44, 63, 85, 93, 39, 43,
93, 10, 15, 13, 43, 44, 39, 63, 85, 98,
93, 13, 63, 15, 43, 85, 98, 39, 44, 10,
44, 98, 39, 85, 13, 10, 63, 43, 93, 15,
10, 98, 63, 93, 85, 44, 39, 15, 13, 43,
63, 39, 44, 98, 93, 15, 43, 85, 13, 10,
43, 63, 93, 44, 15, 39, 10, 85, 98, 13,
39, 85, 13, 63, 44, 98, 93, 43, 10, 15,
39, 44, 85, 63, 43, 93, 98, 10, 15, 13,
15, 43, 44, 93, 85, 39, 63, 10, 98, 13,
Sorting
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
10, 13, 15, 39, 43, 44, 63, 85, 93, 98,
Sorted
我在下面采用的多线程方法没有按预期工作。我尝试过很多东西但没有成功。
#include<iostream>
#include <thread>
#include<vector>
#include <ctime>
#include <mutex>
#include<chrono>
#include <algorithm>
#include <random>
#include <deque>
#include <condition_variable>
std::deque<int> q;
std::mutex mu;
std::condition_variable cond;
int count = 4;
std::vector<int> x{ 5,2,1,3,4 };
std::vector<std::vector<int>> xx;
void init() {
for (int i = 0; i < 5; i++)
{
xx.push_back(x);
}
}
void display()
{
for (int i = 0; i < xx.size(); i++)
{
for (int j = 0; j < xx[i].size(); j++)
{
std::cout << xx[i][j] << " ,";
}
std::cout << "\n";
}
}
bool isSorted(int z)
{
for (int i = 0; i < 5; i++)
{
if (xx[z][i] > xx[z][i + 1])
return false;
}
return true;
}
void function_1(int &row)
{
while (!isSorted(row))
{
std::unique_lock<std::mutex> locker(mu);
for (int i = 0; i < 4; i++)
{
if (xx[row][i] > xx[row][i + 1])
{
int temp = xx[row][i];
xx[row][i] = xx[row][i + 1];
xx[row][i + 1] = temp;
}
}
count--;
locker.unlock();
cond.notify_one();
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
void function_2() {
int data = 0;
while (data != 1)
{
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker, []() {return (count == 0); });
q.clear();
display();
count = 4;
locker.unlock();
}
}
int main()
{
init();
for (int i = 0; i < 4; i++) {
std::thread *t1 = new std::thread(function_1, std::ref(i));
}
std::thread t2(function_2);
// t2.join();
std::cout << " all threads done";
getchar();
}
输出是这样的(当使用g ++和c ++ 11标准集编译时):
main.cpp: In function 'void display()':
main.cpp:27:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < xx.size(); i++)
~~^~~~~~~~~~~
main.cpp:29:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < xx[i].size(); j++)
~~^~~~~~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:89:22: warning: unused variable 't1' [-Wunused-variable]
std::thread *t1 = new std::thread(function_1, std::ref(i));
^~
terminate called without an active exception
all threads done5 ,2 ,1 ,3 ,4 ,
5 ,2 ,1 ,3 ,4 ,
2 ,1 ,3 ,4 ,5 ,
2 ,1 ,3 ,4 ,5 ,
1 ,2 ,3 ,4 ,5 ,
这是the demo。
答案 0 :(得分:0)
您遇到的最大问题是从main传递i
作为参考。所有线程都将共享循环退出的4
。
for (int i = 0; i < 4; i++) {
std::thread *t1 = new std::thread(function_1, std::ref(i)); //same i !!
}
然后:
void function_1(int &row)...
在退出循环之前,i+1
也不是5,因此它超出了范围:
for (int i = 0; i < 5; i++)
{
if (xx[z][i] > xx[z][i + 1])
return false;
}
然后,正如所指出的那样,只需要加入线程等待完成。这似乎工作正常,但我会用一个类完成这个,所以没有全局变量。
我把包括了......
std::mutex mu;
std::vector<int> x{ 5,2,1,3,4 };
std::vector<std::vector<int>> xx;
void init() {
for (int i = 0; i < 5; i++)
{
xx.push_back(x);
}
}
void display( int in )
{
for (size_t i = 0; i < xx.size(); i++)
{
for (size_t j = 0; j < xx[i].size(); j++)
{
std::cout << xx[i][j] << ", ";
}
std::cout << std::endl;
}
std::cout << " id: " << in << "\n";
}
bool isSorted( int z )
{
for (size_t i = 0; i < 4; i++)
{
if (xx[z][i] > xx[z][i + 1])
return false;
}
return true;
}
void function_1( int row )
{
for( ; ! isSorted(row); )
{
for (int i = 0; i < 4; i++)
{
if (xx[row][i] > xx[row][i + 1])
{
int temp = xx[row][i];
xx[row][i] = xx[row][i + 1];
xx[row][i + 1] = temp;
}
}
std::unique_lock<std::mutex> locker(mu);
display( row );
locker.unlock( );
}
std::unique_lock<std::mutex> locker(mu);
std::cout << "end: " << row << std::endl;
locker.unlock( );
}
int main()
{
std::vector< std::thread* > threads;
init();
for (int i = 0; i < 4; i++)
{
std::unique_lock<std::mutex> locker(mu);
threads.push_back( new std::thread( function_1, i ) );
std::cout << "started: " << i << std::endl;
locker.unlock( );
}
for( auto thread : threads )
thread->join( );
std::cout << " all threads done";
return 0;
}