My problem is that I cannot figure out how to process a new thread from a class method. Here is the error ouput from compilation:
Code: Select all
$ g++ test.cpp `wx-config --cxxflags --libs` -o test -pthread
test.cpp: In member function ‘void MainWindow::OnButton(wxCommandEvent&)’:
test.cpp:32: error: argument of type ‘void* (MainWindow::)(void*)’ does not match ‘void* (*)(void*)’
Code: Select all
#include <iostream>
#include <pthread.h>
#include <wx/wx.h>
using namespace std;
class MainWindow : public wxFrame
{
public:
MainWindow(const wxString& title);
void OnButton(wxCommandEvent& event); // Method that creates the thread
void *StartThread(void *arg); // Method that starts the thread
private:
wxPanel *bg;
wxButton *button;
pthread_t thread1;
int rc; // thread's return code
};
MainWindow::MainWindow(const wxString& title) : wxFrame(NULL, -1, _T(""))
{
Center();
bg = new wxPanel(this, -1);
button = new wxButton(bg, -1, _T("Start Thread"));
button->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MainWindow::OnButton), 0, this);
}
void MainWindow::OnButton(wxCommandEvent& event)
{
cout << "Button Pressed" << endl;
rc = pthread_create(&thread1, NULL, MainWindow::StartThread, (void*)NULL); // Compile error here
if (rc)
{
cout << "Error Occurred" << endl;
exit(-1);
}
}
void *MainWindow::StartThread(void *arg)
{
cout << "This is a thread" << endl;
wxSleep(3);
pthread_exit(NULL);
}
thread handling function:
Code: Select all
void *StartThread(void* arg)
{
cout << "This is a thread" << endl;
pthread_exit(NULL);
}
Code: Select all
void MainWindow::OnButton(wxCommandEvent& event)
{
cout << "Button Pressed" << endl;
rc = pthread_create(&thread1, NULL, StartThread, (void*)NULL);
if (rc)
{
cout << "Error Occurred" << endl;
exit(-1);
}
}