Is it possible to dedicate a mutex to a specific name ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eran otzar
    New Member
    • Jan 2011
    • 13

    Is it possible to dedicate a mutex to a specific name ?

    hello,
    on my server iv'e got an xml file for each client witch holds the packets witch the client had not received.

    i need to access multiple client files on a server side ,this happens from multiple background threads

    i'm looking for a way to create mutual Exclusion an a specific file

    for example lets say iv'e got tow clients john and tom
    and the method running in the background (_AppendToUnsen t)

    now lets say john as 2 packets to append ,and tom as 3

    2 threads would be dispatched for john and three for tom

    i don't want to block a thread writing to tom.xml because a different thread is writing to john.xml

    i would how ever want to block a second thread trying to access john.xml while a different one is already writing to the file

    private static void _AppendToUnSent (object obj)
    {
    append_packets_ mutex.WaitOne() ; // this will block all threads no matter if the writing to the same file or not
    // holds the name and the packet
    KeyValuePair<St ring, byte[]> _pair = (KeyValuePair<S tring, byte[]>)obj;

    XmlDocument _doc = new XmlDocument();
    // build the path of the file
    StringBuilder _builder = new StringBuilder() ;
    _builder.Append (_path);
    _builder.Append (_pair.Key);
    _builder.Append (".xml");
    if (!File.Exists(_ builder.ToStrin g()))
    { // if the file dosent exist create it
    XmlDeclaration xmlDeclaration = _doc.CreateXmlD eclaration("1.0 ", "utf-8", null);
    XmlElement rootNode = _doc.CreateElem ent(_pair.Value .ToString());
    _doc.InsertBefo re(xmlDeclarati on, _doc.DocumentEl ement);
    _doc.AppendChil d(rootNode);
    XmlElement _packets_node = _doc.CreateElem ent("Packets");
    rootNode.Append Child(_packets_ node);

    _doc.Save(_buil der.ToString()) ;
    }

    try
    {
    _doc.Load(_buil der.ToString()) ;
    }
    catch (Exception es)
    {
    Console.WriteLi ne("Could Not save packet for " + _pair.Key);
    return;
    }
    // create and save a new <Packet> Node
    XmlNode declarition_nod e = _doc.FirstChild ;// <Xml......>
    XmlNode packets_node = declarition_nod e.NextSibling;// <Messages>

    XmlElement _packet_node = _doc.CreateElem ent("Packet");// <Packet>

    _packet_node.In nerText = Convert.ToBase6 4String(_pair.V alue);//43rg43g43yt42g. ... // Encode to base64
    _packet_node.Ap pendChild(_pack et_node);// <Packet>43rg43g 43yt42g....</Packet>
    try
    {
    _doc.Save(_buil der.ToString()) ;
    }
    catch (Exception es)
    {
    Console.WriteLi ne("Could Not save packet from " + _pair.Key);
    }
    append_packets_ mutex.ReleaseMu tex(); // realese the genrel mutex for this operaition

    } // end _AppendToUnsent e here
  • Leito
    New Member
    • Apr 2010
    • 58

    #2
    First, paste your code into [ code ] tags, it helps the reader.

    For your problem, I think you should use a switch{ case: } loop, and have a different process depending on the file you have to treat.
    For example:
    Code:
    private static void _AppendToUnSent(object obj){
        switch (file_name ){
            case "john.xml":
                append_packets_mutex_john.WaitOne();
            case "tom.xml":
                append_packets_mutex_tom.WaitOne();
        }
    }

    Comment

    Working...