|
-
January 27th, 2005, 05:46 AM
#6
Re: NULL pointer and void pointer?
void * is also commonly used in standard APIs. For example, a thread function takes a void * parameter, a generic way of being able to pass it data.
This is how C APIs are often done, but in C++ APIs it is better to define a base class and expect a pointer to the base class.
Threads, for example, can be done like this:
Code:
// in header:
class ThreadClass
{
public:
virtual ThreadClass* threadFunc() = 0;
};
thread_t create_thread( ThreadClass * pThreadClass );
/// in .cxx file
namespace
{
extern "C" void * the_one_thread_function( void * param )
{
ThreadClass * pThreadClass( reinterpret_cast< ThreadClass * >(param ) );
return pThreadClass->threadFunc();
}
}
thread_t create_thread( ThreadClass * pThreadClass )
{
thread_t result;
pthread_create( &result, 0, the_one_thread_function, 0 );
return result;
}
This is a very loose description to show how one might start writing a C++ wrapper to pthread_create which removes the need for void *. (The return value of the thread function is also void *, and you might want to use a different class, a ThreadResult class).
(If you want to implement this in full, then it is probably a good idea to take an optional thread-attribute parameter (2nd parameter to pthread_create). You'd also need some kind of collection (somewhere to store the ThreadFunc object and subsequently be able to delete it), thus a thread pool ).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|