0

Mycode is trying to pass a std::map as reference to thread but it seems something is bad and result in

error: invalid conversion from ‘void* (*)(std::map<std::basic_string<char>,
       std::vector<std::basic_string<char> > >)’ to ‘void* (*)(void*)’ [-fpermissive]

I need to pass map to thread and insert the key and value of map in that thread and after successful. In main process i need to update or copy(thread map) in another object of same map i.e myMapcache

int main()
{
std::map< std::pair<std::string , std::string> , std::vector<std::string> > myMap,myMapCache;

  pthread_t threads;

  //how to pass map object in thread as reference

  int rc = pthread_create(&threads, NULL, myfunction, std::ref(myMap)); 
  if (rc)
  {
     cout << "Error:unable to create thread," << rc << endl;
     exit(-1);
   }

   // if true then update to local myMapCache

   if(update)
    {
      std::copy(myMap.begin(), myMap.end(), std::inserter(MyMapCache, myMapCache.end()) );
    } 

}


void * myfunction (std::map< std::pair<std::string , std::string> , std::vector<std::string> >& myMap)
{

  // here i will insert data in a map
  myMap[std::make_pair(key1,key2)].push_back(value);

  // if update make the flag true
    Update=true;  



}
robo
  • 23
  • 2

1 Answers1

5

pthread_create is not a template, and it does not understand C++ types. It takes a void*, which is what C libraries do in order to fake templates (kind of).

You can pass a casted pointer instead of a C++ reference wrapper object:

int rc = pthread_create(&threads, NULL, myfunction, static_cast<void*>(&myMap)); 
// ...
void* myfunction(void* arg)
{
   using T = std::map<std::pair<std::string, std::string>, std::vector<std::string>>;
   T& myMap = *static_cast<T*>(arg);

…or, better yet, use boost::thread (C++98) or std::thread (C++11 and later) to get type safety and a longer lifespan. You're not writing a C program.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • the answers to the question you linked mainly say that `std::thread` isnt working reliably on all platforms. I dont know how up to date they are, but I didnt find a mention of longer lifespan... – 463035818_is_not_an_ai Jun 27 '17 at 20:03
  • thank you but what if i try to pass two map instead of one? – robo Jun 27 '17 at 20:05
  • @tobi303: Those answers are five years old. Take out the time-sensitive complaints and absorb the general theme. As for lifespan, well if you want to give yourself cancer with C callbacks that's your call I guess :) – Lightness Races in Orbit Jun 27 '17 at 20:35
  • @robo: Well, you can't. You can only pass a pointer. You can pass a pointer to a structure that has two other pointers in it, if you like. But, again, you're really limiting yourself with this ancient C technology. – Lightness Races in Orbit Jun 27 '17 at 20:36
  • sorry I dont get the lifespan thing. What would be the direct replacement for C callbacks? – 463035818_is_not_an_ai Jun 27 '17 at 20:40
  • @tobi303: `boost::thread` and `std::thread` both accept `bind` arguments with (almost) arbitrary arguments of (almost) any type, and in the latter case you get lambdas with which you can basically do whatever the heck you like. That's clearly a _lot_ more flexible than a single opaque pointer. In this case, by dropping `pthread_create`, the OP could pass their map in just the way they're trying to do it now. – Lightness Races in Orbit Jun 27 '17 at 20:43