Example #1
0
        public static int Simulate_Games(int games_to_simulate)
        {
            recordsTotal = games_to_simulate;
            // Database writing setup code
            conn = SQLite_Methods.CreateConnection(Program.NUMBER_OF_PLAYERS);
            SQLite_Methods.CreateTableIfNotExists(conn);
            SQLite_Methods.DropIndexIfExists(conn);

            // CALL PRODUCERS AND CONSUMER HERE
            ThreadStart tProd           = new ThreadStart(RecordProducer);
            ThreadStart tCons           = new ThreadStart(RecordConsumer);
            Thread      producerThread  = new Thread(tProd);
            Thread      producerThread2 = new Thread(tProd);
            Thread      producerThread3 = new Thread(tProd);
            Thread      consumerThread  = new Thread(tCons);
            Timing      timer           = new Timing();

            timer.StartTime();
            producerThread.Start();
            producerThread2.Start();
            //producerThread3.Start();
            consumerThread.Start();
            while (true)
            {
                if (consumerThread.ThreadState == ThreadState.Stopped)
                {
                    timer.StopTime();
                    Thread.Sleep(1000);
                    break;
                }
            }

            Console.WriteLine($"Runtime duration: {timer.Result().TotalMinutes} minutes");
            return(0);
        }
Example #2
0
 // Record consumer writes records to the sqlite database
 public static void RecordConsumer()
 {
     command     = conn.CreateCommand();
     transaction = conn.BeginTransaction();
     do
     {
         // Execute Insert command on database, one row per player
         GameRecord record;
         while (true)
         {
             if (collection.TryTake(out record, 1))
             {
                 break;
             }
         }
         SQLite_Methods.InsertResultItem(record, command);
         recordsWritten++;
         if (recordsWritten % gamesPerTransaction == 0)
         {
             transaction.Commit();
             transaction = conn.BeginTransaction();
         }
     } while (recordsWritten < recordsTotal);
     // Inevitably we broke out of loop with a partial transaction. Flush it to disk.
     transaction.Commit();
     // Clean up
     command.Dispose();
     transaction.Dispose();
     conn.Dispose();
 }