C++: Threads and Classes

This is a discussion on C++: Threads and Classes within the C++ Programming forums, part of the General Programming Boards category; Hey, I was just curious if you could make a thread out of a class function...if it is possible, what ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    10

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    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*)’

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    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

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    10

    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;
    }

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    10
    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!

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How To Initialize concurrent processes in C?
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 12:59 PM
  2. Ok so I now know how to do...
    By PCG33K in forum C++ Programming
    Replies: 15
    Last Post: 10-04-2007, 08:54 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. classes are pain
    By pizzapie in forum C++ Programming
    Replies: 5
    Last Post: 09-24-2004, 12:55 PM
  5. help with classes or threads or something :)
    By btq in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2002, 11:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21