简体   繁体   English

pthread看不到实例变量作为参数传递

[英]pthread does not see instance variable passed as argument

I have a class in C++ that uses boost python. 我在C ++中有一个使用boost python的类。 I am trying to run python code in a thread from C++ using pthread. 我正在尝试使用pthread在C ++的线程中运行python代码。 The problem is that the code below isn't producing any output. 问题在于下面的代码没有产生任何输出。 I was expecting an output John DOE in stdout. 我期待在stdout中输出John DOE It seems that &this->instance doesn't carry the values that are being set inside the object. 看来&this->instance不包含在对象内部设置的值。 How can I pass current object or its instance variable to the pthread_create so that pthread can see what is being passed? 如何将当前对象或其实例变量传递给pthread_create以便pthread可以查看正在传递的内容?

Python:

class A: 
  def __init__(self, name): 
      self.name = name

  def printName(self, lastName): 
      print self.name + " " + lastName

C++:

#include <boost/python.hpp>
#include <string.h>
#include <pthread.h>

using namespace std;
using namespace boost::python;

class B {
    public:
        object instance;
        B();
        void setupPython();
        static void *runPython(void *);
};

B::B() {
    Py_Initialize();
}

void B::setupPython() {
    pthread_t t1;
    try {
        object a = import("A");
        instance = a.attr("A")("John");
        pthread_create(&t1, NULL, runPython, &this->instance); // THIS IS PROBLEM
    }
    catch(error_already_set const &) {
        PyErr_Print();
    }
}

void *B::runPython(void *instance) {
    ((object *)instance)->attr("printName")("DOE");
}

int main() {
    B b;
    b.setupPython();
}

Thank you. 谢谢。

The problem is: 问题是:

int main() {
    B b;
    b.setupPython(); // You create a thread here
    // But here, b is destroyed when it's scope ends
}

The code in your thread is not guaranteed to run before b is freed. 不能保证线程中的代码在释放b之前可以运行。

Try allocating b on the heap and check if it works: 尝试在堆上分配b并检查其是否有效:

int main() {
    B* b = new B();
    b->setupPython();
    // also, you should add a call to pthread_join
    // here to wait for your thread to finish execution.
    // For example, changing setupPython() to return the
    // pthread_t handle it creates, and calling pthread_join on it.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将在构造函数中作为参数传递的函数指针分配给私有实例变量时出错 - error assigning a function pointer passed as an argument in the constructor to a private instance variable 是否需要将互斥锁锁定才能将参数传递给pthread? - Does it require mutex locking for arguments to be passed to pthread? 局部变量和变量作为参数传递 - Local Variable and Variable Passed as Argument Pthread_create似乎刷新了传递给输入函数的参数 - Pthread_create seems to flush the argument passed to the input function 为什么 for_each 需要一个实例作为参数传递,而无序映射的哈希一元函数在 C++ 中没有? - Why for_each requires an instance to be passed as an argument while the hash unary function for unordered map does not in C++? 当分配的带有指针的字符串的结构体作为pthread的参数传递时,指针会丢失 - Pointer gets lost when allocated struct with pointer on allocated string gets passed as argument for pthread 为什么我不能通过传入的类对象参数杀死pthread - Why can't I kill pthread via class object argument passed in 变量模板可以作为模板模板参数传递吗? - Can a variable template be passed as a template template argument? Pthread参数传递 - Pthread Argument Passing 使用define参数进行Pthread - Pthread with define argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM