Threading with Mutex

Posted on 2014-11-15
Last Modified: 2014-11-22
I have a console application which will process a number of folder paths and documents. The idea is that I want to start a thread for each path/file. Each of these paths will be created on a server using an API. for each folder created I need to get the guid for that folder

Once I process each path I'm adding them to a dictionary Dictionary<string, Guid>, so if I have the folder in the dictionary I don't need to call the api again.

The problem is when I start the threads they are all hitting the method at the same time. So if I have this string[] folders = { "/Folder 1/Folder 2", "/Folder 1" }; both threads are calling and trying to create Folder 1.

What I would like to do is something like this:
using (_lockThis.GetLock(folderPath)) {
                    Guid folderGuid = DirectoryRecursively(folderPath);

Open in new window

How could I obtain a lock so I an process the folder path and record the returned guid.

Question by:Webfort
  • 5
  • 3
LVL 16

Expert Comment

ID: 40445330
I would use a global lock inside the DirectoryRecursively() function. The function will then block if another call is in progress. Inside the lock the function should then check first if the dir already exists (done by another call).

Possibly you can show your DirectoryRecursively() function to get a detailed advice.

Author Comment

ID: 40445365
I use a dictionary to check if the directory exists. The problem is, say two threads start off in the example outlined. Both hit the method at the same time so both try and create Folder 1,  which then causes problems with the dictionary as they both try and add an item with the same key.

Check if incoming folder path exists in dictionary. return guid if it does
loop through the individual folders
Check if individual folder exists.
if it does not exist call api get guid add to dictionary and return value.

thanks in advance

Author Comment

ID: 40445368
Also the reason I'm looking at mutex is this code could run across multiple processes. This looks like something I could adapt, but need a little help. I was looking to use a unique id/folder path for that user running the process?
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


Author Comment

ID: 40445380
I think a combination of the above and this would work
LVL 16

Expert Comment

ID: 40445395
Sorry, but your code posts and links do not show how to approach your issue.

Example: you try to start two threads with \Dir1\Dir2 and \Dir1; thread 1 tries to create Dir1 and then Dir2, thread2 try to create Dir1. Now you need a lock for creating Dir1. If that is in thread 1 it will lock thread 2. Then Dir1 is created and thread2 starts to create Dir1, but it already exists. what I mean is you need a lock on a specific path (like /Dir1 or /Dir1/Dir2). A lock on the last dir component would not make sense, ie you can have Dir1/Dir2 und Dir1/Dir3 and would use a lock on Dir2 and Dir3.

So if you lock during any dir creation that would make your multithreading obsolete, if creating a dir is the only task inside the thread. But if you then have maultiple tasks (like Document creations etc) inside the sub dir, that would be a task to place inside a separate thread.

A lock (mutex) can asure that only one thread performs a specific task on a shared object. If you have multiple non-shared objects, like individual path, a lock makes no sense.

So, what about creating the dirs in one thread and then start separate threads to perform the tasks inside the individual dirs?

Assisted Solution

Webfort earned 0 total points
ID: 40445515
Thanks, that makes sense. It would make the multi threading obsolete.

So create all  my directory structure in one thread, and then the files could be put into multiple threads? I guess I would then need to map file to the folder id so i can perform my uploads. Or like you say once I have created my dir upload my files and then move on to create the other folders?
LVL 16

Assisted Solution

hjgode earned 250 total points
ID: 40446632
"So create all  my directory structure in one thread, and then the files could be put into multiple threads?"
Yes, and for each directory created that needs files put there start a thread to do so and the code will continue in creating new dirs.

pseudo code:
  create dir
  if dir has to get files->create thread with arg for dir and files
goto start

  will copy the files in background

with the above you do not need a mutex. Possibly you may use a ThreadPool where the threads are put in and started and executed by the system if there are free slots. The results will then delivered async.
LVL 33

Accepted Solution

sarabande earned 250 total points
ID: 40447800
your first idea is also valid. instead of locking a path you can lock directory creation like that:

public class MyFolder
    private string folderPath;
    private static object LockingVar = new object();
    public MyFolder(string path) 
          folderPath = path;
    public bool CreateFolderIfNotExists()
      lock (LockingVar)
         // check whether the folder exists. if not create the folder
      return true;

Open in new window


Author Closing Comment

ID: 40459183
Thanks, I now lock the directory creation. Thanks for all your help.

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Media Player 2 26
compare between entities in entity framework 8 72
MediaHelp 4 23
VB.NET 2008 - SQL Timeout 9 24
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question