相关疑难解决方法(0)

一个类的pthread函数

假设我有一个类如

class c { 
    // ...
    void *print(void *){ cout << "Hello"; }
}
Run Code Online (Sandbox Code Playgroud)

然后我有一个c的向量

vector<c> classes; pthread_t t1;
classes.push_back(c());
classes.push_back(c());
Run Code Online (Sandbox Code Playgroud)

现在,我想创建一个线程 c.print();

以下是给我以下问题: pthread_create(&t1, NULL, &c[0].print, NULL);

错误输出:无法将'void*(tree_item :: )(void)'转换为'void*()(void)'以将参数'3'转换为'int pthread_create(pthread_t*,const pthread_attr_t*,void*()(void),void*)'

c++ pthreads

77
推荐指数
3
解决办法
10万
查看次数

将线程作为C++类的成员启动的最佳方法是什么?

我想知道启动一个C++类成员的pthread 的最佳方法是什么?我自己的方法作为答案......

c++ pthreads

16
推荐指数
3
解决办法
2万
查看次数

Pthread锁定

我已经像这样创建了MutexCondition类

/*MutexCondtion.h file*/
#ifndef MUTEXCONDITION_H_
#define MUTEXCONDITION_H_

#include <pthread.h>
#include <stdio.h>

class MutexCondition {

private:
    bool init();
    bool destroy();

protected:
    pthread_mutex_t m_mut;
    pthread_cond_t m_con;

public:
    MutexCondition(){
        init();
    }
    virtual ~MutexCondition(){
        destroy();
    }

    bool lock();
    bool unLock();
    bool wait();
    bool signal();

};
#endif /* MUTEXCONDITION_H_ */
Run Code Online (Sandbox Code Playgroud)

MutexCondtion.cpp文件

#include "MutexCondition.h"

bool MutexCondition::init(){
    printf("MutexCondition::init called\n");
    pthread_mutex_init(&m_mut, NULL);
    pthread_cond_init(&m_con, NULL);
    return true;
}

bool MutexCondition::destroy(){
    pthread_mutex_destroy(&m_mut);
    pthread_cond_destroy(&m_con);
    return true;
}

bool MutexCondition::lock(){
    pthread_mutex_lock(&m_mut);
    return true;
}

bool MutexCondition::unLock(){
    pthread_mutex_unlock(&m_mut);
    return true;
}

bool MutexCondition::wait(){ …
Run Code Online (Sandbox Code Playgroud)

c++ pthreads

4
推荐指数
1
解决办法
2674
查看次数

虚方法和这个指针

我刚刚开始学习C++,我正在尝试使Thread类具有Java Thread类的基本功能.我正在尝试做的是创建一个你继承的类,编写一个Run方法(在基类中是纯虚拟的)创建一个子类的对象,在它上面调用start方法并且你有线程.

问题是,在我使用C++的方式中,调度没有正确完成 - 就像Run函数不是虚函数一样,调用了基类的Run方法.

这是标题的代码

#ifndef _THREAD_H_
#define _THREAD_H_

#include <pthread.h>

class Thread {
 public:
  Thread();

  void Start();

  ~Thread();

 protected:
  virtual void Run() = 0;

 private:
  static void *RunWrapper(void *);

  pthread_t thread;
};

#endif
Run Code Online (Sandbox Code Playgroud)

实施

#include "thread.h"

#include <pthread.h>

Thread::Thread() {
}

void Thread::Start() {
  pthread_create(&thread, NULL, Thread::RunWrapper, (void *) this);
}

void *Thread::RunWrapper(void *arg) {
  Thread *t = (Thread *) arg;
  t->Run();
  return arg;
}

Thread::~Thread() {
  pthread_join(thread, NULL);
}
Run Code Online (Sandbox Code Playgroud)

而实际上试图做某事的文件

#include <iostream>

#include "thread.h"

class MyThread : …
Run Code Online (Sandbox Code Playgroud)

c++ linux pthreads

1
推荐指数
1
解决办法
1666
查看次数

C++ pthread成员函数

可能重复:
来自类的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*))&gtk_functor::_threaded_run, static_cast<void*>(&win));
    }
    else
    {
        Gtk::Main::run(win);
    }
}
Run Code Online (Sandbox Code Playgroud)

这个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]
Run Code Online (Sandbox Code Playgroud)

显然代码不能正常工作,我得到sementation faultif (threaded)提升.

我知道它与演员,但我不知道将成员函数传递给pthread_create的正确形式.有什么建议?

c++ g++ pthreads member-functions

1
推荐指数
2
解决办法
8416
查看次数

在成员函数中调用pthread_create?

我创建了一个包含声明的widget.h文件pthread_function,我想destroyWidget在widget.cpp中该类Widget 的成员函数中调用它.但总是显示错误.我将展示.cpp和.h文件.

widget.h文件


class Widget
{
public:
Widget();
void createWidget(int x,int y,int w,int h);
void showWidget();
int wid;
pthread_t thread;
int *incomingval,id;
void join();
Window win;
XEvent evt;
private:
void* destroyWidget(void* ptr);
Display *disp;
int screenNumber;
unsigned long white;
unsigned long black;
long eventMask;
GC gc;
int tbit;
int *incoming,val;
};

Run Code Online (Sandbox Code Playgroud)

现在是widget.cpp


Widget::Widget()
{
disp=XOpenDisplay( NULL );
screenNumber=DefaultScreen(disp);
white=WhitePixel(disp,screenNumber);
black=BlackPixel(disp,screenNumber);
eventMask=StructureNotifyMask;
tbit=0;
}

void Widget::createWidget(int x,int y,int w,int h)
{
wid=w;
win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),x,y,w,h,1,white,black);
}

void Widget::showWidget() …
Run Code Online (Sandbox Code Playgroud)

c++ x11 pthreads

0
推荐指数
1
解决办法
6292
查看次数

pthread_create在类的成员中

假设我有以下课程:

*.h:

class MyClass{
    void caller();
    int threadProcuder(void *args);
};
Run Code Online (Sandbox Code Playgroud)

*cpp:

void MyClass::caller()
{
    pthread_t i;
    pthread_create(&i,NULL,(void*(*)(void*))&MyClass::threadProcedure,(void*)this);
}

int MyClass::threadProcedure(void *args) 
{
    cout << "body of thread" << endl;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,线程没有运行.

c++ oop class pthreads this

0
推荐指数
1
解决办法
309
查看次数

标签 统计

c++ ×7

pthreads ×7

class ×1

g++ ×1

linux ×1

member-functions ×1

oop ×1

this ×1

x11 ×1