我试着写一个类成员,它并行多次调用另一个类成员.
我写了一个简单的问题示例,甚至无法编译.调用std :: async我做错了什么?我想问题就在于我如何传递函数.
#include <vector>
#include <future>
using namespace std;
class A
{
int a,b;
public:
A(int i=1, int j=2){ a=i; b=j;}
std::pair<int,int> do_rand_stf(int x,int y)
{
std::pair<int,int> ret(x+a,y+b);
return ret;
}
void run()
{
std::vector<std::future<std::pair<int,int>>> ran;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
auto hand=async(launch::async,do_rand_stf,i,j);
ran.push_back(hand);
}
}
for(int i=0;i<ran.size();i++)
{
pair<int,int> ttt=ran[i].get();
cout << ttt.first << ttt.second << endl;
}
}
};
int main()
{
A a;
a.run();
}
Run Code Online (Sandbox Code Playgroud)
汇编:
g++ -std=c++11 -pthread main.cpp
Run Code Online (Sandbox Code Playgroud) 所以我正在尝试编写一个与c ++ 11 lambdas一起使用的Integration函数.代码看起来像这样:
double Integrate(std::function<double(double,void*)> func, double a,double b,std::vector<double> & params)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
gsl_function F;
F.function =func;
F.params = (void*)¶ms;
double error,result;
gsl_integration_qag (&F, a, b, 0, 1e-7, 1000,GSL_INTEG_GAUSS61,w, &result, &error);
gsl_integration_workspace_free (w);
return result;
}
void Another_function()
{
//...
Integrate([](double a,void* param)
{
return ((vector<double> *)params)->at(0)*a+((vector<double> *)params)->at(1);
}
,0,3,{2,3});
}
Run Code Online (Sandbox Code Playgroud)
试图编译它,编译器说:
error: cannot convert ‘std::function<double(double, void*)>’ to ‘double (*)(double, void*)’ in assignment
Run Code Online (Sandbox Code Playgroud)
关于线
F.function =func;
Run Code Online (Sandbox Code Playgroud)
但如果我写:
F.function =[](double a,void* param)
{
return ((std::vector<double> …Run Code Online (Sandbox Code Playgroud) 我在Go中实现了一个简单的Web服务器.由于我没有网络开发的经验,这对我来说是一个严肃的问题.
假设我loadPage从这里提供具有修改功能的网页
func loadPage(title string) []byte {
filename := title
body, _ := ioutil.ReadFile(filename)
return body
}
func handler(w http.ResponseWriter, req *http.Request) {
content := loadPage(req.URL.Path[1:])
fmt.Fprintf(w, "%s", content)
}
Run Code Online (Sandbox Code Playgroud)
从技术上讲,这允许我以一种形式写一个请求
http://example.com/../../etc/passwd
Run Code Online (Sandbox Code Playgroud)
并且代码很乐意提供/ etc/passwd文件,但事实并非如此.这是否意味着../在Go http包或http协议本身中存在某种形式的保护,或者我只是做错了什么并且它是一个安全漏洞?
让我们从C++中的一个简单类开始:
class aClass {
bool b;
aClass(bool x){b=x;}
};
Run Code Online (Sandbox Code Playgroud)
是否可以输入2个新类型stateTrue和stateFalse,以便我这样做:
stateTrue variable;
Run Code Online (Sandbox Code Playgroud)
它会转化为:
aClass variable(true);
Run Code Online (Sandbox Code Playgroud)
?
在这个简单的例子中,如何保证这一点
a->push_back(i)
Run Code Online (Sandbox Code Playgroud)
是以线程启动的顺序发生的?所以内容将是{1,2,3}.
#include <vector>
#include <thread>
void do_stuf(int i,std::vector<int> * a)
{
//do very long stuff
a->push_back(i);
}
int main()
{
std::vector<int> tmp;
std::thread t1(do_stuf,1,&tmp);
std::thread t2(do_stuf,2,&tmp);
std::thread t3(do_stuf,3,&tmp);
t1.join();
t2.join();
t3.join();
}
Run Code Online (Sandbox Code Playgroud) 在C++中,我有这样的数据结构:
struct Data
{
int N;
double R;
char Name[20];
};
Run Code Online (Sandbox Code Playgroud)
这个数据我必须从一个客户端发送到另一个系统上的服务器(我必须发送一个数据结构数组,但我可以一个接一个地发送它).我想把它作为二进制数据发送出去,这样我就可以在另一端提取数据,把它放在同一个结构类型中.
如果(客户端和服务器)都使用相同的编译器进行编译,则sizeof(Data)和结构内的所有位填充都是相同的.但由于服务器是64位运行Linux,客户端甚至可能是32位窗口,因此数据中的数据排序可能会有所不同.
我对吗?处理这个问题的最佳方法是什么?
我有一个结构,我想要不可复制,只能移动,但因为它包含大量的POD,写移动构造函数会很长,忘记变量将很难调试.例:
struct myStruct{
int a,b,c,d;
double e,f,g,h;
std::complex<double> value1,value2;
std::unique_ptr<Calculator> calc;
myStruct(){}
myStruct(const myStruct &)=delete;
myStruct(myStruct && other);
};
Run Code Online (Sandbox Code Playgroud)
这种移动构造函数会出现什么问题:
myStruct::myStruct(myStruct && other){
std::memcpy(this,&other,sizeof(myStruct));
other.calc.release();
calc->rebind(this);
}
Run Code Online (Sandbox Code Playgroud)
我可以面对什么问题,这是明确定义的吗?