0
私はboost pythonを使用するC++のクラスを持っています。私はpthreadを使ってC++からのスレッドでPythonコードを実行しようとしています。問題は、以下のコードが出力を生成していないことです。私はstdoutの出力John DOE
を期待していました。 &this->instance
は、オブジェクト内で設定されている値を保持していないようです。どのようにpthread
が渡されているのを見ることができるように、現在のオブジェクトまたはそのインスタンス変数を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();
}
ありがとうございます。
*ヒープ上にBを割り当ててみて、それが動作するかどうかを確認:*おそらくまだ動作しません - とき 'メイン()'を返します、プログラムは終了する。 –
@AndrewHenle正しい答えを編集しました。 – Steeve
@Steeve答えをありがとう。できます。 – pseudo