Geeting hash_map values back

C

C C++ C++

Hi All,

I am c++ beginner..

I have thread create function like this
pthread_create(&ThreadA,NULL,threadWork::requestThread,(void
*)&ResultSet); // <--- ResultSet is hasp_map with around 10 key-values
in it.


in threadWork.cc member function is defined
void * threadWork::requestThread(void *ResultSet)
{
cout << typeid(ResultSet).name() << ResultSet<<endl; // <<------ its
printing void * which is correct, how can i get my hash_map and its
values back ?
}

Please guide.

Regards,
-MA
 
E

Erik Wikström

Hi All,

I am c++ beginner..

I have thread create function like this
pthread_create(&ThreadA,NULL,threadWork::requestThread,(void
*)&ResultSet); // <--- ResultSet is hasp_map with around 10 key-values
in it.


in threadWork.cc member function is defined
void * threadWork::requestThread(void *ResultSet)
{
cout << typeid(ResultSet).name() << ResultSet<<endl; // <<------ its
printing void * which is correct, how can i get my hash_map and its
values back ?
}

You iterate over all the values in the hash-map and print them. Look at
the begin() and end() functions and read up on iterators.
 
B

Bernd Strieder

Hello,
I have thread create function like this
pthread_create(&ThreadA,NULL,threadWork::requestThread,(void
*)&ResultSet); // <--- ResultSet is hasp_map with around 10 key-values
in it.


in threadWork.cc member function is defined
void * threadWork::requestThread(void *ResultSet)
{
cout << typeid(ResultSet).name() << ResultSet<<endl; // <<------ its
printing void * which is correct, how can i get my hash_map and its
values back ?

You get it back by casting the void pointer to the right type, then you
can use all member functions of the hash_map. Perhaps you read in a C++
textbooks on the C++ casts, what advantages they have compared to your
C style cast used here. If there are more than one possibility of
classes of objects passed, then you have to use some variant data type,
or create your own wrapper class.

std::hash_set<int> * hsp = (std::hash_set<int> *)ResultSet;
if (hsp->size() > 0) std::cout said:

In your case I would recommend using a descriptive class of your own,
like WorkThreadData. This way all the casting will have to be done
once, and the actual types like your hash_set, which are pretty nasty
to handle with casts are behind a clean interface.

Bernd Strieder
 
C

C C++ C++

Hello,



You get it back by casting the void pointer to the right type, then you
can use all member functions of the hash_map. Perhaps you read in a C++
textbooks on the C++ casts, what advantages they have compared to your
C style cast used here. If there are more than one possibility of
classes of objects passed, then you have to use some variant data type,
or create your own wrapper class.

std::hash_set<int> * hsp = (std::hash_set<int> *)ResultSet;


In your case I would recommend using a descriptive class of your own,
like WorkThreadData. This way all the casting will have to be done
once, and the actual types like your hash_set, which are pretty nasty
to handle with casts are behind a clean interface.

Bernd Strieder

Thanks dude it worked well for me :)
Regards,
-MA
 
J

James Kanze

Several comments...
I have thread create function like this
pthread_create(&ThreadA,NULL,threadWork::requestThread,(void
*)&ResultSet); // <--- ResultSet is hasp_map with around 10 key-values
in it.

Is "threadWork" a namespace or a class? The third argument to
pthread_create must be a pointer to an `extern "C"'
function---member functions (even static) can't be `extern "C"',
and there's really no point in putting an `extern "C"' in a
namespace.
in threadWork.cc member function is defined
void * threadWork::requestThread(void *ResultSet)
{
cout << typeid(ResultSet).name() << ResultSet<<endl; // <<------ its
printing void * which is correct, how can i get my hash_map and its
values back ?
}

Use static_cast. With hash_map, there should be no problem. In
general, however, do be careful that you go through the right
types---something like:

extern "C" void*
threadFunction( void* p )
{
Base* args = static_cast< Base* >( p ) ;
// ...
}

// ...

Derived args ;
pthread_create( ..., &args ) ;

has undefined behavior. (The call to pthread_create should be:
pthread_create( ..., static_cast< Base* >( &args ) ) ;
And watch out for potential lifetime of object issues.)
 
C

C C++ C++

Several comments...


Is "threadWork" a namespace or a class? The third argument to class
pthread_create must be a pointer to an `extern "C"'
function---member functions (even static) can't be `extern "C"',
and there's really no point in putting an `extern "C"' in a
namespace.


Use static_cast. With hash_map, there should be no problem. In
general, however, do be careful that you go through the right
types---something like:

extern "C" void*
threadFunction( void* p )
{
Base* args = static_cast< Base* >( p ) ;
// ...
}

// ...

Derived args ;
pthread_create( ..., &args ) ;

has undefined behavior. (The call to pthread_create should be:
pthread_create( ..., static_cast< Base* >( &args ) ) ;
fourth agument should be void *, how can you pass above one ?
 
J

James Kanze

On Jan 22, 6:35 pm, James Kanze <[email protected]> wrote:

[...]
fourth agument should be void *, how can you pass above one ?

A pointer to object type converts implicitly to a void*; you
don't need an explicit conversion.

The important thing about void*, here, is that the *only* thing
you can really do with it, legally, is convert it back to the
original type. If you convert a Derived* (here, &args) to
void*, and then convert the void* to a Base* in threadFunction,
you have undefined behavior. In the simplest cases, it will
probably work, but throw in a bit of multiple inheritance or
virtual inheritance, and it's almost certainly not going to
work.

Whence the static_cast to Base*, before the implicit conversion
to void*. (The implicit conversion has the semantics of a
static_cast. And no, static_cast is not transitive:
static_cast< void* >( &aDerived ) is *not* the same as
static_cast< void* >( static_cast< Base* >( &aDerived ) ).)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top