473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Thread safety

Hi,

I've just discovered that accessing Image.Height (and probably other
properties) in a multi-threaded environment can cause access problems (as a
test, you can spawn a hundred threads and get them all to perform a couple
of multiplications on an image's height and width ). I know I can prevent
simultaneous access, but is there any reason why Image can't be accessed
like this? And is this common for all .NET objects? I know the docs say 'not
thread-safe' but not being safe for multiple reads seems bizarre.

Steve
Jul 21 '05 #1
4 1973
http://msdn.microsoft.com/library/de...asp?frame=true

Info on thread safety. Internally Height always calls methods to get the height
and no caching is performed. Since each call is a method, there might be some
work, the return of an ObjectBusy status, or any number of other items that
might go wrong.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

I've just discovered that accessing Image.Height (and probably other
properties) in a multi-threaded environment can cause access problems (as a
test, you can spawn a hundred threads and get them all to perform a couple
of multiplications on an image's height and width ). I know I can prevent
simultaneous access, but is there any reason why Image can't be accessed
like this? And is this common for all .NET objects? I know the docs say 'not
thread-safe' but not being safe for multiple reads seems bizarre.

Steve

Jul 21 '05 #2
OK, cheers. Just the sort of thing I need to find out at 1am :-) Is this
common to the framework? I'm never likely to know what's cached and what
isn't for a given property.... and surely this is unnecessary if there are
only read operations going on? I'm still not sure why exactly that could
ever be a problem.

Steve

"Justin Rogers" <Ju****@games4dotnet.com> wrote in message
news:e5**************@tk2msftngp13.phx.gbl...
http://msdn.microsoft.com/library/de...asp?frame=true
Info on thread safety. Internally Height always calls methods to get the height and no caching is performed. Since each call is a method, there might be some work, the return of an ObjectBusy status, or any number of other items that might go wrong.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

I've just discovered that accessing Image.Height (and probably other
properties) in a multi-threaded environment can cause access problems (as a test, you can spawn a hundred threads and get them all to perform a couple of multiplications on an image's height and width ). I know I can prevent simultaneous access, but is there any reason why Image can't be accessed
like this? And is this common for all .NET objects? I know the docs say 'not thread-safe' but not being safe for multiple reads seems bizarre.

Steve


Jul 21 '05 #3
Image is abstracting you from a bunch of method calls. In GDI+ each object
is simply a pointer or handle, and you call methods to get the information from
those handles. If Image cached the values for you, then it would have a bunch
of state work to handle for things like moving to the next frame or other events
that could possibly change the height or width of the underlying image.

This generally isn't a huge problem across the framework, but is common when
API's are created that are wrapping an unmanaged API. The unmanaged API's
simply have semantics that don't translate 1 for 1 into the .NET world.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:Ob**************@TK2MSFTNGP09.phx.gbl...
OK, cheers. Just the sort of thing I need to find out at 1am :-) Is this
common to the framework? I'm never likely to know what's cached and what
isn't for a given property.... and surely this is unnecessary if there are
only read operations going on? I'm still not sure why exactly that could
ever be a problem.

Steve

"Justin Rogers" <Ju****@games4dotnet.com> wrote in message
news:e5**************@tk2msftngp13.phx.gbl...

http://msdn.microsoft.com/library/de...asp?frame=true

Info on thread safety. Internally Height always calls methods to get the

height
and no caching is performed. Since each call is a method, there might be

some
work, the return of an ObjectBusy status, or any number of other items

that
might go wrong.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

I've just discovered that accessing Image.Height (and probably other
properties) in a multi-threaded environment can cause access problems (as a test, you can spawn a hundred threads and get them all to perform a couple of multiplications on an image's height and width ). I know I can prevent simultaneous access, but is there any reason why Image can't be accessed
like this? And is this common for all .NET objects? I know the docs say 'not thread-safe' but not being safe for multiple reads seems bizarre.

Steve



Jul 21 '05 #4
Splendid explanation. That does kind of make sense, and it's a good job we
spotted it now (and didn't put it into our 'strange but true' bug file) and
not after release. My TV show "When multithreading goes bad" is coming on
leaps and bounds.

Thanks again, and I hope it's not as late where you are as it is here :-)

Steve

"Justin Rogers" <Ju****@games4dotnet.com> wrote in message
news:OC*************@TK2MSFTNGP12.phx.gbl...
Image is abstracting you from a bunch of method calls. In GDI+ each object is simply a pointer or handle, and you call methods to get the information from those handles. If Image cached the values for you, then it would have a bunch of state work to handle for things like moving to the next frame or other events that could possibly change the height or width of the underlying image.

This generally isn't a huge problem across the framework, but is common when API's are created that are wrapping an unmanaged API. The unmanaged API's
simply have semantics that don't translate 1 for 1 into the .NET world.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:Ob**************@TK2MSFTNGP09.phx.gbl...
OK, cheers. Just the sort of thing I need to find out at 1am :-) Is this
common to the framework? I'm never likely to know what's cached and what
isn't for a given property.... and surely this is unnecessary if there are only read operations going on? I'm still not sure why exactly that could
ever be a problem.

Steve

"Justin Rogers" <Ju****@games4dotnet.com> wrote in message
news:e5**************@tk2msftngp13.phx.gbl...

http://msdn.microsoft.com/library/de...asp?frame=true

Info on thread safety. Internally Height always calls methods to get the
height
and no caching is performed. Since each call is a method, there might
be some
work, the return of an ObjectBusy status, or any number of other items

that
might go wrong.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"Steve McLellan" <sjm.NOSPAM AT fixerlabs DOT com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I've just discovered that accessing Image.Height (and probably other
> properties) in a multi-threaded environment can cause access
problems (as a
> test, you can spawn a hundred threads and get them all to perform a

couple
> of multiplications on an image's height and width ). I know I can

prevent
> simultaneous access, but is there any reason why Image can't be
accessed > like this? And is this common for all .NET objects? I know the docs

say 'not
> thread-safe' but not being safe for multiple reads seems bizarre.
>
> Steve
>
>



Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
9
by: Alexander Fleck | last post by:
Hi, I' ve to make a software module thread safe. I know how to realize that and what' re the main topics of thread safety. But I don' t know how thread safety can be tested. I read about a test...
4
by: The Crow | last post by:
for example i have static readonly SqlParameter and i want to clone them at runtime. as clone operation will not write to SqlParameter object, just reading, should i lock that object during read...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
1
by: paul.hester | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
0
by: Graham Wideman | last post by:
Folks: Can anyone tell me what controls php's "thread safety" feature? I have an installation where phpinfo() is showing Thread safety: enabled, whereas I need it disabled in order to work...
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.