Question Resolving Deadlock

manishdubey

New member
Joined
Mar 31, 2017
Messages
1
Programming Experience
5-10
Hello Everyone,
I am working on a project which uses multithreading frequently. Currently I am stuck with the situation of deadlock. Below is the method which is accessed by the multiple threads and causing the deadlock:
C#:
        [COLOR=#569cd6]public[/COLOR] [COLOR=#569cd6]bool[/COLOR] [COLOR=cyan]TryGetItem[/COLOR]([COLOR=lightblue]U[/COLOR] [COLOR=white]id[/COLOR], [COLOR=#569cd6]out[/COLOR] [COLOR=lightblue]T[/COLOR] [COLOR=white]item[/COLOR])
        {
            [COLOR=#569cd6]lock[/COLOR] ([COLOR=violet]lockingObject[/COLOR])
            {
                [COLOR=#569cd6]try[/COLOR]
                {
                    [COLOR=#569cd6]return[/COLOR] [COLOR=violet]objectDictionary[/COLOR][COLOR=#b4b4b4].[/COLOR][COLOR=cyan]Find[/COLOR]([COLOR=white]id[/COLOR], [COLOR=#569cd6]out[/COLOR] [COLOR=white]item[/COLOR]);
                }
                [COLOR=#569cd6]catch[/COLOR] ([COLOR=lightblue]Exception[/COLOR])
                {
                    [COLOR=white]item[/COLOR] [COLOR=#b4b4b4]=[/COLOR] [COLOR=#569cd6]null[/COLOR];
                }
                [COLOR=#569cd6]return[/COLOR] [COLOR=#569cd6]false[/COLOR];
            }
        }

Please let me know any solution by which I can make this method deadlock free. Thanks in advance.
 
I can't see how that method alone could cause a deadlock. A deadlock occurs when two or more threads are each waiting for access to a resource that the other thread(s) have locked and I don't see how that can happen based on just that method.

By the way, please just post your code snippets as plain text like so:
[xcode=c#]your code here[/xcode]
 
Back
Top Bottom