P
Philip V Pham
These questions apply to std vector, map, and cout:
I am uncertain of the thread safety for reading/writing
for std templates. I know if all threads are reading
concurrently, it is thread safe. However, I have this situation:
Case 1: map
thread 1
---------
someMapOb [key] = value //inserting new pair
thread 2
---------
interator pos;
while (1==1)
{
for (pos = someMapOb.begin(); pos != someMapOb.end(), pos++ )
{
someValueOb = pos->second;
someValueOb.foo ();
}
sleep (1)
{
Is safe for case 1 if I do that? I don't care
if I miss a new inserted object in thread 2.
If a new object inserted into the map, will the
iterator be invalidated?
The reason that I omit a mutex is to maximize performance.
---------------
Case 2: queue
thread 1
--------
someQueue.push ( value );
thread 2
---------
while ( ! someQueue.isEmpty ()
{
// display a status indicator
// NOte, I don't pop or push the Q here
}
Is safe to call isEmpty() while the Q is being
pushed?
--------------------------
Case 3: cout
thread 1
--------
cout << "Thank you in advance for your help!" << endl;
thread 2
---------
cout << "Display....display ....." << endl;
I know the screen output might be messed up,
but would this concurrent cout cause a buffer overfloat
crash?
Thank you vey much for your help!
I look forward to hear from your solutions.
I am uncertain of the thread safety for reading/writing
for std templates. I know if all threads are reading
concurrently, it is thread safe. However, I have this situation:
Case 1: map
thread 1
---------
someMapOb [key] = value //inserting new pair
thread 2
---------
interator pos;
while (1==1)
{
for (pos = someMapOb.begin(); pos != someMapOb.end(), pos++ )
{
someValueOb = pos->second;
someValueOb.foo ();
}
sleep (1)
{
Is safe for case 1 if I do that? I don't care
if I miss a new inserted object in thread 2.
If a new object inserted into the map, will the
iterator be invalidated?
The reason that I omit a mutex is to maximize performance.
---------------
Case 2: queue
thread 1
--------
someQueue.push ( value );
thread 2
---------
while ( ! someQueue.isEmpty ()
{
// display a status indicator
// NOte, I don't pop or push the Q here
}
Is safe to call isEmpty() while the Q is being
pushed?
--------------------------
Case 3: cout
thread 1
--------
cout << "Thank you in advance for your help!" << endl;
thread 2
---------
cout << "Display....display ....." << endl;
I know the screen output might be messed up,
but would this concurrent cout cause a buffer overfloat
crash?
Thank you vey much for your help!
I look forward to hear from your solutions.