使用pthread Create创建两个类,并在cpp中相互访问
create two classes with pthread create and get access to each other in cpp
#include <pthread.h>
class Controller{
private:
int x;
int y;
public:
void Run();
int getX(){return x;};
int getY(){return y;};
int getXSpeed(){return xSpeed;};
int getYSpeed(){return ySpeed;};
void setLocation(int x2, int y2);
};
class AutomaticControl {
private:
int lastX;
int lastY;
Controller contr;
public:
AutomaticControl(Controller controller){
contr = controller;
}
void *Run(void);
static void *Run_helper(void *context){return ((AutomaticControl *)context)->Run();};
};
class Ballsearch {
private:
Controller contr;
public:
Ballsearch(Controller controller){
contr = controller;
};
void *Run();
static void *Run_helper(void *context){return ((Ballsearch *)context)->Run();};
};
在我的头文件中提到了三个类:控制器自动控制和Ballsearch。现在我想创建两个线程:它们是ballsearch.Run()和AutomaticControl.Run()我用下面的代码创建了它。这个作品。我在ballsearch函数Run()中使用控制器对象。这改变了x和y。在我做完这个之后。有另一个线程在活动。自动控制运行()函数。它还通过getX()和getY()使用控制器对象。如果我在自动控制中使用它,就不会得到我期望的值。应该有在ballsearch函数Run()中提到的值。我怎样才能解决这个问题呢?
以下是cpp中的完整代码:#include "Header.h"
using namespace std;
using namespace cv;
Controller contr1;
Ballsearch ballsearch(contr1);
AutomaticControl automaticcontr (contr1);
int main(int argc, char *argv[])
{
Controller contrl;
contrl.Run();
return 0;
}
void Controller::Run(){
pthread_t thread1;
pthread_t thread2;
pthread_create(&thread1,NULL,&Ballsearch::Run_helper,&ballsearch); // &ballsearch
pthread_create(&thread2,NULL,&AutomaticControl::Run_helper,&automaticcontr); //&automaticcontr
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
cout << "hello"<<endl;
}
void Controller::setLocation(int x2, int y2){
x = x2;
y = y2;
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
cout << " x-Position: " << x <<" y -Position: " << y <<endl;
cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
}
void *Ballsearch::Run() {
cout << "ballsearch run" << endl;
contr.setLocation(20,30);
delay(3000);
}
void *AutomaticControl::Run() {
cout << "AutomaticControl run " << endl;
cout << "* Start Automatic Control *" << endl;
delay(1000);
lastX = contr.getX();
lastY = contr.getY();
cout << "-------------------------------------------------------------" << endl;
cout << " contr.getX() " << lastX << " contr.getY() " << lastY <<endl;
cout << "-------------------------------------------------------------" << endl;
}
我发现我的错误了:我改变了这个:在Ballsearch中的contr->setLocation
。
然后我修改了这个:
Controller * contr;
AutomaticControl(Controller &controller){
contr = &controller;
}
在自动控制和球搜索。
然后我修改了这个:
Ballsearch ballsearch(*this);
AutomaticControl automaticcontr(*this);
在Controller::Run()函数;
Controller::Run()
函数包括ptread_create
相关文章:
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 如何创建一个空的全局类并在启动时实例化它
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 无法"this"指针传递到另一个类并在 CPP 中调用该类的任何方法
- 将数据保存在.cpp上,并在 .h 上运行
- 在h文件上内联发布版本并在cpp上实现调试版本的函数
- C ++ OpenGL 如何制作着色器文件并在主 CPP 中使用它
- 将 JSON 对象作为参数在 JNI 中传递给 CPP,并在 CPP 中检索 JSON 对象的数据
- 实用程序函数不会在 .h 文件中初始化并在 main 中定义.cpp
- (编辑)如何在Windows中导出模板专业,并在CPP文件中声明源
- C++ .h 文件中转发声明结构,并在.cpp文件中定义结构
- 是否可以在标头中声明 constexpr 类并在单独的.cpp文件中定义它
- 我在 C++ 的 .h 文件中进行原型设计并在 Cpp 文件上定义它们时遇到问题
- 没有 /resetsettings 就无法运行 VS 2015 社区;在打开新项目时获取 CANTLOAD 库,并在新.cpp文件上崩溃
- 在头文件中定义void*数组,并在cpp文件中声明
- 在c++中,什么时候需要分别在“.h”文件中声明一个类,并在“.cpp”文件中提供函数实现?
- 如何在.cpp文件中定义内联函数,并在.hpp文件中定义签名?
- 使用pthread Create创建两个类,并在cpp中相互访问
- 在header中定义并在cpp中实现的c++结构体
- 在CPP文件中实现并在另一个类中使用的c++结构体