本文介绍了C ++ pthread成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不能编译因为 pthread_create
行:
void* gtk_functor::_threaded_run(void* win)
{
Gtk::Window* w = static_cast<Gtk::Window*>(win);
Gtk::Main::run(*w);
delete w;
}
void gtk_functor::operator ()(Gtk::Window& win, bool threaded)
{
if (threaded)
{
pthread_t t_num;
pthread_create(&t_num, NULL, (void* (*)(void*))>k_functor::_threaded_run, static_cast<void*>(&win));
}
else
{
Gtk::Main::run(win);
}
}
此gcc行:
g ++ -o main'pkg-config --cflags --libs sqlite3 gtkmm-3.0'-lpthread main.cpp
使用此输出结束编译:
code/ui.cpp: In member function 'void ui::gtk_functor::operator()(Gtk::Window&, bool)':
code/ui.cpp:45:65: warning: converting from 'void* (ui::gtk_functor::*)(void*)' to 'void* (*)(void*)' [-Wpmf-conversions]
b $ b
显然代码不能正常工作,当 if(threaded) sementation fault
and apparently the code doesn't work correctly, I get sementation fault
when the if (threaded)
is raised.
我知道它与转换,但我不知道正确的形式传递一个成员函数到pthread_create。任何建议?
I know its with the cast, but I don't know the correct form of passing a member function into pthread_create. Any suggestions?
推荐答案
尝试 _threaded_run
static。在标题中:
Try making _threaded_run
static. In the header:
private:
static void* _threaded_run(void*);
在实现中:
void* gtk_functor::_threaded_run(void* win) {
Gtk::Window* w = static_cast<Gtk::Window*>(win);
Gtk::Main::run(*w);
delete w;
}
然后在创建线程时:
pthread_create(&t_num, NULL, >k_functor::_threaded_run, static_cast<void*>(&win));
这篇关于C ++ pthread成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!