Friday, November 18, 2005

ThreadStaticAttribute

I was experimenting with ThreadStaticAttribute.  A way to make available a new copy of class (note: not instance) variables to each of the threads running.

Static methods usually do not store state in class level variables for it would be shared by all other static methods too.  Unless marked ‘volatile’ two threads executing two different static methods on the same class could step on each other while assigning / reading from the variable.  Also, they get to share the copy of the state.  Now you could store state from a static method into “ThreadStatic” variables that only the same executing thread could see.  There’s a side effect though: If you happen to use a ThreadPool.  ThreadPool threads could be reused leading to state being visible when it isn’t supposed to be.  Well, you could always re-initialize before exiting.   The following example demonstrates that.

using System;
using System.Threading;

public class TestThreadStatic
{

     int completed = 0;
     public static void Main(string [] args)
     {
          TestThreadStatic tts = new TestThreadStatic();
          tts.StartTesting();
          while(tts.completed < 5)
          {
               Thread.Sleep(1000);
          }
     }


     public void StartTesting()
     {
          Console.WriteLine("Initial value of ticks at " +
" the beginning: {0}", ticks);
          for(int i = 0; i < 5; i++)
          {
               Console.WriteLine("Assigning work item {0}", i);
               ThreadPool.QueueUserWorkItem(ExecutedOnAThread, i);
          }

     }

     [ThreadStatic]
     private static long ticks;

     public void ExecutedOnAThread(object state)
     {
          string hashCode = Thread.CurrentThread.GetHashCode().ToString();
          //Initialize only once
          switch(ticks)
          {
               case 0:
                    Console.WriteLine("[{0}] A New Thread", hashCode);
                    ticks = -2;
                    break;
               case -1:
                    Console.WriteLine("[{0}] Thread reused", hashCode);
                    break;
               default:  // if ticks is -2
                    Console.WriteLine("[{0}] A supposedly new " +
                                   "thread is able to see "         +
"the old value {1}",
hashCode,
ticks);
                    break;
          }

          Console.WriteLine("[{0}] Ticks at starting: {1}"
, hashCode, ticks);
          ticks = DateTime.Now.Ticks;
          Console.WriteLine("[{0}] Ticks at End: {1}", hashCode, ticks);
          ticks = -1;

          //If sleep is reduced some of the threads would be reused
          Thread.Sleep(5000);
            completed++;
     }
}


Remember, the variable must be declared static.


Results (if running with a 5 second delay):


Initial value of ticks at the beginning: 0
Assigning work item 0
Assigning work item 1
Assigning work item 2
Assigning work item 3
Assigning work item 4
[3] A New Thread
[3] Ticks at starting: -2
[3] Ticks at End: 632691576162343750
[4] A New Thread
[4] Ticks at starting: -2
[4] Ticks at End: 632691576171406250
[5] A New Thread
[5] Ticks at starting: -2
[5] Ticks at End: 632691576176406250
[6] A New Thread
[6] Ticks at starting: -2
[6] Ticks at End: 632691576181406250
[7] A New Thread
[7] Ticks at starting: -2
[7] Ticks at End: 632691576186406250

If the delay is made 1 second:

Initial value of ticks at the beginning: 0
Assigning work item 0
Assigning work item 1
Assigning work item 2
Assigning work item 3
Assigning work item 4
[3] A New Thread
[3] Ticks at starting: -2
[3] Ticks at End: 632691595203906250
[3] Thread reused
[3] Ticks at starting: -1
[3] Ticks at End: 632691595213906250
[4] A New Thread
[4] Ticks at starting: -2
[4] Ticks at End: 632691595213906250
[5] A New Thread
[5] Ticks at starting: -2
[5] Ticks at End: 632691595218906250
[3] Thread reused
[3] Ticks at starting: -1
[3] Ticks at End: 632691595223906250

Thursday, November 17, 2005

Software as a Service

Is Microsoft really behind in its initiatives with “Windows Live”? Since I was there when the world – at least the Java guys - called Microsoft.Net initiative as “vaporware”, I suspect that it really might not. Though Microsoft has enough resources to buy its way into this futurescape, I feel it doesn’t have to. Windows Update is by far the most effective way to deliver OS Updates and is a feat not matched by any “practical” Operating Systems. Microsoft gets notified when a program on your machine crashes. Yes, it merely gets posted for the end-user with no apparent benefit. For Microsoft? It builds a database on possible failures that its Windows Updates can “evolve” to learn to anticipate and suggest “solutions”. Software-as-a-service is an idea, if I remember correctly, is something I read having propounded by Bill Gates about quite sometimes in the past. Now, yeah, Microsoft may be trailing its competitors in delivering on that promise. But don’t underestimate Microsoft. It may be a behemoth but when it moves, it does so with surprising speed. And, it has all the right things going for it. It has the software - Windows, IE, Windows Media Player, Windows CE, Windows Mobile - and partnerships - with Noika, Palm(!), AOL (Heard Microsoft is trying to buy into it?) and million other deals not disclosed to the mere mortals to do it for them.

The Machines & Us

I've been a big fan of Kevin Kelly, the founder editor of the Wired Magazine, for quite a while. Recently, when I was in US, I had a chance to buy his book Out Of Control which I had partially read it on his website - again a while ago, I guess. Even after 11 years after its publication, the book hasn't lost its charm. I think it will not for another 5-10 years at the least. His book is what I would call "a book of ideas" and a book where he mulls what's learnt and makes educated guess on what it will be. I ran a search on Google news, and found his latest article here It talks about a WebOS, the all knowing intelligence that our Net will be, similar to the AIs in Arthur Clark & Stephen Baxter - my favorite science fiction author(s). It's a fitting analogy to compare the human brain to the Net. But what's the analogy of consciousness will be? The consciousness, unbeknownst to itself, commands the brain to do what it wants. Now, all the computers sit there interlinked...and does the consciousness emerge itself as it does in human brains? I think, we have not understood the origin of consciousness yet, no? You just have the infrastructure and bingo, the consciousness emerges? I'm not too widely read to even fathom that question. But it's interesting indeed for me to come out of my blogging slumber and record it. I've been an application developer for the past 10 years from dBase, Clipper to now .Net. Yeah, what would an application developer know! But, I think, I've read and intuitively understand enough to realize that Network and software that evolves is the future. I can easily imagine what Kelly points to - A Web that learns from the actions of its users - prosumers. Right now, the web is a tangle of standards: CGI, PHP, ASPX and ASP etc. They are mostly designed and written to serve up what their masters are meant to. We have protocols on how information travels on the web and how the handshake and the further response of the Web Server (a piece of software that requires a PC with an Operating System). What shall push the Web to become the Ultimate Observer? I do not know. The infrastructure - consciousness - for that, I feel, is yet to emerge.