Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Implement Thread Class in C++. Please Help

Status
Not open for further replies.

vasudev123

Programmer
Oct 11, 2005
8
0
0
IN
Hi All,

I am working on Solaris 5.8 Machine. I need to build c++ thread class which acts as a wrapper around
posix thread API's.

Please suggest me how to declare and implement Threads class in C++.

Thanks in Advance.

Thanks & Regards,
vasu
 
To start with, have a look at all the pthread routines: they all of the form pthread_xxx.

Start with pthread_create - that is basically the constructor. The pthread_t will be the handle that you keep in a member variable. All the other methods are just the xxx bits. The destructor will either call pthread_join or pthread_exit.

Try that for starters.
 
I think Boost already has a set of thread classes. You might want to see if they do what you want.
 
Rogue Wave also had a set of C++ wrappers for threads. I think it was called threads++. I found it cumbersome to use because it pulled in an enormous number of headers. I don't know what the Boost version is like but if you do implement something, do not include <pthread.h> in the class header. You can save all the local info using the PIMPL idiom.

The problem with someone else implementation comes when you want to use something new that is not implemented and you can't get at the necessary info because it is all hidden: that is unless they give you the source as well. Newer features like spinlocks may or may not be in.

Sometimes, only the subset that the implementor is familiar with are implemented. The stuff that the implementor cannot see a use for is not implemented and if you want to use it, you're really stuck. I had that with Rogue Wave and since the whole product was already based around Rogue Wave, I had to code around it but it was a pain and a half. It is swings and roundabouts - you may be better off with someone else's implementation, you may be better off with your own. Only you can decide that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top