|
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
|
|
|
|
|
You will be bound by the disk I/O at that point, so it will depend on how much work your doing with that stream. It might make more sense to use a memory stream and when your done, quickly dump that to disk.
I'd probabyl use MSMQ for my process list and have an external service pick up those messages to do the processing. Using MSMQ allows you to seperate out the functions of accepting work and doing work. The messages will also be persisted during failures by MSMQ, so it also gives you a safe guard.
I haven't looked at it much atall, but I think WCF can use MSMQ for it's message delivery medium. If you decide to go the route of seperating the processes, take a look at WCF as the communication mechanism.
|
|
|
|
|
Hi,
It has already been said that disk I/O will most likely be your performance bottleneck anyway, making object allocation / garbage collection a non-issue from a performance point of view.
However, disposing of file streams is in my experience often critical - as the physical file will stay open (and possibly locked) until the stream is disposed (or Close()d, which is equivalent if I remember correctly - either of them kills the Win32 file handle). Unless you're the only one touching the files, this can cause problems. I routinely wrap any file I/O in using blocks when possible, in order to minimize file lock durations.
In similar scenarios I usually just enqueue what needs to be done in a Queue<my_job_type> and dequeue it from a worker thread (locking the Queue when enqueuing or dequeueing). Usually I use a Queue<my_result_type> for the results from the worker thread.
Note: The normal restriction on only locking private stuff applies, of course. Your queues should not be visible outside of your class - if people in other classes (as would be expected) enqueue things to do and dequeue results, you wrap those operations (and queue lengths or HasWorkToDo and HasResults properties or whatever feels appropriate) in methods and properties of your own class. User code calls your en-/dequeueing methods (which use lock blocks), not the ones of the actual Queues.
I usually raise an event as well whenever I have enqueued stuff to the results queue, but beware that naïve users of your class may run into problems when they touch UI stuff from the event handler if they forget to Invoke() them properly (as the event will be raised from another thread than the UI thread). You should document that if your class isn't strictly internal.
Later,
--
Peter
|
|
|
|
|
First, thanks for your feedback.
I realize I've been a bit too vague in my initial post; however your suggestions reflect what I initially suspected.
Let me clarify a bit. I'm currently working on a 3D game and I've created a texture loader using the producer / consumer queue pattern. Depending on the view distance and the player position, I load and unload landscape textures from hard disk. I assumed (wrongly) that loading textures from another thread would be non-blocking for the main thread; this was not the case, since upon testing I was experiencing hiccups.
In another forum I read that perhaps an asynchronous loading would generally perform better (i.e. no hiccups). Seeing as I'm bound to load many textures per loader usage, I was curious to know about the performance of FileStream objects. I know they have to be correctly disposed; I'm not sure how the loader would behave when there are many concurrent asynchronous locks - if there's such a thing.
I know some testing would pretty much answer, but I thought that having some feedback, some first-hand experience on the matter would save me some time (which is never enough!).
Thanks again for any further advice,
Rob
PS: For any of you interested, some game screenshots can be found in the Gallery section of www.darkmana.com[^]
|
|
|
|
|
we'll be making an appointment schedulling for doctors.. it should display a table:
columns - doctor's name
rows - time slots
everytime we'll enter a new appointment, it will be displayed in the table.. can somebody help us??
|
|
|
|
|
sorry, whats the exact problem?
Tiefe Brunnen muss man graben wenn man klares Wasser will,
tiefe Wasser sind nicht still.
|
|
|
|
|