pthreads with objects method
pthreads with objects method
我有一个小问题,在我目前的项目,因为我想使用一个对象方法时创建我的线程。我认为不将此方法声明为静态方法是不可能的。你知道吗?
public:
CModelisation (int argc, char **argv, char[]);
~CModelisation ();
void Init ();
void *RunMainLoop (void* args);
};
CModelisation.cpp
void *CModelisation::RunMainLoop (void* args)
{
glutDisplayFunc(Display);
glutIdleFunc(Display);
glutReshapeFunc(Redisplay);
glutMotionFunc(Mouse);
glutKeyboardFunc(Keyboard);
glutMainLoop();
return args;
}
主要 threads[1] = new CThread();
threads[1]->exec(Model->RunMainLoop,(void*)1);
谢谢!
我认为为任何线程方法创建包装器函数是一种常见的做法:
struct Foo {
void someMethod() {
// ... the actual method ...
}
static void* someMethodWrap(void* arg) {
((Foo*) arg)->someMethod();
return 0;
}
void callSomeMethodInOtherThread() {
pthread_create(thread, attr, someMethodWrap, this);
}
};
传递额外的参数需要更多的努力,但这是一般的想法。
幸运的是,下一个标准的std::thread
使我们的生活更容易…但这仍是未来的事
相关文章:
- 通过 get-Method 访问变量在类外不起作用
- 为什么我的 BaseClass:Method 代码编译(带有单冒号)?
- 如何在 c++ 中理解这样的代码 [request->headers().Method()->value().getStringView())]
- g++ [[noreturn]] on a virtual method
- GotW #88 中的"It doesn’t work for references that are members of objects"是什么意思?
- 错误"pure virtual method called",当此方法已被覆盖时
- 如何修复此错误? "Method 'str' could not be resolved"
- 如何在 module.reload() 之后保持 py::objects 存活
- initializer_list for heavy objects
- 使用 -march 编译会导致线程说"pure virtual method called"
- QMetaObject::invokeMethod: no such method QTextCursor::MoveO
- 为什么"no temporary std::string objects would arise"在有效的现代C++的第25项中说?
- 如何在 GCC 中将"non trivially copyable objects"传递到 varargs 函数中
- 隐藏PIMPL-Objects拥有的成员的实现
- cudaMalloc/cudaMemcpy with embedded objects/structures
- ":"(单个冒号)在"Klass:method(p)"中是什么意思?
- Opengl VAO and VBO objects
- LuaJIT 和 C++ - 调用 Table.Method() 在 loadstring/pcall 中不起作用
- 如何在源文件中定义类并将其声明在标题文件中(而不必使用`class :: method'语法定义类方法)
- pthreads with objects method