Multithreading C++;线程模板向量快速排序
线程化快速排序方法:Multithreading C++;线程模板向量快速排序,multithreading,vector,quicksort,Multithreading,Vector,Quicksort,线程化快速排序方法: #include <iostream> #include <fstream> #include <string> #include <vector> #include "MD5.h" #include <thread> using namespace std; template<typename T> void quickSort(vector<T> &arr, int left,
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "MD5.h"
#include <thread>
using namespace std;
template<typename T>
void quickSort(vector<T> &arr, int left, int right) {
int i = left, j = right; //Make local copys to modify
T tmp; //Termorary variable to use for swaping.
T pivot = arr[(left + right) / 2]; //Find the centerpoint. if 0.5 truncate.
while (i <= j) {
while (arr[i] < pivot) //is i < pivot?
i++;
while (arr[j] > pivot) //Is j > pivot?
j--;
if (i <= j) { //Swap
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
thread left_t; //Left thread
thread right_t; //Right thread
if (left < j)
left_t = thread(quickSort<T>, ref(arr), left, j);
if (i < right)
right_t = thread(quickSort<T>, ref(arr), i, right);
if (left < j)
left_t.join();
if (left < j)
right_t.join();
}
int main()
{
vector<int> table;
for (int i = 0; i < 100; i++)
{
table.push_back(rand() % 100);
}
cout << "Before" << endl;
for each(int val in table)
{
cout << val << endl;
}
quickSort(table, 0, 99);
cout << "After" << endl;
for each(int val in table)
{
cout << val << endl;
}
char temp = cin.get();
return 0;
}
#包括
#包括
#包括
#包括
#包括“MD5.h”
#包括
使用名称空间std;
模板
无效快速排序(矢量和arr、整型左、整型右){
int i=left,j=right;//制作要修改的本地副本
T tmp;//用于交换的Termorary变量。
T pivot=arr[(左+右)/2];//找到中心点。如果0.5截断。
while(i-pivot)//j>pivot吗?
j--;
如果(i您在快速排序方面没有任何问题,但是在向线程传递模板化函数方面没有问题。没有函数快速排序
。您需要显式指定类型,以实例化函数模板:
#include <thread>
#include <iostream>
template<typename T>
void f(T a) { std::cout << a << '\n'; }
int main () {
std::thread t;
int a;
std::string b("b");
t = std::thread(f, a); // Won't work
t = std::thread(f<int>, a);
t.join();
t = std::thread(f<decltype(b)>, b); // a bit fancier, more dynamic way
t.join();
return 0;
}
#包括
#包括
模板
void f(ta){std::cout您在快速排序方面没有任何问题,但是在将模板化函数传递给线程方面没有任何问题。没有函数快速排序
。您需要显式指定类型,以实例化函数模板:
#include <thread>
#include <iostream>
template<typename T>
void f(T a) { std::cout << a << '\n'; }
int main () {
std::thread t;
int a;
std::string b("b");
t = std::thread(f, a); // Won't work
t = std::thread(f<int>, a);
t.join();
t = std::thread(f<decltype(b)>, b); // a bit fancier, more dynamic way
t.join();
return 0;
}
#包括
#包括
模板
voidf(ta){std::cout问题在于线程连接和@luk32所说的内容
需要将线程转换为指向线程的指针
thread* left_t = nullptr; //Left thread
thread* right_t = nullptr; //Right thread
if (left < j)
left_t = new thread(quickSort<T>, ref(arr), left, j);
if (i < right)
right_t = new thread(quickSort<T>, ref(arr), i, right);
if (left_t)
{
left_t->join();
delete left_t;
}
if (right_t)
{
right_t->join();
delete right_t;
}
thread*left\u t=nullptr;//左线程
thread*right\u t=nullptr;//右线程
if(左连接();
删除左\u t;
}
如果(右)
{
右键->连接();
删除右键;
}
似乎如果您创建了一个默认构造的线程对象。但不要使用它,它仍然希望被加入。如果您加入它,它会抱怨。问题在于线程加入和@luk32所说的
需要将线程转换为指向线程的指针
thread* left_t = nullptr; //Left thread
thread* right_t = nullptr; //Right thread
if (left < j)
left_t = new thread(quickSort<T>, ref(arr), left, j);
if (i < right)
right_t = new thread(quickSort<T>, ref(arr), i, right);
if (left_t)
{
left_t->join();
delete left_t;
}
if (right_t)
{
right_t->join();
delete right_t;
}
thread*left\u t=nullptr;//左线程
thread*right\u t=nullptr;//右线程
if(左连接();
删除左\u t;
}
如果(右)
{
右键->连接();
删除右键;
}
似乎如果您创建了一个默认构造的线程对象。但不使用它,它仍然希望被加入。如果您加入它,它会抱怨。K现在它似乎中止。可能是并发修改异常?是的,您需要加入
,或者处理每个构造的线程(我认为唯一的另一个选择是分离)在cppreference:“(析构函数):分解线程对象,基础线程必须被连接或分离”。现在它似乎中止。可能是并发修改异常?是的,您需要连接
,或者处理每个构造的线程(我认为唯一的其他选择是分离)在cppreference之后:“(析构函数):分解线程对象,底层线程必须被连接或分离”。