Home » Community » Coffee corner » Sharing and Locking
Re: Sharing and Locking [message #25851 is a reply to message #25831] |
Mon, 15 March 2010 21:26   |
gridem
Messages: 45 Registered: August 2008
|
Member |
|
|
luzr wrote on Sun, 14 March 2010 20:58 |
I am still not quite sure what you are trying to solve:)
What I think you are trying to do is to avoid dangling pointer. Anyway, making pointer itself dangling helps only a bit and perhaps is not a good strategy: Pointer itself can still exist, but the state of object can be "destroyed". So it may seal some references to it, but IMO is not a good way.
Now maybe my experiences are not wide enough, but I belive that so far, I had little problems with race conditions of this kind in MT code. I guess, usually the best is to make things simple and not get involved into any shared ownership, which after all is the cornerstone of U++ design.
|
OK, let me to clarify the problem statement.
Suppose that we want to share some data between 2 threads. The first thread (SetterThread) will create the global variable and put the pointer to such data, than the data will be destoyed. The second (AccesserThread) will try to access to the data and if such data will exist than it will assign some value. From U++ it looks like this:
void SetterThread()
{
while (true)
{
Data d;
*DataAccess() = &d;
}
}
void AccesserThread()
{
while (true)
{
Ptr<Data> d = *DataAccess();
if (d)
d->a = 2;
}
}
I use StaticAutoLock to prevent simultanious writing to the global data (see presentation for autolocking technique). If I start the following threads I will obtain the general protection failure error message (on Windows). The result will be better (crash will take place quicker) when the application will be started on multicore processor.
The specified code can be rewritten using the boost shared_ptr. In that case the global value must have the weak_ptr as the reference to the value in SetterThread. Corresponding code will be:
void SetterThread()
{
while (true)
{
shared_ptr<Data> d(new Data);
*DataAccess() = d;
}
}
void AccesserThread()
{
while (true)
{
shared_ptr<Data> d = DataAccess()->lock();
if (d)
d->a = 2;
}
}
In that case the application will never be crashed due to atomical conversion from weak_ptr to shared_ptr using lock() method in weak_ptr (see boost documentation for details).
This simple example shows that Ptr doesn't prevent from dangling pointer in concurrent application. This is not the problem in single threaded model and in MT when the access can be serialized using the "big lock" like GuiLock. But in other cases it can lead to problem with stability. This is the main reason and what I want to demonstrate.
The attachement contains the full code to compile and check.
|
|
|
 |
|
Sharing and Locking
By: gridem on Sun, 07 March 2010 09:27
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 07 March 2010 14:40
|
 |
|
Re: Sharing and Locking
By: gridem on Sun, 07 March 2010 16:38
|
 |
|
Re: Sharing and Locking
By: mirek on Mon, 08 March 2010 01:42
|
 |
|
Re: Sharing and Locking
By: gridem on Mon, 08 March 2010 10:57
|
 |
|
Re: Sharing and Locking
By: gridem on Sun, 14 March 2010 13:37
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 14 March 2010 18:58
|
 |
|
Re: Sharing and Locking
By: gridem on Mon, 15 March 2010 21:26
|
 |
|
Re: Sharing and Locking
By: mirek on Tue, 16 March 2010 06:02
|
 |
|
Re: Sharing and Locking
By: gridem on Tue, 16 March 2010 08:04
|
 |
|
Re: Sharing and Locking
By: mirek on Tue, 16 March 2010 23:50
|
 |
|
Re: Sharing and Locking
By: gridem on Fri, 19 March 2010 07:44
|
 |
|
Re: Sharing and Locking
By: mirek on Fri, 19 March 2010 07:59
|
 |
|
Re: Sharing and Locking
By: gridem on Sat, 20 March 2010 10:21
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 21 March 2010 07:37
|
 |
|
Re: Sharing and Locking
By: gridem on Sun, 21 March 2010 11:39
|
 |
|
Re: Sharing and Locking
By: mirek on Sun, 21 March 2010 14:21
|
 |
|
Re: Sharing and Locking
By: gridem on Thu, 01 April 2010 09:14
|
 |
|
Re: Sharing and Locking
By: mirek on Tue, 16 March 2010 06:14
|
Goto Forum:
Current Time: Sun Apr 27 08:12:50 CEST 2025
Total time taken to generate the page: 0.00436 seconds
|