Well...take your pick between the methods. They all amount to the same I think. Depends on what you need. I'll show each method and give the pros and cons as I see it. By the way, I'm just rattling this code off the top of my head, so some might have minor compile errors. I changed the prototypes for the functions...I think that is the right prototype for pthread functions. Definately need to pass a void* param as data to the thread. Pretty sure that it returns an int. Try "man pthread_create" to double check the prototype.
Also, you'll need to sort out the issues with prototypes here for example 4. The compiler is going to complain because MainLoop references ControlManager before ControlManager is defined. If you don't know how to fix this with header files, then just skip example 4.
Method 1: Make the member functions static.
Pros: Easy to implement.
Cons: You don't have an instance of the object to work with. For example purposes, I added a member data "int member_data" for example purposes.
Code:
#include <pthread.h>
#include <unix.h>
class ControlManager
{
public:
int member_data;
static int ReadMessage(void* data); // This function need associated as a
//thread
static int WriteMessage(void* data); // also a thread
static int MainLoop(void* data); // also a thread
void SpawnThreads();
private:
pthread_t thread1, thread2, thread3;
}
void ControlManager::SpawnThreads()
{
pthread_create(thread1, NULL, ReadMessage, NULL);
pthread_create(thread2, NULL, WriteMessage, NULL);
pthread_create(thread3, NULL, MainLoop, NULL);
}
int ControlManager::ReadMessage(void* data)
{
// data can be anything you want, passed in from the pthread_create call.
// We don't use it here
// This won't work!! You don't really have a this pointer, and that is the real drawback
// this->member_data = 10;
}
Method 2: Non-member functions
Pros: Easy like first solution
Cons: Again, you don't have a this pointer
Code:
#include <pthread.h>
#include <unix.h>
/* Non-member functions */
int ReadMessage(void* data)
{
}
int WriteData(void* data)
{
}
int MainLoop(void* data)
{
}
class ControlManager
{
public:
int member_data;
void SpawnThreads();
private:
pthread_t thread1, thread2, thread3;
}
void ControlManager::SpawnThreads()
{
pthread_create(thread1, NULL, ReadMessage, NULL);
pthread_create(thread2, NULL, WriteMessage, NULL);
pthread_create(thread3, NULL, MainLoop, NULL);
}
Method 3: Same as method 2, but pass pointer to object as parameter to pthread_create
Pros: This poniter available
Cons: No idea
Code:
#include <pthread.h>
#include <unix.h>
/* Prototypes */
int ReadData(void* data);
int WriteData(void* data);
int MainLoop(void* data);
class ControlManager
{
public:
int member_data;
void SpawnThreads();
private:
pthread_t thread1, thread2, thread3;
}
void ControlManager::SpawnThreads()
{
pthread_create(thread1, NULL, ReadMessage, this);
pthread_create(thread2, NULL, WriteMessage, this);
pthread_create(thread3, NULL, MainLoop, this);
}
/* Non-member functions */
int ReadMessage(void* data)
{
ControlManager* pThis = (ControlManager *)data;
// works now
pThis->member_data = 10;
}
int WriteData(void* data)
{
ControlManager* pThis = (ControlManager *)data;
// works now
pThis->member_data = 10;
}
int MainLoop(void* data)
{
ControlManager* pThis = (ControlManager *)data;
// works now
pThis->member_data = 10;
}
Fourth Method: Use functors
Pros: This pointer available, your friends think you're cool
Cons: More compilcated; someone might ask you to explain it
Code:
#include <pthread.h>
#include <unix.h>
/* Functors: */
class ReadData {
private:
ControlManager* pThis;
public:
ReadData(ControlManager* p) { pThis = p; }
int operator()(void* data)
{
pThis->member_data = 100;
}
};
class WriteData {
private:
ControlManager* pThis;
public:
WriteData(ControlManager* p) { pThis = p; }
int operator()(void* data)
{
pThis->member_data = 100;
}
};
class MainLoop {
private:
ControlManager* pThis;
public:
MainLoop(ControlManager* p) { pThis = p; }
int operator()(void* data)
{
pThis->member_data = 100;
}
};
class ControlManager
{
public:
int member_data;
void SpawnThreads();
private:
pthread_t thread1, thread2, thread3;
}
void ControlManager::SpawnThreads()
{
pthread_create(thread1, NULL, ReadMessage(this), NULL);
pthread_create(thread2, NULL, WriteMessage(this),NULL);
pthread_create(thread3, NULL, MainLoop(this), NULL);
}