Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concerned about deadlocks with C# StreamReader

Just a general question, I'm developing an ASP.NET MVC 3 web application that reads from a configuration file using the Stream Reader inside a Using statement for automatic disposal as follows:

using (StreamReader sr = new StreamReader(filepath))
{

}

My concern is that when multiple users are running the application, a deadlock will occur when multiple instances of the application are reading values from the configuration file. My question is, are deadlocks an issue when working with the Stream Reader class? Intuitively I would think not, since I'm assuming the Stream Reader is opening the file in read-only mode and there are no external processes writing to the configuration file other than manual edits. I just wanted to be sure whether or not this would be an issue. Any insight is appreciated.

like image 280
kingrichard2005 Avatar asked Apr 16 '12 18:04

kingrichard2005


People also ask

What causes a deadlock in C?

A process with two or more threads can enter deadlock when the following three conditions hold: Threads that are already holding locks request new locks. The requests for new locks are made concurrently.

How can we avoid deadlock in C?

The common advice for avoiding deadlock is to always lock the two mutexes in the same order: if you always lock mutex A before mutex B, then you'll never deadlock.

What is a deadlock C?

A deadlock is a situation in which two computer programs sharing the same resource are effectively preventing each other from accessing the resource, resulting in both programs ceasing to function. The earliest computer operating systems ran only one program at a time.

What are 3 ways of handling deadlocks?

There are four methods of handling deadlocks - deadlock avoidance, deadlock prevention, deadline detection and recovery and deadlock ignorance. We can prevent a deadlock by preventing any one of the four necessary conditions for a deadlock. There are different ways of detecting and recovering a deadlock in a system.


1 Answers

My concern is that when multiple users are running the application, a deadlock will occur when multiple instances of the application are reading values from the configuration file

No, don't worry. No deadlock will occur. You are opening the file for reading. So you could have concurrent readers. But if you write to this file in some other location of your code that you will get an exception. But no deadlock. If you want to properly synchronize readers and writers to some shared resource such as a file you could use the ReaderWriterLockSlim class. If you never write to the file (from the same process or some other process) then you are fine. But if you never write to the file, to avoid reading and parsing this file everytime, you should only read it once and cache it in some memory structure. For example that's how the .NET application configuration system works. It reads and parses the app/web.config once when running the application and then stores it into memory for faster access. But of course everything will depend on your exact scenario.

like image 161
Darin Dimitrov Avatar answered Sep 30 '22 09:09

Darin Dimitrov