Home » U++ Library support » U++ MT-multithreading and servers » MT/Locking Questions
Re: MT/Locking Questions [message #15534 is a reply to message #15529] |
Mon, 28 April 2008 19:41 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
captainc wrote on Mon, 28 April 2008 09:23 | This is a general question about using Mutex locking. Does locking occur on a per object basis? what about static variables?
Also, how do locks work in class hierarchies?
For example:
class Parent{
Mutex lock;
String mydata;
virtual void DoSomething(){
for(int i=0;i<10;++i){
Cout() << mydata << "\n;"
}
};
};
class Child1{
void DoSomething(){
INTERLOCKED_(lock){
mydata = "Foo";
Parent::DoSomething();
}
}
};
class Child2{
void DoSomething(){
INTERLOCKED_(lock){
mydata = "Bar";
Parent::DoSomething();
}
}
};
CONSOLE_APP_MAIN{
Parent * c1 = new Child1();
Parent * c2 = new Child2();
Thread().Run(callback(&c1, &Parent::DoSomething);
Thread().Run(callback(&c2, &Parent::DoSomething);
delete c1;
delete c2;
}
Since 2 separate instances of the object are created, both DoSomething()'s can run concurrently, correct?
|
Yes and you do not even need the lock
General practice for C++ programming is, well, to put it simple:
Ignore MT in the class design as long as you do not need it.
In practice, this means that you need to serialize all write method calls for exclusive access and all read methods calls for shared access if you access single instance from more than one thread concurently.... (client code is responsible for locking).
Mirek
|
|
|
|
|
MT/Locking Questions
By: captainc on Mon, 28 April 2008 15:23
|
|
|
Re: MT/Locking Questions
By: mirek on Mon, 28 April 2008 19:41
|
|
|
Re: MT/Locking Questions
By: captainc on Mon, 28 April 2008 20:00
|
|
|
Re: MT/Locking Questions
By: mr_ped on Mon, 28 April 2008 20:57
|
|
|
Re: MT/Locking Questions
By: captainc on Mon, 28 April 2008 22:10
|
|
|
Re: MT/Locking Questions
By: mr_ped on Tue, 29 April 2008 10:03
|
|
|
Re: MT/Locking Questions
By: mirek on Tue, 29 April 2008 19:09
|
|
|
Re: MT/Locking Questions
By: mr_ped on Tue, 29 April 2008 21:49
|
|
|
Re: MT/Locking Questions
By: mirek on Wed, 30 April 2008 10:51
|
|
|
Re: MT/Locking Questions
By: mr_ped on Wed, 30 April 2008 16:04
|
|
|
Re: MT/Locking Questions
By: mirek on Wed, 30 April 2008 21:05
|
Goto Forum:
Current Time: Thu Apr 25 03:16:00 CEST 2024
Total time taken to generate the page: 0.04073 seconds
|