【问题标题】:create two classes with pthread create and get access to each other in cpp使用 pthread 创建两个类并在 cpp 中相互访问
【发布时间】:2014-07-05 13:03:05
【问题描述】:
 #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();};
};

在我的头文件中提到了三个类:Controller AutomaticControl 和 Ballsearch。 现在我想创建两个线程:它们是 ballsearch.Run() 和 AutomaticControl.Run() 我在下面的代码中创建了它。 这行得通。 我在 ballsearch funktion Run() 中使用控制器对象。这会改变 x 和 y。 在我这样做之后。有另一个线程处于活动状态。 AutomaticControl Run() 函数。 它还使用带有 getX() 和 getY() 的控制器对象。 如果我在 AutmaticControl 中使用它,则没有我期望的值。应该有球搜索函数 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;
}

【问题讨论】:

  • 你无法测试它,因为它是在 Visual Studio 上编写的。我将它与树莓派一起使用。
  • 基本上,如果您有 2 个线程共享一条信息,您希望使用消息传递系统通知每个线程数据已更改,并将更新它的任务留给单个线程,或者共享对该数据的访问并使用互斥锁来避免并发访问。
  • 我倾向于选择第一种解决方案,因为关注点分离,但这实际上取决于您的设置。
  • 我发现了我的错误:我改变了这个:在 Ballsearch 中的 contr->setLocation。比我改变了这个:>>Controller * contr;>>AutomaticControl(Controller &controller){>>contr = &controller;>>}>> 在 AutomaticControl AND Ballsearch。比我改变了这个:>> Ballsearch ballsearch(*this);>>AutomaticControl automaticcontr(*this);>> 在 Controller::Run() 函数中;

标签: c++ class object pthreads


【解决方案1】:

我发现了我的错误: 我改变了这个: contr-&gt;setLocation 在 Ballsearch 中。

然后我改变了这个:

Controller * contr;
AutomaticControl(Controller &controller){
contr = &controller;
}

在 AutomaticControl 和 Ballsearch 中。

然后我改变了这个:

Ballsearch ballsearch(*this);
AutomaticControl automaticcontr(*this);

在 Controller::Run() 函数中;

Controller::Run() 函数包括 ptread_create

【讨论】:

    猜你喜欢
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多