C++ c++;不适合打电话给
我编写了以下代码,其中给出了一个错误 ipt.cpp:在函数“bool isprimet(长无符号整数,整数)”中: ipt.cpp:28:86:错误:调用“(std::thread)(void(&)(长无符号int,长无符号int,长无符号int,bool),const长无符号int&,长无符号int&,长无符号int&)时不匹配 对于(无符号长c=0;c 我做错了什么C++ c++;不适合打电话给,c++,compiler-errors,C++,Compiler Errors,我编写了以下代码,其中给出了一个错误 ipt.cpp:在函数“bool isprimet(长无符号整数,整数)”中: ipt.cpp:28:86:错误:调用“(std::thread)(void(&)(长无符号int,长无符号int,长无符号int,bool),const长无符号int&,长无符号int&,长无符号int&)时不匹配 对于(无符号长c=0;c 我做错了什么 #include <iostream> #include <thread> #include <
#include <iostream>
#include <thread>
#include <math.h>
using namespace std;
void ipt(const unsigned long number, const unsigned long root, const unsigned long threadid, bool &result)
{
result=true;
for (unsigned long c=5+threadid*6;c<=root;c+=(threadid+1)*6)
{
if(number % c-1 == 0) {result=false; break;};
if(number % c+1 == 0) {result=false; break;};
}
}
bool isprimet(const unsigned long number, const int nthreads)
{
if (number > 1)
{
if (number > 3)
{
if (number % 2 == 0) return false;
if (number % 3 == 0) return false;
unsigned int results[nthreads];
unsigned long root=(unsigned long)floor(sqrt(number))+1;
thread t[nthreads];
for (unsigned long c=0;c<nthreads;c++) t[c](ipt, number, root, c, results[c]);
for (unsigned long c=0;c<nthreads;c++) t[c].join();
for (unsigned long c=0;c<nthreads;c++) if (results[c]==false) return false;
return true;
}
else return true;
}
else return false;
}
#包括
#包括
#包括
使用名称空间std;
void ipt(常量无符号长编号、常量无符号长根、常量无符号长线程ID、bool和result)
{
结果=真;
for(无符号长c=5+threadid*6;c 1)
{
如果(数量>3)
{
if(数字%2==0)返回false;
if(数字%3==0)返回false;
无符号整数结果[n读取];
无符号长根=(无符号长)楼层(sqrt(编号))+1;
螺纹t[n螺纹];
对于(unsigned long c=0;c当使用std::thread
时,您需要向std::thread
的构造函数发送一个可调用对象,您可以使用Lambda表达式来实现此目的:
t[c] = new thread([&](){ ipt(number, root, c, results[c]); });
以下代码起作用:
#include <thread>
#include <math.h>
#include <iostream>
using namespace std;
static int const MAX_THREADS = 128;
void ipt(const unsigned long number, const unsigned long root, const unsigned long threadid, bool &result)
{
result = true;
for (unsigned long c = 5 + threadid * 6; c <= root; c += (threadid + 1) * 6)
{
if (number % c - 1 == 0) { result = false; break; };
if (number % c + 1 == 0) { result = false; break; };
}
}
bool isprimet(const unsigned long number, const unsigned long nthreads)
{
if (number > 1)
{
if (number > 3)
{
if (number % 2 == 0) return false;
if (number % 3 == 0) return false;
bool results[MAX_THREADS];
unsigned long root = (unsigned long)floor(sqrt(number)) + 1;
thread* t[MAX_THREADS];
for (unsigned long c = 0; c < nthreads; c++)
t[c] = new thread([&](){ ipt(number, root, c, results[c]); });
for (unsigned long c = 0; c < nthreads; c++) {
t[c]->join();
delete t[c];
}
for (unsigned long c = 0; c < nthreads; c++)
if (results[c] == false)
return false;
return true;
}
else return true;
}
else return false;
}
int main(int argc, char *argv[])
{
for (int i = 1; i < 100; ++i)
if (isprimet(i,5))
cout << i << "\n";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
静态int const MAX_THREADS=128;
void ipt(常量无符号长编号、常量无符号长根、常量无符号长线程ID、bool和result)
{
结果=真;
for(无符号长c=5+threadid*6;c 1)
{
如果(数量>3)
{
if(数字%2==0)返回false;
if(数字%3==0)返回false;
bool结果[最大线程数];
无符号长根=(无符号长)楼层(sqrt(编号))+1;
螺纹*t[最大螺纹];
for(无符号长c=0;cjoin();
删除t[c];
}
for(无符号长c=0;c cout当使用std::thread
时,您需要向std::thread
的构造函数发送一个可调用对象,您可以使用Lambda表达式来实现此目的:
t[c] = new thread([&](){ ipt(number, root, c, results[c]); });
以下代码起作用:
#include <thread>
#include <math.h>
#include <iostream>
using namespace std;
static int const MAX_THREADS = 128;
void ipt(const unsigned long number, const unsigned long root, const unsigned long threadid, bool &result)
{
result = true;
for (unsigned long c = 5 + threadid * 6; c <= root; c += (threadid + 1) * 6)
{
if (number % c - 1 == 0) { result = false; break; };
if (number % c + 1 == 0) { result = false; break; };
}
}
bool isprimet(const unsigned long number, const unsigned long nthreads)
{
if (number > 1)
{
if (number > 3)
{
if (number % 2 == 0) return false;
if (number % 3 == 0) return false;
bool results[MAX_THREADS];
unsigned long root = (unsigned long)floor(sqrt(number)) + 1;
thread* t[MAX_THREADS];
for (unsigned long c = 0; c < nthreads; c++)
t[c] = new thread([&](){ ipt(number, root, c, results[c]); });
for (unsigned long c = 0; c < nthreads; c++) {
t[c]->join();
delete t[c];
}
for (unsigned long c = 0; c < nthreads; c++)
if (results[c] == false)
return false;
return true;
}
else return true;
}
else return false;
}
int main(int argc, char *argv[])
{
for (int i = 1; i < 100; ++i)
if (isprimet(i,5))
cout << i << "\n";
return 0;
}
#包括
#包括
#包括
使用名称空间std;
静态int const MAX_THREADS=128;
void ipt(常量无符号长编号、常量无符号长根、常量无符号长线程ID、bool和result)
{
结果=真;
for(无符号长c=5+threadid*6;c 1)
{
如果(数量>3)
{
if(数字%2==0)返回false;
if(数字%3==0)返回false;
bool结果[最大线程数];
无符号长根=(无符号长)楼层(sqrt(编号))+1;
螺纹*t[最大螺纹];
for(无符号长c=0;cjoin();
删除t[c];
}
for(无符号长c=0;c cout您的代码有两个问题,它们都发生在这一行:
for (unsigned long c=0;c<nthreads;c++) t[c](ipt, number, root, c, results[c]);
for(unsigned long c=0;c您的代码有两个问题,它们都出现在此行中:
for (unsigned long c=0;c<nthreads;c++) t[c](ipt, number, root, c, results[c]);
for(无符号长c=0;c