Re: What's the connection between objects and threads?
On May 21, 3:20 am, c...@mailvault.com wrote:
On May 20, 5:34 pm, Szabolcs Ferenczi <szabolcs.feren...@gmail.com>
wrote:
[...]
3) In object-based or higher level object-oriented languages
the unit of shared resource is obviously an object. So the
association of the Critical Region and the critical resource
is naturally merged in an object. That was one of the
failure of Java that they were not brave enough to mark the
class as the shared resource. In a decent concurrent
object-oriented language you could specify a keyword for the
class to mark a shared object (and not for the methods).
E.g.:
synchronized class M {
int m;
public:
void foo() {
// do something with `m' in mutual exclusion
}
};
You must mark the class as a whole and not the individual
methods of it. Then the compiler can again help you to make
correct concurrent programs. This is again not possible with
library-based solution.
I'm not sure marking the class like that is a good idea.
Would you mark vector as synchronized? If yes, do you expect
compilers to figure out that only one thread is able to access
some of your vector objects at any point in time and disable
the synchronization? I don't want to pay for extra
synchronization when it isn't needed.
That's not really the point (although it certainly would be in
some applications). The point is that this idea was put forward
many, many years ago; it works well when you're dealing with
simple objects, like int's, but it doesn't work when you start
dealing with sets of objects grouped into transactions.
Ensuring transactional integrity in a multi-threaded
environment, without deadlocks, still requires manually managing
locking and unlocking---even scoped locking doesn't really work
here. (You can implement transactions with scoped locking, but
only if you only handle one transaction at a time. In which
case, there's really no point in being multithreaded.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34