I have a map in my program to hold the pthreads created by the pthread_create method (that requires a place to put this pthread), associated with a relevant thread ID.
Is there a problem of erasing the pthread from the map in the last command of the pthread's function?

- 537
- 4
- 9
- 19
4 Answers
As you said, you are holding thread ID. It's just a number. Nothing more.
Erasing element (number) can't do any harm to your program.
Edit: However you should check, that erasing element in std::map
is done synchronized. Don't forget STL containers can be not thread safe. See this question for more info.
Edit2: To ensure, that you haven't problems with synchronization do following :
pthread_mutex_t mut; //global variable
pthread_mutex_init(&mut,0); //initialize mutex before calling pthread_create()
//and use mutex to prevent synchronization problems in the end of .
pthread_mutex_lock(&mut);
my_map.erase(key);
pthread_mutex_unlock(&mut);
-
I'm holding a map
. I just want to make sure what you say: if i erase the pthread_t object during my pthread running, nothing bad will happen? – Zach Apr 05 '11 at 15:40 -
@Andrew when you are calling `pthread_create()` operating system returns you your threads "ID" ( a unique identifier) in pthread_t argument, which identify your thread, nothing more. So erasing it can't do any harm. – UmmaGumma Apr 05 '11 at 15:53
-
@Andrew see Edit2 for synchronization. – UmmaGumma Apr 05 '11 at 15:59
I agree with the answer provided by Ashot Martirosyan. I just want to add one other point.
If the threads are created as joinable, then your application will need to call pthread_join()
; otherwise you will leak memory. If the map is the only place where you record the thread IDs, then you won't be able to join with the threads if each thread has removed its thread ID from the map just before it died.

- 2,126
- 14
- 21
-
question sais "erasing the pthread from the map in the **last** command of the pthread's function". This means, that after erasing element from map thread ends. So no need to call `pthread_join` after erasing element. – UmmaGumma Apr 05 '11 at 15:31
-
@Ashot, you seem to think that `pthread_join()` just waits for a thread to terminate. That is incorrect. `pthread_join()` waits for a thread to terminate (if the thread has not already done so) _and then_ it frees up some resources associated with that now-terminated thread. Because of this, an application _must_ call `pthread_join()` for a joinable thread; otherwise, there will be a memory leak. Removing a thread ID from a `std::map` is not a substitute for calling `pthread_join()`. – Ciaran McHale Apr 07 '11 at 12:06
You should either pthread_join
or pthread_detach
or create a deteached thread, otherwise you'll get an error from pthread_create
sometime. For all joinable threads OS reserves some amount of memory to store the thread return value. Total amount of memory reserved for such purpose is limited and can be less that you expect, so detatch all threads you are not going to join.

- 1,389
- 9
- 14
You can erase the data whenever you want. However there could be race conditions in your program if threads access this map. if thread A is exiting but gets swapped out before it erases its data, thread B might see thread A's data and think that thread A is still a viable thread.

- 5,184
- 5
- 34
- 58