|
George_George wrote: I doubt -- "Reading and writing of an int are atomic operations". Because if it is true, the system API Interlocked.Exchange overload for Int32 is useless. Any comments?
Either you don't know that the Interlocked.Exchange method does, or you misunderstood what I wrote. The documentation for the Interlocked.Exchange method says:
"Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation."
So, it does two operations as an atomic operation. I wrote that reading and writing of an int are atomic operations, I didn't write that subsequent reading and writing of an int becomes a single atomic operation.
George_George wrote: It is great if you could provide some references to support the points -- "Reading and writing of an int are atomic operations".
"12.5 Atomicity of variable references
Reads and writes of the following data types shall be atomic: bool , char , byte , sbyte , short , ushort ,
uint , int , float , and reference types. In addition, reads and writes of enum types with an underlying type
in the previous list shall also be atomic. Reads and writes of other types, including long , ulong , double ,
and decimal , as well as user-defined types, need not be atomic. Aside from the library functions designed
for that purpose, there is no guarantee of atomic read-modify-write, such as in the case of increment or
decrement."
ECMA-334[^]
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
Your reply is great!
"reference types" -- means reference types of bool, char, byte, sbyte, short, ushort, uint, int, float (e.g. ref bool) or all reference types (including reference to a class)?
regards,
George
|
|
|
|
|
George_George wrote: "reference types" -- means reference types of bool, char, byte, sbyte, short, ushort, uint, int, float (e.g. ref bool)
No, you have to keep those two concepts separate. Sending a parameter by reference doesn't make it a reference type. A value type sent by reference is still a value type.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
So in the great section "12.5 Atomicity of variable references" you quoted, "reference type" means references to classes' instances, right?
regards,
George
|
|
|
|
|
George_George wrote: So in the great section "12.5 Atomicity of variable references" you quoted, "reference type" means references to classes' instances, right?
No, "reference type" means class.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
It is clear now.
regards,
George
|
|
|
|
|
Your question is not clear to me. Say status is a variable which will be modified by multiple threads, you can tell compiler not to optimize that filed by setting it as volatile . Else, you could acquire a lock before you read the variable value. Correct me if I got you wrong.
|
|
|
|
|
Thanks N a v a n e e t h,
1.
My question is, I want to make the access to variable Status thread safe. Some threads will write it and some threads will read it.
When write it, I will use Interlocked.Exchange method to set the value, and when read it, I will use Thread.VolatileRead method to read its current value. Is such solution thread safe?
2.
I think int assignment operation is not atomic and not thread safe, right?
regards,
George
|
|
|
|
|
George_George wrote: I will use Interlocked.Exchange method to set the value, and when read it, I will use Thread.VolatileRead method to read its current value. Is such solution thread safe?
It should be. Easy way is to mark the variable as volatile.
George_George wrote: I think int assignment operation is not atomic and not thread safe, right?
Assignment operation is not thread safe. You need some kind of locking here. volatile variables always have the latest data updated by each thread. So if you are looking for a flagging mechanism to end the running thread safely, volatile is the easy way to go.
|
|
|
|
|
Thanks N a v a n e e t h,
Only add volatile to read, no need to add to write?
regards,
George
|
|
|
|
|
George_George wrote: Only add volatile to read, no need to add to write?
You add volatile to the declaration of the variable. This will keep the compiler from optimising away reads and writes.
However, the only situation where a write would ever be possible to optimise away, would be if you assign it more than once in a code block without any alternative execution paths. Like this:
Status = 1;
Status = 0;
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
1.
Your sample about write assignment does not relates to volatile, right? I mean volatile will keep assignment of Status from optimization?
2.
So, volatile only works for read, not for write?
regards,
George
|
|
|
|
|
George_George wrote: Your sample about write assignment does not relates to volatile, right? I mean volatile will keep assignment of Status from optimization?
Right.
George_George wrote: So, volatile only works for read, not for write?
It works for both. Normally writes are just not optimised away at all (for member variables), so it's mostly the reads that are critical.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thanks Guffa,
I am interested how volatile will impact write?
regards,
George
|
|
|
|
|
George_George wrote: I am interested how volatile will impact write?
The effect is that every write will be done.
For example, in this code:
for (int i = 0; i <= 1000; i++) x = i;
If the x variable is declared as volatile, the code is guaranteed to perform the loop and write every single value in the loop to the variable. If it's not volatile, the compiler could optimize away the entire loop, replacing it with simply:
x = 1000;
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Great Guffa!
You let me know new things.
regards,
George
|
|
|
|
|
I have to access to a filde in a column(record) in data grid when click on the column.(in c#)
hello
modified on Friday, May 16, 2008 8:10 AM
|
|
|
|
|
post this in a forum not here
Sacha Barber
- Microsoft Visual C# MVP 2008
- Codeproject MVP 2008
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
My Blog : sachabarber.net
|
|
|
|
|
I know this is not C# but I figure there's more likely to be people in this forum that have played with PowerShell.
I've been experimenting with a couple of scripts to set folder permissions and I'm getting nowhere fast.
I've tried this from here http://chrisfederico.wordpress.com/2008/02/01/setting-acl-on-a-file-or-directory-in-powershell/[^]:
$acl = Get-Acl c:\temp
$permission = "domain\user","FullControl","Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
$acl | Set-Acl c:\temp
and this from here http://technet.microsoft.com/en-us/magazine/cc194419.aspx[^]:
#ChangeACL.ps1
$Right="FullControl"
#The possible values for Rights are
# ListDirectory, ReadData, WriteData
# CreateFiles, CreateDirectories, AppendData
# ReadExtendedAttributes, WriteExtendedAttributes, Traverse
# ExecuteFile, DeleteSubdirectoriesAndFiles, ReadAttributes
# WriteAttributes, Write, Delete
# ReadPermissions, Read, ReadAndExecute
# Modify, ChangePermissions, TakeOwnership
# Synchronize, FullControl
$StartingDir=Read-Host "What directory do you want to start at?"
$Principal=Read-Host "What security principal do you want to grant" `
"$Right to? `n Use format domain\username or domain\group"
#define a new access rule.
#note that the $rule line has been artificially broken for print purposes.
#it needs to be one line. the online version of the script is properly
#formatted.
$rule=new-object System.Security.AccessControl.FileSystemAccessRule
($Principal,$Right,"Allow")
foreach ($file in $(Get-ChildItem $StartingDir -recurse)) {
$acl=get-acl $file.FullName
#Add this access rule to the ACL
$acl.SetAccessRule($rule)
#Write the changes to the object
set-acl $File.Fullname $acl
}
For the second one I get this error when trying to apply it to an inetpub folder:
Exception calling "SetAccessRule" with "1" argument(s): "Some or all identity references could not be translated."
At C:\Documents and Settings\kmcfarlane\My Documents\Development\ChangeACL.ps1:29 char:21
+ $acl.SetAccessRule( <<<< $rule)
Set-Acl : The security identifier is not allowed to be the owner of this object.
At C:\Documents and Settings\kmcfarlane\My Documents\Development\ChangeACL.ps1:32 char:10
+ set-acl <<<< $File.Fullname $acl
For the first script I just get the first part of that error. Any ideas?
Kevin
|
|
|
|
|
This would probably be a better question in the Operating System/ Sysadmin forum as I think there's probably more PowerShell users there.
|
|
|
|
|
There are more eyes in this forum and nothing turned up when I did a search on PowerShell in that forum, so I thought I'd try here.
Kevin
|
|
|
|
|
I handle a lot of images (about 500) in my application. These Images do I present in PictureBoxes that is dynamically inserted form top to bottom In a TabControl.
My program uses about 1 GB memory when I present all these Pictureboxes. I need a way to handle this better. I’ve searched for a custom control that handle Images but haven’t found one.
_____________________________
...and justice for all
APe
|
|
|
|
|
Ditch the pictureboxes, they suck. Create your own control. The way I did this, was the store my images in memory as JPEGs ( in a stream, using the Save method on the Bitmap class ) and I decompress them as they become visible and compress them again as they go from view.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I agree - PictureBoxes suck!
I'll try to create my own control for this. Feels like there should be a conrtol for this somewhere, but ok - I'll try to do my own.
_____________________________
...and justice for all
APe
|
|
|
|
|
Hello everyone,
I'd like to describe thoroughly my current scenario but it'd get a little bit verbose and perhaps misleading.
Let's say the main thread is queuing items to load and another thread should be loading asynchronously these items.
My main question is about the performance of creating and disposing of several FileStream objects, used to load files asynchronously several times in a row.
If you were to create a producer/consumer queue bound to async load files from hard-disk, how would you go? Would the disposal and creation of FileStream objects be of concern for you?
Thanks in advance!
Rob
|
|
|
|
|