M
mihai
What does the standard say about those two? Is any assurance that the
use of STL is thread safe?
Have a nice day,
Mihai.
use of STL is thread safe?
Have a nice day,
Mihai.
mihai said:What does the standard say about those two? Is any assurance that the
use of STL is thread safe?
mihai said:What does the standard say about those two? Is any assurance that the
use of STL is thread safe?
We are sure that STL is NOT thread safe
mihai said:What does the standard say about those two? Is any assurance that the
use of STL is thread safe?
mihai said:What does the standard say about those two? Is any assurance that the
use of STL is thread safe?
Have a nice day,
Mihai.
mihai said:What does the standard say about those two? Is any assurance that the
use of STL is thread safe?
Have a nice day,
Mihai.
Axter said:In general, STL objects are not thread safe.
However, it's not that hard to make them thread safe.
Check out the following link:
http://code.axter.com/ThreadSafeObject.h
The above link contains a wrapper class that can be use in Win32 code
to make STL objects thread safe.
Once the wrapper class is declared, it acts like a pointer.
Example Usage
ThreadSafeObject<vector<int> > MyThreadSafeVectorInt(new vector<int>);
MyThreadSafeVectorInt.GetLockedObject()->push_back(123);
cout << MyThreadSafeVectorInt.GetLockedObject()->at(0) << endl;
For UNIX/Linux/OS2 it would not be to hard to modify the
ThreadSafeObject wrapper class to use POSIX functions instead of Win32
API functions.
Also see following link:
http://code.axter.com/ThreadSafeViaMutexObject.h
In general, STL objects are not thread safe.
However, it's not that hard to make them thread safe.
Check out the following link:
http://code.axter.com/ThreadSafeObject.h
The above link contains a wrapper class that can be use in Win32 code
to make STL objects thread safe.
Once the wrapper class is declared, it acts like a pointer.
Example Usage
ThreadSafeObject<vector<int> > MyThreadSafeVectorInt(new vector<int>);
MyThreadSafeVectorInt.GetLockedObject()->push_back(123);
cout << MyThreadSafeVectorInt.GetLockedObject()->at(0) << endl;
Peter said:First of all, I do not see how your scheme is protecting you against a
template library that is not multithreaded. You will have problems if one of
the underlying collectionclasses access some shared variable. Secondly,
you're protecting the data at the wrong level: in general, it is not an
individual "push" operation that needs to be protected, but a larger
"logical" operation. In this common case, your protection will be at worst
useless while it in the best case only serves to eat cpu-cycles.
/Peter
vector?Uenal said:This looks interessting, but have you also stress tested this using
multiple threads such like say 10 threads wanting to modify this
Axter said:Try running, and see if you can break the code.
Peter said:First of all, I do not see how your scheme is protecting you against a
template library that is not multithreaded. You will have problems if one of
the underlying collectionclasses access some shared variable. Secondly,
you're protecting the data at the wrong level: in general, it is not an
individual "push" operation that needs to be protected, but a larger
"logical" operation. In this common case, your protection will be at worst
useless while it in the best case only serves to eat cpu-cycles.
/Peter
Pete said:while (!vec.empty())
vec.pop_back();
If the code doesn't lock around the entire loop it will break. Sooner or
later you'll get a thread switch right after the call to empty returns
false, and the vector may, in fact, be empty for the call to pop_back.
As Peter said, locking containers doesn't make an application
thread-safe. And once you've added the high-level protection that's
needed to make the application thread-safe, locks in containers will
usually be redundant.
Axter said:I'm not talking about a vector
by itself. You're above code is not using the wrapper class which is
what I was referring to in my previous post.
Please read the entire posted thread. I'm not talking about a vector
by itself. You're above code is not using the wrapper class which is
what I was referring to in my previous post.
I'm talking about an object wrapped in the thread safe wrapper class.
See following link:
http://code.axter.com/ThreadSafeObject.h
Pete said:The point is that with or without the wrapper class this use of the
container is not thread-safe. You need an application-level policy and
broader locking. Thread-safe containers are not sufficient, and are
usually redundant.
Axter said:Can you prove that point with some type of example code that would
break?
Pete said:The point is that with or without the wrapper class this use of the
container is not thread-safe. You need an application-level policy and
broader locking. Thread-safe containers are not sufficient, and are
usually redundant.
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.