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?

Author Comment

ID: 40445380
I think a combination of the above and this would work
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

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 32

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed 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…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now