Example #1
0
        public IPredictor getPredictor()
        {
            IPredictor predictor = null;

            // Prepare the predictor
            // Create new instance for each thread for concurrent simulation
            // In the same time reuse the predictors after threads released them
            lock (this)
            {
                bool foundFree = false;
                int  i         = 0;

                for (; i < predictorLocked.Count; i++)
                {
                    if (!predictorLocked[i])
                    {
                        foundFree = true; break;
                    }
                }

                if (foundFree)
                {
                    if (predictorLocked[i] == true)
                    {
                        throw new Exception("Lock mechanism fail!");
                    }
                    predictorLocked[i] = true;
                    predictor          = predictorInstances[i];
                }
                else
                {
                    predictor = createPredictorInstance();
                    if (predictor == null)
                    {
                        return(null);
                    }

                    predictorInstances.Add(predictor);
                    predictorLocked.Add(true);
                }
            }

            predictor.reset();
            return(predictor);
        }