Q:

Thred safe data structure for holding client information

I am writing an application in which the server has to keep track of connections from multiple clients. Each client is assigned a connection id which it sends in every packet for identification. I want to map the connection id to the client information. Earlier, I was using std::map but I found that it wasnt thread safe.  I need a container that can support this functionality in plain C++03. No 3rd party libraries allowed(using lab equipment). Failing this, please tell me how I can achieve this using std::map and some kind of locking
Let us call the data structure info.There are 2 threads running, (for sending & receiving respectively).  They perform the following operations on info:-
recv_thread {
  //read id
  if(id == 0) info.insert(0,clientdata);
  else {
       do stff, update info[id]
}

send_thread { 
     for each key in info:
       if (key==0) {
          delete info[0];
          info.insert(connid, clientdata);
       }
       else {
           update info[key]
           if(client taking too long) delete info[key];
       }
 }

A:

Use __sync_fetch_and_add to get the next connid and use pthread mutex to wrap your other map calls.
pthread_mutex_t mutex;
int nextConnid = 1;
...
pthread_mutex_init(&mutex, NULL);
...
recv_thread {
  //read id
  if(id == 0) 
    info.insert(__sync_fetch_and_add(&nextConnid, 1), clientdata);
  else {
       do stff, 
       pthread_mutex_lock(&mutex);
       update info[id]
       pthread_mutex_unlock(&mutex);
}

send_thread { 
     for each key in info:
           pthread_mutex_lock(&mutex);
           update info[key]
           pthread_mutex_unlock(&mutex);
           if(client taking too long) delete info[key];

 }

