0

The following code strips a ThreadState to one of the four most useful values: Unstarted, Running,WaitSleepJoin, and Stopped:

public static ThreadState SimpleThreadState (ThreadState ts)
{
   return ts & (ThreadState.Unstarted |
   ThreadState.WaitSleepJoin |
   ThreadState.Stopped);
}

I read the above in a book, but I am not quite sure what the author want to illustrate here. I have tested like below:

class Program
{
   static void Main(string[] args)
   {
      System.Console.WriteLine(SimpleThreadState(ThreadState.Aborted));
      System.Console.WriteLine(SimpleThreadState(ThreadState.Background));
      System.Console.WriteLine(SimpleThreadState(ThreadState.AbortRequested));
      System.Console.WriteLine(SimpleThreadState(ThreadState.Suspended));
      System.Console.WriteLine(SimpleThreadState(ThreadState.Unstarted));
      System.Console.WriteLine(SimpleThreadState(ThreadState.WaitSleepJoin));
      System.Console.WriteLine(SimpleThreadState(ThreadState.Stopped));
   }

public static ThreadState SimpleThreadState(ThreadState ts)
{
    return ts & (ThreadState.Unstarted |
        ThreadState.WaitSleepJoin |
        ThreadState.Stopped);
}

}

And here is the running result: Running Running Running Running Unstarted WaitSleepJoin Stopped

The last three lines of output is straight forward, but why all else outputs Running state?

Thanks!

Rakshit Pai
  • 291
  • 2
  • 15
spspli
  • 3,128
  • 11
  • 48
  • 75

3 Answers3

1

It determines if the ThreadState is in any of the three states in parenthesis. This is simply a convenience function written by someone to determine if it's one of the states that they care about, and weed out the states they don't (returning Running for all over values).

It's easier to figure out what it's doing if you think of it in terms of binary logic with numbers, rather than looking at the enumeration names. Here's a tutorial that might explain it in better detail.

pickypg
  • 22,034
  • 5
  • 72
  • 84
1

It's more obvious when you check flag values for each member of the ThreadState enumeration:

[Flags]
public enum ThreadState
{
    Running = 0,
    StopRequested = 1,
    SuspendRequested = 2,
    Background = 4,
    Unstarted = 8,
    Stopped = 16,
    WaitSleepJoin = 32,
    Suspended = 64,
    AbortRequested = 128,
    Aborted = 256,
}

Since your method uses a binary AND operation with (ThreadState.Unstarted | ThreadState.WaitSleepJoin | ThreadState.Stopped), it will return 0 in all other cases, which is the value of ThreadState.Running.

vgru
  • 49,838
  • 16
  • 120
  • 201
1

Bascially this is a binary mask. By or'ing together

ThreadState.Unstarted, ThreadState.WaitSleepJoin,ThreadState.Stopped you construct a mask. When you and that mask with another value tells if you an of the other bits are still turned on.

Let's pretend that we're dealing with a four bit int Thread.Unstarted == 1, ThreadState.WaitSleepJoing==2, ThreadState.Stopped==4, ThreadState.Aborted= 8 and ThreadState.Running = 0 . These are not the real values, just using them to make the example easier. Look at ThreadState for the real values.

So in binary:
Thread.Unstarted == 0001
Thread.WaitSleepJoing == 0010
Thread.Stopped == 0100

When you or these values together you get 0111.

Now when we and 0111 with ThreadState.Aborted (1000) you get 0000 which is ThreadState.Running.

So what the author is basically trying to show you here is that ThreadState is a bit vector and you have to construct a mask to see which values are set.

Karthik Ramachandran
  • 11,925
  • 10
  • 45
  • 53