P
Pete Becker
Axter said:Sigh.
Your examples did not use the wrapper class.
PLONK.
Axter said:Sigh.
Your examples did not use the wrapper class.
May you explain what you mean with the above, using std::vector as an example?
Uenal said:What Axter's wrapper does is that it can make accesses to the
object thread-safe, even only for the duration of a method-call (or element-wise)
on a "on-demand" basis (ie. not simply locking the whole object; though this
is possible too). I must say, a clever idea, and it works. Of course there
is a small overhead for locking/unlocking but this is minimal and unavoidable.
His method can make everything thread-safe, ie. the whole STL and
also anything else too.
I would like to congratulate him for his brilliant idea. He should be awarded
the nobel price in CS; it solves a big problem in the programming land.
What he said is IMO self-explanatory...
His method cannot make the inner-workings, ie. the implementation, of
any class used thread-safe; and this is also not necessary, since it
is the class designers (the implementers) job.
Ie. for your example: what inside vector happens is up to the designer
of it. What Axter's wrapper does is that it can make accesses to the
object thread-safe, even only for the duration of a method-call (or element-wise)
on a "on-demand" basis (ie. not simply locking the whole object; though this
is possible too). I must say, a clever idea, and it works. Of course there
is a small overhead for locking/unlocking but this is minimal and unavoidable.
His method can make everything thread-safe, ie. the whole STL and
also anything else too.
I would like to congratulate him for his brilliant idea. He should be awarded
the nobel price in CS; it solves a big problem in the programming land.
I don't know if you're serious, but lest anyone be led astray: This
"brilliant idea" is obvious to anyone skilled in the art, and it has
been applied in many places. It does not solve the fundamental problems
of writing fast, robust multi-threaded code. Beware of anyone claiming
to have found the silver bullet for multi-threading; there's fifty years
of history of silver bullets, and all of them are now badly tarnished.
Ioannis said:May you explain what you mean with the above, using std::vector as an example?
Uenal said:His method makes the use of any object in a generic way thread safe.
In my eyes this is a big step forward.
From my own experience: until now I hesitated to use many of the
STL classes because of this problem (thread-safety, concurrently
accessing/modifying objects). Now, with this method all these
problems are solved; with this method STL can safely be used in
all multithreading apps.
Pete said:I don't know if you're serious, but lest anyone be led astray: This
"brilliant idea" is obvious to anyone skilled in the art, and it has
been applied in many places.
It does not solve the fundamental problems
of writing fast, robust multi-threaded code. Beware of anyone claiming
to have found the silver bullet for multi-threading; there's fifty years
of history of silver bullets, and all of them are now badly tarnished.
His method makes the use of any object in a generic way thread safe.
In my eyes this is a big step forward.
From my own experience: until now I hesitated to use many of the
STL classes because of this problem (thread-safety, concurrently
accessing/modifying objects). Now, with this method all these
problems are solved; with this method STL can safely be used in
all multithreading apps.
If you have to perform several functions who's results depend on each
other, then you can lock access to the object by using a RefLock
variable.
For the duration of the RefLock variable, the object is locked and
only the one thread can access it.
You can then safely do multiple functions who's results depend on each
other, and do it in a thread safe way.
example:
ThreadSafeObject<std::vector<int> >::RefLock MyLockedVec
MyRefThreadSafeOjbectTestCase.MyThreadSafeVectorInstance.GetLockedObjec
t(); if (MyLockedVec->size() > 10000 && MyLockedVec->size()&1) //Due
odd numbers
{
MyLockedVec->pop_back();
For a more complex example see following Unit Test class that is able
to perform all above senerios in an thread safe way.
http://code.axter.com/ThreadSafeOjbectTestCase.h
http://code.axter.com/ThreadSafeOjbectTestCase.cpp
The above unit test has 13 threads accessing the same object, and
using the ThreadSafeObject class to allow the objects to be access in
a thread safe manner.
This OO approach is safer, and it makes application locks redundant.
Pete said:I stand by what I said: writing fast, robust multi-threaded applications
requires application-level design. No library can do it for you.
Libraries can provide tools to make implementing that design easier, but
they cannot replace it.
Uenal said:From my own experience: until now I hesitated to use many of the
STL classes because of this problem (thread-safety, concurrently
accessing/modifying objects). Now, with this method all these
problems are solved; with this method STL can safely be used in
all multithreading apps.
I stand by what I said: writing fast, robust multi-threaded applications
requires application-level design. No library can do it for you.
Libraries can provide tools to make implementing that design easier, but
they cannot replace it.
Uenal said:What he said is IMO self-explanatory...
His method cannot make the inner-workings, ie. the implementation, of
any class used thread-safe; and this is also not necessary, since it
is the class designers (the implementers) job.
Ie. for your example: what inside vector happens is up to the designer
of it. What Axter's wrapper does is that it can make accesses to the
object thread-safe, even only for the duration of a method-call (or element-wise)
on a "on-demand" basis (ie. not simply locking the whole object; though this
is possible too). I must say, a clever idea, and it works. Of course there
is a small overhead for locking/unlocking but this is minimal and unavoidable.
His method can make everything thread-safe, ie. the whole STL and
also anything else too.
I would like to congratulate him for his brilliant idea. He should be awarded
the nobel price in CS; it solves a big problem in the programming land.
Uenal said:His method makes the use of any object in a generic way thread safe.
In my eyes this is a big step forward.
From my own experience: until now I hesitated to use many of the
STL classes because of this problem (thread-safety, concurrently
accessing/modifying objects). Now, with this method all these
problems are solved; with this method STL can safely be used in
all multithreading apps.
Uenal said:I would say that application level locking mostly leads to slower
code because you are locking too much; in the worst case
the whole application.
Even if you lock a class which has say 5 vectors, you still have
locked too much. It is better to lock on a per object basis, ie. here
each vector individually, so the other threads can still use the other
vectors. Of course it heavily depends on the business logic of the application.
But with this method you have the possibility to lock the whole object
(which can contain other objects), or lock each sub-object therein individually.
Depending on the business logic one can achieve more performance
by locking as less as possible, and this can be done best by locking at
atomic object level.
C++ standard library implementation, "doesn't mean that the objectsIoannis said:What he said in the above is not self-explanatory. I did not ask about
his wrapper class, and he did not talk about it in the above.
What he said is that in the case of a compiler providing a thread-safe
Uenal said:I would say that application level locking mostly leads to slower
code because you are locking too much; in the worst case
the whole application.
However, that is not how you presented your class to be used. To quote:
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;
Note, no mention in here about a RefLock type, and you specifically
pointed out that your class was to be used like a pointer. Pointers
don't have a RefLock member....
Axter said:It's hard to explain in writing.
Check out the following link:
http://code.axter.com/TestCaseAppforThreadSafeObject_sourcecode.zip
The above link is a zip file with VC++ project source code.
I've modified the ThreadSafeObject class so that you can stub out the
synchronization code.
If you compile and run the code as-is, you'll see that it runs just
find, and does not crash.
You can then stub out the sync code by uncommented out the following
line in ThreadSafeObject.h file
//#define STUB_OUT_SYNC_CODE_
Then recompile the code with above define, and then run it.
You'll see that it crashes right away.
Even though you're using a thread-safe VC++ STL class, the application
will still crash.
This happens because the class is thread safe, but that does not mean
an instance of the class is thread safe.
To make your instance thread safe, you need to add a thread safe
wrapper around it, or you need to add application level synchronization
code.
IMHO, a thread safe wrapper class is much more OO and much safer then
using an application level code.
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.