|
The function written in my question was a 3 minute wrap, not yet formatted for my real program (which as i normally do tries to escape most errors by testing on validity before i make the call, besides that likely i'll introduce a boolean on return and the return value as an out variable)
But that about the function.
Does memberwiseclone does something different then copying fields bitwise and referenced objects by reference
?
And for the things i apply them are structures/classes which are only ment for data storage (iow abusing dictionaries for speedy retrieval of my mini in memory database like environment) i think that's more then enough.
Though if it can cause unwanted results it's handy to know that. But ain't that the same for createhandle and destroyhandle of a nativewindow etc. U need to know how to handle them and normally that would be by a good documentation of the function for everyone to read, which likely is present for memberwiseclone as well.
|
|
|
|
|
Mark Kruger wrote: But that about the function
MSDN[^]
Bastard Programmer from Hell
|
|
|
|
|
Exactly
But still u know often when u just have enough with a shallow copy, cause u created the class/structure etc u need an not original reference instance from.
And that's exactly where i made the function for.
Just to let me not dumb re-re-re-re enter the same line of code over and over again, nor gives me need to inherit a base class which makes it public
|
|
|
|
|
If you created the class you can provide a public Clone method which has an exactly specified interface and definition of its action.
If you are simply using an existing class, you can never know if MemberwiseClone will do what you want, because shallow copying private fields is dependent on the implementation of the class, and it is entirely legitimate for the class provider to update the private workings of the class in a way which will change how it operates.
For example, using a list: if the class you're using is simply a wrapper around an internal inner list class, you'll be getting essentially the same object after MemberwiseClone (and actions like Add will apply to both copies); if information is stored in fields of the class you're using, list modifying methods will (mostly) only apply to one copy. And you can't possibly tell which of those two it is going to be (you can use ILDASM or similar to find out what it is right now, but it might not be the same in the next release, or in the Compact Framework, or under Mono).
|
|
|
|
|
A clear view i understand and agree with, thank you
Cause of the reactions yesterday ended up writing a field reader writer (including basetypes), which works on arrays and icollection(generic only) as well (and specially in those occations to make of copy of each cell) recursivly with an object array[,] to be able to detect when items and pointing to somethin' processed before in that case i place the copied variant on the spot. Implemented all real valuetypes (iow int, long, decimal, string) by individually copy lines to make sure those go well. Called my function GetDeadCopy and returns a boolean now whether it had succes or not. Have not implemented a field checker yet for combi with collection to see what's touched. But the total function does already a lot more then were i wrote the thing for. Being more easily able to use data packages in dictionaries and lists without having the need to give each package an own copy constructor (or clone however you want to name it).
Anyways thanx for the responds and the reason for me to look a bit deeper.
|
|
|
|
|
Please use "you" instead of "u".
thanks.
V.
|
|
|
|
|
where to get tutorials or ebook.
|
|
|
|
|
Have you heard of Google[^]?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
It's a fad.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Yeah, I wonder if it'll last?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
can anybody help me in solving this deadlock?
class Program
{
readonly static object obj1 = new object();
readonly static object obj2 = new object();
static void F1()
{
lock (obj1)
lock (obj2)
{
Console.Write("1");
}
}
static void F2()
{
lock (obj2)
lock (obj1)
{
Console.Write("2");
}
}
static void Main(string[] args)
{
Task[] tasks = new[]
{
Task.Factory.StartNew(() => { for (int i = 0; i < 100; i++) F1(); }),
Task.Factory.StartNew(() => { for (int i = 0; i < 100; i++) F2(); }),
};
Task.WaitAll(tasks);
Console.WriteLine();
Console.WriteLine("all done");
}
}
|
|
|
|
|
Rather than using the locks the way you are doing it, try using Monitor.TryEnter instead. That provides a timeout that allows you to set how long it needs to wait before it gives up trying to acquire the lock.
|
|
|
|
|
can you clearly write your repaired code here?
|
|
|
|
|
numeracy wrote: write your repaired code
I am sure that he could, but why would he? Are your fingers broken?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
in fact, i don't understand his ideal(( so i can't
|
|
|
|
|
It would look something like this:
class Program
{
readonly static object obj1 = new object();
static void F1()
{
if (Monitor.TryEnter(obj1, TimeSpan.FromSeconds(0.3)))
{
Console.Write("1");
Monitor.Exit(obj1);
}
}
static void F2()
{
if (Monitor.TryEnter(obj1, TimeSpan.FromSeconds(0.3)))
{
Console.Write("2");
Monitor.Exit(obj1);
}
}
static void Main(string[] args)
{
Task[] tasks = new[]
{
Task.Factory.StartNew(() => { for (int i = 0; i < 100; i++) F1(); }),
Task.Factory.StartNew(() => { for (int i = 0; i < 100; i++) F2(); }),
};
Task.WaitAll(tasks);
Console.WriteLine();
Console.WriteLine("all done");
Console.ReadKey();
}
} You don't need two locking objects (in fact, I can't see why you had that in your original code - it was always going to deadlock at some point).
|
|
|
|
|
thank Pete O'Hanlon for replying
my code just is a typical sample for deadlock. and I want to find a solution, in which using two locking objects, mentioned above.
|
|
|
|
|
numeracy wrote: I want to find a solution, in which using two locking objects, mentioned above.
Why? The whole reason we do this is to avoid the possibility of deadlocking. Have a read of this[^] article; while old, the techniques and ideas still apply.
|
|
|
|
|
I check your code. unfortunately, it's not true
output:
11111111111111111111111111111111111111111122222222222222222222222222222222222222
22222222222211111111111111111222222222222222222222222222222222221111112222222222
2222211111111111111111111111111111111111
all done
|
|
|
|
|
And what would you expect in code that runs in parallel?
|
|
|
|
|
in fact, i don't deeply understand multithreading
maybe output is that:
11111111111111111....(100 times)
222222222............ (100 times)
|
|
|
|
|
That part was left for you to figure out for yourself - what would you expect to happen if you couldn't acquire a lock? That's application specific, so I didn't put that in there, but you could check to see if you got the lock and loop until you got it in that function. All you need is to capture the return value of Monitor.TryEnter to control this.
|
|
|
|
|
multithreading is like having several workers in a factory: if you don't take any precautions they all will manufacture some goods at their own pace; you need explicit synchronization if you want the goods produced in a controlled order.
However, if all you want is
11111111111111111....(100 times)
222222222............ (100 times)
then multithreading isn't doing anything for you, and you could better just use a single thread and no locks.
|
|
|
|
|
This has a huge smell of homework about it, as this is a clearly contrived example. The simple answer is, you should never nest locks if you can possibly avoid it, and if you have to lock on multiple things simultaneously (which should be rare), you need to do it in a consistent order (i.e. always lock on obj1 and then obj2).
Since this example is so contrived it's not possible to have a sensible discussion about it; neither F1 nor F2 actually requires exclusive access to anything so the correct solution here wouldn't lock at all.
|
|
|
|
|
if i don't want to print any number (1 or 2), i want to go to deadlock "obj1" immediately, so how can i fix my code?
|
|
|
|