My error:
error: cannot convert 'MainWindow::producerThreadFunction' from type 'void* (MainWindow::)(void*)' to type 'void* (*)(void*)'
if (pthread_create (&producer, NULL, producerThreadFunction, NULL))
^
Header file:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QApplication>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <iostream>
#include <QDebug>
class MainWindow : public QMainWindow
{
Q_OBJECT
pthread_mutex_t mutexVariable = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t conditionVariable = PTHREAD_COND_INITIALIZER;
QList <int> queueLIFO;
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
// This function is run by the thread `Producer`.
void *producerThreadFunction (void *arg);
// This function is run by the thread `Consumer`.
void *consumerThreadFunction (void *arg);
int start ();
};
#endif // MAINWINDOW_H
Source file: (function where the error occurs)
int MainWindow :: start()
{
pthread_t producer;
pthread_t consumer;
if (pthread_create (&producer, NULL, producerThreadFunction, NULL))
{
fprintf (stderr, "Error creating thread Producer\n");
return 1;
}
if (pthread_create (&consumer, NULL, consumerThreadFunction, NULL))
{
fprintf (stderr, "Error creating thread Consumer\n");
return 1;
}
if (pthread_join (producer, NULL))
{
fprintf (stderr, "Error joining thread Producer\n");
return 2;
}
if (pthread_join (consumer, NULL))
{
fprintf (stderr, "Error joining thread Consumer\n");
return 2;
}
return 0;
}
According to this thread, the solution is to make the producerThreadFunction
static.
Why should the thread function of a class be made static to be accessible in the same class?
That function is the member function of that class. Why cannot I access it directly?