-
C++: Threads and Classes
Hey, I was just curious if you could make a thread out of a class function...if it is possible, what am i doing wrong?
Code:
#ifndef USER_H
#define USER_H
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using std::cout;
class USER {
private:
int *testPTR;
int test;
pthread_t thread1;
char* msg;
int iret1;
protected:
public:
USER();
~USER();
void* run(void *ptr);
void printTest();
void start();
};
#endif
USER::USER()
{
test = 1;
testPTR = &test;
}
USER::~USER()
{
}
void USER::start()
{
iret1 = pthread_create(&thread1, NULL, run, (void*) testPTR);
pthread_join(thread1,NULL);
return;
}
void* USER::run(void *ptr)
{
cout<<"thread\n";
printTest();
}
void USER::printTest()
{
cout<<"test: "<<test;
return;
}
-
I should probably post the error i'm getting:
Code:
$g++ -lpthread test.cpp
user.h: In member function ‘void USER::start()’:
user.h:42: error: argument of type ‘void* (USER::)(void*)’ does not match ‘void* (*)(void*)’
-
ok....here's an update on the code....i've taken just a little difference approach...i'm trying to use friend classes... c/o the code
Code:
#ifndef USER_H
#define USER_H
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using std::cout;
class USER {
private:
int *testPTR;
int test;
char* msg;
protected:
public:
int iret1;
USER();
~USER();
friend void* run(void *ptr);
void printTest();
friend void start();
};
#endif
USER::USER()
{
test = 1;
testPTR = &test;
}
USER::~USER()
{
}
void start(USER* thisUser)
{
pthread_t thread1;
thisUser->iret1 = pthread_create(thread1, NULL, run, (void*) thisUser);
pthread_join(thread1,NULL);
return;
}
void *run(void *ptr)
{
USER* thisUser = (USER*) ptr;
cout<<"thread\n";
thisUser->printTest();
}
void USER::printTest()
{
cout<<"test: "<<test;
return;
}
here is the error that i'm getting now:
Code:
$ g++ -lpthread test.cpp
user.h: In function ‘void start(USER*)’:
user.h:43: error: ‘run’ was not declared in this scope
-
-
Thanks!
Hey thanks,
that is a really good site. However I think I followed what his [33.2] said, but i'm still getting an error....
Code:
#ifndef USER_H
#define USER_H
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
using std::cout;
class USER {
private:
int *testPTR;
int test;
pthread_t thread1;
char* msg;
int iret1;
protected:
friend void* accessMemFunc(void* objPtr);
public:
USER();
~USER();
void run();
void printTest();
void start();
};
#endif
USER::USER()
{
test = 1;
testPTR = &test;
}
USER::~USER()
{
}
void USER::start()
{
USER* thisUser = this;
iret1 = pthread_create(&thread1, NULL, accessMemFunc, (void*) thisUser);
pthread_join(thread1,NULL);
return;
}
void* accessMemFunc(void* objPtr)
{
USER* objHandler = (USER*) objPtr;
objHandler->run();
}
void USER::run()
{
cout<<"thread\n";
printTest();
}
void USER::printTest()
{
cout<<"test: "<<test;
return;
}
still gets the error:
Code:
$ g++ -lpthread test.cpp
user.h: In member function ‘void USER::start()’:
user.h:44: error: ‘accessMemFunc’ was not declared in this scope
btw, my test.cpp script is this:
Code:
#include <iostream>
#include "user.h"
int main ()
{
USER testUser;
testUser.start();
return 0;
}
-
Maybe if I just discussed what I was trying to do someone could point me in the right direction, because after this i'm guessing that i'm doing it wrong. Ok so, I have this kinda fun idea for a game and wanted it to be shared on a LAN (or internet) which requires a server and a bunch of clients. So how do I get a server to listen to a bunch of clients through sockets? I was gonna try to use a user class for each socket connect (aka user) and inside that user class have a threaded function that just reads info and push out info from the socket connection. I'm kinda new to both sockets and threads (atleast in C++). If it were Java I would have had an easier time. Anyway, can anyone point me to a tutorial or to something helpful. Oh, I think it's also worth noting that I'm on Linux and plan on have the game played on Linux boxes. Thanks in advance!
-
I would make special class thread that would be base class of each class that wants to run a process and then just make virtual functions.