예제 #1
0
        public void Run()
        {
            //create needed mutexes
            Mutex programEndsMutex = new Mutex(true, "programEnds");
            Semaphore wrt = Semaphore.OpenExisting("writerMutex");
            Semaphore currentRecordCount = Semaphore.OpenExisting("currentRecordCount");
            //set thread culture info in order to make easier logs
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            bool programEnds = false;
            while (!programEnds)
            {
               //try and wait for wrt writers mutex

                try
                {
                    wrt.WaitOne();
                    currentRecordCount.Release();//increment current record count
                    Data myData = new Data();
                    Random rand = new Random();
                    Thread.Sleep(rand.Next(0, writersDelay));//do something
                    short recordNum = (short)rand.Next(1, Program.RECORDCOUNT+1);
                    while (myData.writeData(recordNum) == false)
                    {
                        recordNum = (short)rand.Next(1, Program.RECORDCOUNT+1);
                        //Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + ":" + recordNum);
                    };

                    //log
                    Logger.Log(Thread.CurrentThread.ManagedThreadId.ToString(), "Writer finished writing");
                    //release writers mutex

                    wrt.Release();

                }
                catch (Exception e)
                {
                    wrt.Release();
                }
                //check if program should end
                if (programEndsMutex.WaitOne(0))
                {
                    programEndsMutex.ReleaseMutex();
                    programEnds = true;
                }

            }
            //thread will end, decrease threadCounter semaphore
            threadCounter.WaitOne();
        }
예제 #2
0
        public void Run()
        {
            //create needed mutexes
            Mutex programEndsMutex = new Mutex(true, "programEnds");
            Mutex mutex = new Mutex(false, "readerMutex");
            Semaphore wrt = Semaphore.OpenExisting("writerMutex");
            Semaphore currentRecordCount = Semaphore.OpenExisting("currentRecordCount");
            //set thread culture info in order to make easier logs
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            bool programEnds = false;
            while (!programEnds)
            {
                //readers critical section start
                mutex.WaitOne();
                rc++;
                if (rc == 1)//if this thread is the only active reader, prevent writers
                    wrt.WaitOne();

                if (!currentRecordCount.WaitOne(0)) //decrement current record count
                {
                    rc--;
                    if (rc == 0)//if no more active readers, release writers mutex
                        wrt.Release();
                    mutex.ReleaseMutex();
                }
                else
                {

                    mutex.ReleaseMutex();
                    //readers critical section end
                    Data myData = new Data();
                    Random rand = new Random();
                    Thread.Sleep(rand.Next(0, readersDelay));//do something
                    //readers critical section start
                    mutex.WaitOne();
                    short recordNum = (short)rand.Next(1, Program.RECORDCOUNT + 1);
                    while (myData.readData(recordNum) == false)
                    {
                        recordNum = (short)rand.Next(1, Program.RECORDCOUNT + 1);
                        //Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString() + ":" + recordNum);
                    };

                    //log
                    Logger.Log(Thread.CurrentThread.ManagedThreadId.ToString(), "Reader finished reading");
                    myData.makeNull(recordNum);
                    rc--;
                    if (rc == 0)//if no more active readers, release writers mutex
                        wrt.Release();
                    mutex.ReleaseMutex();
                    //readers critical section end
                }
                //check if program should end
                if (programEndsMutex.WaitOne(0))
                {
                    programEndsMutex.ReleaseMutex();
                    programEnds = true;
                }

            }
            //thread will end, decrease threadCounter semaphore
            threadCounter.WaitOne();
        }