使用pthread Create创建两个类,并在cpp中相互访问

create two classes with pthread create and get access to each other in cpp

本文关键字:cpp 并在 访问 两个 创建 Create pthread 使用      更新时间:2023-10-16
 #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

相关文章: