Click to See Complete Forum and Search --> : trying to make thread class
Red Squirrel
September 15th, 2007, 09:38 PM
I'm trying to make a thread class for easy implimentation when writing programs, this is my code:
namespace RSlib
{
class RSthread
{
private:
bool running; //set to true when thread is started, and set to false when stopped
int thr_id; // thread ID for the newly created thread
pthread_t p_thread; // thread's structure
public:
//returns whether or not thread is actually running
bool IsRunning()
{
return running;
}
//triggers the actual work load thread
void *TriggerWorkLoad(void* data)
{
running=true;
WorkLoad();//call actual work load function
running=false;
pthread_exit(NULL);
}
//starts the thread
virtual bool Start()
{
int dummyvar=1;
if(pthread_create(&p_thread, NULL, TriggerWorkLoad, (void*)&dummyvar)!=0)return false;
running=true;
return true;
}
//stops the thread
virtual bool Stop()
{
if(!running)return false;
running=false;
return (pthread_cancel(p_thread)==0);
}
//workload code goes here (whatever the thread is supose to do)
virtual void WorkLoad()
{
}
};
}
I get the error:
../../rsthreads.h: In member function ‘virtual bool RSlib::RSthread::Start()’:
../../rsthreads.h:57: error: argument of type ‘void* (RSlib::RSthread::)(void*)’ does not match ‘void* (*)(void*)’
Anyone know how to fix this? I used this example and it worked fine outside a class but cant seem to get it to work within a class.
http://users.actcom.co.il/~choo/lupg/tutorials/multi-thread/multi-thread.html#thread_create_stop
JVene
September 16th, 2007, 12:45 AM
Developing a thread class is SUCH a good idea. Bravo!
Ok, first, the function required for pthread_create must be a non-member function. You're providing a member function, and pthread_create is a C level API that doesn't understand member functions.
Bummer, I know.
There's a way through that.....
You're calling it dummyvar, but it's much more important to you than that.
It should be your 'this' pointer into an instance where you CAN call a member function.
The function you pass to pthread_create can't be a member function. However, it can be a function that can cast the void * parameter to your thread class, which then subsequently calls the member function.
Is that enough to go on?
Also, this function:
void *TriggerWorkLoad(void* data)
You're not returning anything, and a void * isn't a good idea in C++ anyway. You're not using data, either - so return void (nothing), and take no parameters - that makes it much cleaner.
Red Squirrel
September 16th, 2007, 01:04 PM
Yeah was reading up more and this can't be done without more complex stuff like you outlined. does C++ itself have any multithreading libraries? (for Linux, windows is just a bonus, but I want it for Linux)
What I ended up doing is just making a nicely outlined code structure and threw it in a text file and whenever I need to make a thread I just use that code. Works for me I suppose. Within the actual thread function I can do calls to classes and such anyway so thats good.
dave2k
September 17th, 2007, 07:35 AM
there is the Boost.Threads (http://www.ddj.com/cpp/184401518) library. I am not sure how much it's used though. The tutorial is over 5 years old..
Red Squirrel
September 17th, 2007, 08:40 AM
I actually looked at boost, though it looks like it requires a special process to compile and run the applications, you need to install something extra on the machine or something. I don't plan to mass release my programs, but I want to code them knowing that I can easily pop it on another machine and it runs.
dave2k
September 17th, 2007, 12:53 PM
but I want to code them knowing that I can easily pop it on another machine and it runs.Boost is designed to be platform independant.you need to install something extra on the machine or something.As you do with all libraries. Boost is no different. It's worth the time figuring out how to install on your pc. It's not really that hard, and plus you get a load of other stuff installed for free which you will find usefull in the future, for example boost::shared_ptr.
I can't think of an easier way to get platform independant threading working.
Red Squirrel
September 17th, 2007, 02:01 PM
I'd have to try, but I've just seen lot of programs that require 3rd party libraries, but the libraries wont install without going through a hierarchy of thousands of dependencies. Depends how well boost is written then. I was looking at what it can do and it does look promising.
JVene
September 18th, 2007, 12:21 AM
boost is highly reviewed, committee approved, well exercised stuff.
It's hard to knock it, but I must admit I, too, have reserve about custom or 'special' arrangements like those you not. Not all of boost requires it, I believe, but I'm not familiar enough with it to expand on the subject.
I made my own threading library years ago, and I have a few posts around here on the subject, and there's some great articles here.
Your thread class is a good start. The basic thread class represents the launch of a thread, down to the point where you have some virtual function you override to "do" something.
That's the part I'm not clear on in your post thus far. It seems you have text you'd paste and rename, so as to supply a 'run' function?
Just make the thread object, make "run" virtual, so that you derive from the thread object and provide a run override.
The next step takes a bit of a leap, but it was REALLY worth it to me.
I created a QueProcessor (which I've described around here a few times). It basically let's me stuff in function calls. Creating a QueProcessor means a thread has been launched, waiting (on an event).
When I submit a function for it to perform, the code (an the thread on which it's running) continues without pause. The thread 'wakes up' - calls that function, then goes back to sleep.
If other threads post more 'functions' for the QueProcessor to perform while it's busy, they're stored in a queue, and serviced in turn.
Personally, I wouldn't want to use threads in GUI without it.
codeguru.com
Copyright 2007 Jupitermedia Corporation All Rights Reserved.