Hi Ayende,

After a little little tinkering I managed to get it working with
multiple threads, only with a lock when writing to the pht. I guess it
doesn't handle the locking internally!!! I hope I'm using it in the
correct way. Just wanted a quick yes/no confirmation from you before I
try implementing it on a large system. Sample code is below.

Thanks

using System;
using System.Text;
using System.Threading;
using Rhino.PersistentHashTable;

namespace ConsoleApplication1
{
    public class Program
    {
        public static object LockObj = new object();
        public static PersistentHashTable Pht = new PersistentHashTable
("esent.Test");

        public static void Read()
        {
            try
            {
                for (int j = 0; j < 50000; j++)
                {
                    ReadFromPht(Pht);
                }
            }
            finally
            {
                if (Pht != null)
                    Pht.Dispose();
            }
        }

        public static void Write()
        {
            try
            {
                for (int j = 0; j < 50000; j++)
                {
                    WriteToPht(Pht);
                }
            }
            finally
            {
                if (Pht != null)
                    Pht.Dispose();
            }
        }

        private static void Main(string[] args)
        {
            Pht.Initialize();

            new Thread(Read).Start();
            new Thread(Read).Start();
            new Thread(Write).Start();
            new Thread(Write).Start();
            new Thread(Read).Start();

            Console.ReadLine();
        }

        private static void ReadFromPht(PersistentHashTable pht)
        {
            var guid = "4641b72d-1fed-41fe-8670-6c7d22e9eb5a";

            pht.Batch(actions =>
                          {
                              var str = Encoding.ASCII.GetString
(actions.Get(new GetRequest {Key = guid})[0].Data);
                              Console.WriteLine(str);
                              Console.WriteLine("Length is: " +
actions.Get(new GetRequest { Key = guid })[0].Version.Number);
                              actions.Commit();
                          });

            Console.WriteLine(guid);
        }

        private static void WriteToPht(PersistentHashTable pht)
        {
            var guid = Guid.NewGuid().ToString();

            lock (LockObj)
            {
                pht.Batch(actions =>
                              {
                                  var putResult = actions.Put(new
PutRequest
                                                  {
                                                      Key = guid,
                                                      ParentVersions =
new ValueVersion[0],
                                                      Bytes =
Encoding.ASCII.GetBytes("This is a test string")
                                                  });
                                  actions.Commit();
                              });
            }
            Console.WriteLine(guid);
        }
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Rhino Tools Dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rhino-tools-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to