Solved

Threading with Mutex

Posted on 2014-11-15
9
134 Views
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.

Thanks
0
Comment
Question by:Webfort
[X]
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
9 Comments
 
LVL 16

Expert Comment

by:hjgode
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.
0
 

Author Comment

by:Webfort
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
0
 

Author Comment

by:Webfort
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?
http://mikeperetz.blogspot.co.uk/2013/12/cross-process-locking-using-named-mutex.html?m=1
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Webfort
ID: 40445380
I think a combination of the above and this would work http://www.dotnetperls.com/lock?
0
 
LVL 16

Expert Comment

by:hjgode
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?
0
 

Assisted Solution

by:Webfort
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?
0
 
LVL 16

Assisted Solution

by:hjgode
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:
CreateSubDirAndCopyFiles(xyz)
  create dir
  if dir has to get files->create thread with arg for dir and files
goto start

CopyFilesThread(dir,fileList)
  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.
0
 
LVL 34

Accepted Solution

by:
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


Sara
0
 

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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