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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


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 34

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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