コード例 #1
0
    static int Main(string[] args)
    {
        int rValue = 0;
        Thread[] threads = new Thread[100];
        ThreadSafe tsi = new ThreadSafe();

        KrisClass kcIn = new KrisClass(args[0]);

        Console.WriteLine("Creating threads");
        for (int i = 0; i < threads.Length; i++)
        {
            threads[i] = new Thread(new ParameterizedThreadStart(tsi.ThreadWorker));
            threads[i].Start(kcIn);
        }
			
        tsi.Signal();

        Console.WriteLine("Joining threads");
        for(int i=0;i<threads.Length;i++)
            threads[i].Join();
        
        // Build the expected string
        KrisClass kcExpected = new KrisClass("hello world! ");      
        for(int i=0;i<threads.Length * 100;i++)
            kcExpected = kcExpected + kcIn;

        if(kcExpected == tsi.GetValue)
            rValue = 100;
	Console.WriteLine("Test Expected {0}, but found {1}", kcExpected, tsi.GetValue);
        Console.WriteLine("Test {0}", rValue == 100 ? "Passed" : "Failed");
        return rValue;
    }
コード例 #2
0
    public void ThreadWorker(Object objIn)
    {
        KrisClass kcIn = (KrisClass)objIn;

        signal.WaitOne();
        for (int i = 0; i < numberOfIterations; i++)
        {
            AddToTotal(kcIn);
        }
    }
コード例 #3
0
 private KrisClass AddToTotal(KrisClass addend)
 {
     KrisClass initialValue = new KrisClass(string.Empty);
     KrisClass newValue = new KrisClass(string.Empty);
     do
     {
         initialValue = Val;
         newValue = initialValue + addend;
     } 
     while (initialValue != Interlocked.CompareExchange<KrisClass>(
         ref Val, newValue, initialValue));
     return newValue;
 }
コード例 #4
0
    private KrisClass AddToTotal(KrisClass addend)
    {
        KrisClass initialValue = new KrisClass(string.Empty);
        KrisClass newValue     = new KrisClass(string.Empty);

        do
        {
            initialValue = Val;
            newValue     = initialValue + addend;
        }while (initialValue != Interlocked.CompareExchange <KrisClass>(
                    ref Val, newValue, initialValue));
        return(newValue);
    }
コード例 #5
0
    private KrisClass AddToTotal(KrisClass addend)
    {
        KrisClass initialValue;
        KrisClass newValue;

        do
        {
            initialValue = Val;
            newValue     = initialValue + addend;
        }while ((object)initialValue != Interlocked.CompareExchange <KrisClass>(
                    ref Val, newValue, initialValue));

        return(newValue);
    }
コード例 #6
0
ファイル: exchangetclass.cs プロジェクト: yukitos/coreclr
    public void ThreadWorkerB()
    {
        KrisClass ret = null;
        signal.WaitOne();
        for (int i = 0; i < numberOfIterations; i++)
        {
            ret = Interlocked.Exchange<KrisClass>(ref totalValue, newValueB);

            // Check return value
            if (ret.ClassVal != newValueA.ClassVal && 
                ret.ClassVal != newValueB.ClassVal &&
                ret.ClassVal != 1)
            {
                Console.WriteLine(ret.ClassVal + "," + 
                    newValueB.ClassVal + "," + newValueA.ClassVal);
                success = false;
            }
        }
    }
コード例 #7
0
    static int Main(string[] args)
    {
        int rValue = 0;

        Thread[]   threads = new Thread[100];
        ThreadSafe tsi     = new ThreadSafe();

        KrisClass kcIn = new KrisClass(args[0]);

        Console.WriteLine("Creating threads");
        for (int i = 0; i < threads.Length; i++)
        {
            threads[i] = new Thread(new ParameterizedThreadStart(tsi.ThreadWorker));
            threads[i].Start(kcIn);
        }

        tsi.Signal();

        Console.WriteLine("Joining threads");
        for (int i = 0; i < threads.Length; i++)
        {
            threads[i].Join();
        }

        // Build the expected string
        KrisClass kcExpected = new KrisClass("hello world! ");

        for (int i = 0; i < threads.Length * 100; i++)
        {
            kcExpected = kcExpected + kcIn;
        }

        if (kcExpected == tsi.GetValue)
        {
            rValue = 100;
        }
        Console.WriteLine("Test Expected {0}, but found {1}", kcExpected, tsi.GetValue);
        Console.WriteLine("Test {0}", rValue == 100 ? "Passed" : "Failed");
        return(rValue);
    }