threading and bypassing a lock cont: 
Author Message
 threading and bypassing a lock cont:

Hi:

I tried the suggestions so far, but I still do not have the results
that I want. What I want is for the second thread to entirely skip
the call being made to the call to Fib. (I do not want it to go back
at a later time).

The program output is given first, and then is followed by the program
itself .

**This is the result what I want
hi from the second method
hi from the third method
Fib 41 is 165580141  // my note: this would be the result from the first
thread
       // the first two lines represent the result from the second thread
       // the second thread skips the call to Fib1
hi from the second method
hi from the third method
Press any key to continue

**This is the result that I get
Fib 41 is 165580141
hi from the second method
hi from the third method
Fib 41 is 165580141
hi from the second method
hi from the third method
Press any key to continue

The program:
---------------------------------------------------

using System;
using System.Threading;

namespace learningthreads3
{

 class Class1
 {
  public delegate void OpsList();
  Thread[] thrd;
  static int tnum = 0;
  [STAThread]
  static void Main(string[] args)
  {
   Class1 c1 = new Class1();
   c1.NonStaticContext();
  }

//********************** initializing and starting the theads ******

  public void NonStaticContext()
  {
   // the first delegate
   OpsList ol1 = new OpsList(M1);
   ol1 += new OpsList(M2);
   ol1 += new OpsList(M3);

   // the second delegate
   OpsList ol2 = new OpsList(M1);
   ol2 += new OpsList(M2);
   ol2 += new OpsList(M3);

   thrd = new Thread[2];

   thrd[0] = new Thread(new ThreadStart(ol1));
   thrd[1] = new Thread(new ThreadStart(ol2));

   thrd[0].Start();
   thrd[1].Start();
  }

//********************** using monitor with fibonacci
*************************

  public void M1()
  {
   Monitor.TryEnter(thrd[tnum++], 50);
   try
   {
    Console.WriteLine("Fib 41 is {0}",Fib(41));
   }
   finally
   {
    Monitor.Exit(thrd[(tnum-1)]);
   }
  }

  // two methods that follow the call to the fibonacci
  public void M2() {Console.WriteLine("hi from the second method");}
  public void M3() {Console.WriteLine("hi from the third method");}

  public int Fib(int k)
  {
   if(k <= 0)
   {
    return 0;
   }
   if(k <= 2)
   {
    return 1;
   }
   else
   {
    return Fib(k-1) + Fib(k-2);
   }

  }

 }

Quote:
}



Thu, 30 Dec 2004 05:23:05 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. threads and bypassing a lock

2. Knowing when threads have finished without locking the main thread

3. num lock, cap lock, and scroll lock

4. Caps Lock / Num Lock / Scroll Lock

5. Thread safe file locking.

6. thread locks up when calling GetInterfaceFromGlobal

7. In-Proc Server Thread Locking

8. Visual Studio 6 locks up when debugging multi-thread app

9. Problem concerning CRecordset, ODBC, Time consumption, Threads locked

10. newbie Question about threads and Lock?

11. Threads and vitual lock

12. COM Thread Locking Problem

 

 
Powered by phpBB® Forum Software