in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

A single step solution to avoid Deadlock in Multi-threading

Last post 02-19-2010 6:02 AM by elizas. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 02-19-2010 6:02 AM

    • elizas
    • Not Ranked
    • Joined on 02-19-2010
    • Posts 1

    A single step solution to avoid Deadlock in Multi-threading

    n its simplest form, deadlock occurs when each of two (minimun two) threads try to acquire a lock on a resource already locked by another. E.g: Take example of two threads 1. thread 1 2. Thread 2 And two resources 1. Resource 1 2. Resource 2 Thread 1 locked on Resources 1 and tries to acquire a lock on Reosurce 2. At the same time, Thread 2 has a lock on Resource 2 and it tries to acquire lock on Resource 1. Two threads never give up their locks, hence a DEADLOCK occures. Solution : The simplest way to avoid deadlock is to use a timeout value . You can use the Monitor class (system.Threading.Monitor) to set a timeout during acquiring a lock. Ex. in C# if(Monitor.TryEnter(this, 500)) { // critical section } catch (Exceprion ex) { } finally { Monitor.Exit(); } Here the timeout is 500 milliseconds . If the lock can't be acquired, after 500 miliseconds, timeout occurs and the code exit the Monitor block. Please add on to this.Hope u find this useful.
    Cheers,
    Eliza
Page 1 of 1 (1 items)
Powered by Community Server (Commercial Edition), by Telligent Systems