comments suggest edit

The path of least resistance when writing threading code as well as socket communications is to use techniques that cause indefinite blocking of some sort. Personally, I prefer never to block indefinitely. For example, it’s quite common to see code such as:

lock(someObject)
{
    //Do Something here...
}

Nothing wrong with this inherently, but this piece will try to acquire a lock on someObject indefinitely. Imagine if you mistakenly had code like (yes, it’s a bit contrived)

using System.Threading;

//... other stuff ...

object someObject = new Object();
object someOtherObject = new object();

public void LockItUp()
{
    lock(someObject)
    {
        Console.WriteLine("Lock 1 acquired.");

        ManualResetEvent mre = 
                new ManualResetEvent(false);
        WaitCallback callback = 
                new WaitCallback(ThrowAwayTheKey);        

        ThreadPool.QueueUserWorkItem(callback, mre);
        
        // wait till other thread has lock on 
        // someOtherObject
        mre.WaitOne(); 

        lock(someOtherObject)
        {
            Console.WriteLine("I never get called.");
        }
    }
}

void ThrowAwayTheKey(object resetEvent)
{
    lock(someOtherObject)
    {
        Console.WriteLine("Lock 2 acquired.");
        ManualResetEvent mre = 
                resetEvent as ManualResetEvent;
        if(mre != null)
            mre.Set(); //original thread can continue.

        lock(someObject)
        {
            Console.WriteLine("Neither do I");
        }
    }
}

Calling the method LockItUp will cause a deadlock and the application will hang until you kill it. Although this example is a bit contrived, you’d be surprised how easy it is in a sufficiently large and complicated system with multiple developers for you to run into this situation in a more roundabout manner. I see this often enough because using the lock statement is the path of least resistance. Instead, try using the TimedLock struct.

Another situation this type of thing comes up is with socket programming. Often I see code like this:

using System.Net.Sockets;

//... other stuff ...

byte[] _buffer = new byte[4096];
public void Listen(Socket socket)
{
    int bytesRead 
        = socket.Receive(_buffer, 0, 4096
                , SocketFlags.None);

    //You're sitting here all day.
}

If the remote socket isn’t forthcoming with that data, you’re going to be sitting there all day holding that thread open. In order to stop that socket, you’ll need another thread to call Shutdown or close on the socket. Contrast that with this approach:

byte[] _buffer = new byte[4096];
public void BeginListen(Socket socket)
{
    socket.BeginReceive(_buffer, 0, 4096
            , SocketFlags.None
            , new AsyncCallback(OnDataReceived)
            , socket);
    //returns immediately.
}

void OnDataReceived(IAsyncResult ar)
{
    Socket socket = ar.AsyncState as Socket;
    int bytesRead = socket.EndReceive(ar);
    
    //go on with your bad self...
}

BeginListen returns immediately and OnDataReceived isn’t called until there’s actual data to receive. An added benefit is that you’re not taking up a thread from the ThreadPool, but rather you’re using IO completion ports. IO Completion ports is a method Windows uses for asynchronous IO operations. When an asynchronous IO is complete, Windows will awaken and notify your thread. The IO operations run on a pool of kernel threads whose only task in life is to process I/O requests.

Since BeginListen returns immediately, you’re free to close the socket if no data is received after a certain time or in response to some other event. This may be a matter of preference, but this is a more elegant and scalable approach to sockets.

For more on asynchronous sockets, take the time to read Using an Asynchronous Server Socket and related articles.

comments suggest edit

This seems to be my favorite geek subject, but I have to tell you a success story using Ian Griffith’s Timed Lock struct with my enhancement.

To recap, when you fail to acquire a lock on an object because another thread already has one, my enhancement allows you to see the stack trace of the blocking thread. Well the other day, I was running a suite of unit tests against a socket server I was building when one of the tests failed with a ThreadTimeoutException. Looking at the stack trace, I found the line of code where another thread was unnecessarily holding a lock on the object. I used to spend a lot of time poring through logs trying to decipher threading issues such as this.

comments suggest edit

There are by some estimates more than a million weblogs. But most of them get no visibility in search engines. Only a few “A-List” blogs get into the top search engine results for a given topic, while the majority of blogs just don’t get noticed. The reason is that the smaller blogs don’t have enough links pointing to them. But this posting could solve that. Let’s help the smaller blogs get more visibility!

This posting is GoMeme 4.0. It is part of an experiment to see if we can create a blog posting that helps 1000’s of blogs get higher rankings in Google. So far we have tried 3 earlier variations. Our first test, GoMeme 1.0, spread to nearly 740 blogs in 2.5 days. This new version 4.0 is shorter, simpler, and fits more easily into your blog.

Why are we doing this? We want to help thousands of blogs get more visibility in Google and other search engines. How does it work? Just follow the instructions below to re-post this meme in your blog and add your URL to the end of the Path List below. As the meme spreads onwards from your blog, so will your URL. Later, when your blog is indexed by search engines, they will see the links pointing to your blog from all the downstream blogs that got this via you, which will cause them to rank your blog higher in search results. Everyone in the Path List below benefits in a similar way as this meme spreads. Try it!

Instructions: Just copy this entire post and paste it into your blog. Then add your URL to the end of the path list below, and pass it on! (Make sure you add your URLs as live links or HTML code to the Path List below.)

Path List\

  1. Minding the Planet\
  2. Luke Hutteman’s public virtual MemoryStream\
  3. JayBaz_MS blog\
  4. You’ve Been Haacked! and you like it.\
  5. (your URL goes here! But first, please copy this line and move it down to the next line for the next person).

\ (NOTE: Be sure you paste live links for the Path List or use HTML code.)\

[Via Luke Hutteman’s public virtual MemoryStream]

comments suggest edit

Dave Winer writes:

There’s something missing from Windows. An application that hooks into the outbound Internet message flow, and shows me where messages are going. This would allow me to figure out what spyware is running on my system even if the various utilities can’t get rid of them. Then the next step would be to allow me to block traffic to certain servers. That would disable the spyware. It seems that I should have control of my machine at that level.

And Jeff Sandquist responds that Windows XP SP2 can do this via the new firewall, but look again. The firewall built into XP blocks incoming traffic, not outgoing. My guess is that feature will come with SP3. For now, I recommend the free version of ZoneAlarm which is perennially considered by many to be the best desktop firewall software out there. It will notify you when an application tries to send an outgoing message to the internet and its quite easy to configure.

comments suggest edit

Having been on one of the most beautiful hikes during our trip to Seattle, it would be a grave disservice not to post a few pictures of the hike here. For all you Microsofties out there (and other geeks in Washington state), I highly recommend getting out of the office for this hike. Bring some water and sunscreen.

The four to five mile trail begins on the Pacific Crest Trail No. 2000 (the entire Pacific Crest Trail stretches from Canada to Mexico) from Chinook pass. At the parking area, you’ll cross Route 410 by walking on a wooden overpass seen above. The two in the picture below are the friends we visited, Dan and Grace Kalish.

Beginning of the hike \ Car careens off the road while hikers wave at camera.

After a bit of hiking, through lush green forest, we came upon the first of several lakelets. The hike has an elevation gain of just 700 feet, so it’s fairly easy, though we managed to make it a challenge by taking a wrong turn and visiting a full fledged lake. The detour set us back around another half hour.

A lake \ Dan wonders how those people in the lake got so small

Akumi and I had a plane to catch that evening and we were running out of time and getting a bit worried. It took us a bit longer than we expected to get out here, and the unexpected detour cost us even more time. At that point, we decided to play it safe and return back the way we came and not complete the loop. What a terrible decision that would have been. When we returned to the main loop, another hiker told us we were exactly halfway around the loop. That led to the obvious conclusion that we should continue. What a fortuitous decision, as this trail saves all the best views for those willing to stay for act 2.

Mt. Rainier \ You’ve seen one picture perfect lake surrounded by violet wildflowers in the woods with an enormous mountain backdrop, you’ve seen them all.

Phil has a snack \ Man loves nature. Man eats nature.

Not one to let a photographic opportunity go by, we made sure to pose for a group shot with our new friend, Mt. Rainier. As big as it looks in the photos, it looked absolutely ginormous in person. It positively encroaches on us as we take this shot.

Group Shot \ That cardboard backdrop is so damn convincing!

For most of the rest of the hike, we were treated to wonderful views of the mountain. It started to get rather blase. “Yet another view of the one of the most spectacular woodland scenes I’ve ever seen. Ho hum.”

Hiking along \ Tired of my incessant demands to stop and pose, my friends pose a mutiny.

Wildflowers \ Rainier tries to hide behind some trees.

At the end, the hike ended up being reasonably vigorous due to our detour, but quite manageable over all. The next time we hike here, we plan on making it a picnic next to the lake with the view of the mountain. I highly recommend this hike.

Directions From Seattle: via the Seattle Post Intelligencer\ Go south on Interstate 5 and take the state Route 18/Auburn exit (before Tacoma). Follow the signs to Auburn and state Route 164 (take the second Auburn exit off Highway 18, not the C Street exit). Follow 164 through Enumclaw to state Route 410 and turn left. Continue east to Chinook Pass, about 44 miles from Enumclaw. At Chinook Pass, continue a short distance past the wooden overpass to a parking area on the left side of Route 410 (the Mather Memorial Parkway). This is the parking area for the PCT with stock access and portable toilets, elevation 5,040 feet. If you are beginning from Tipsoo Lake, park at the Tipsoo Lake picnic area lot and look for the signed Naches Peak Loop trailhead opposite the lake.

comments suggest edit

Patrick Cauldwell points out that the challenge of coding a business app isn’t in writing the code, that’s easy. The challenge is in understanding business requirements, and not because developers and architects can’t understand plain English.

In my experience, I find that often, “business people” (for the lack of a better word) do not apply the same rigor to developing a business model for an application as developers do towards requirements gathering, specifications, and coding for the application.

I understand that agile methodologies are designed to help manage change, but even they can’t keep up when the business owner doesn’t even know what the requirements should be, but is spouting them out anyways. What do you do when the business owner has been involved in every step of the project, played with every prototype and interim release, but at the end says, well that’s not what I want.

For ~95% of business software, the actual code needed to implement them ends up being, if not trivial, then at least not technically challenging.  Business software projects go over budget/schedule etc. because the business rules aren’t known (to anyone) or because no one in the organization really knows what they want.  …snip…

So when people complain that projects are going way over budget/schedule/what have you, it’s almost always (in my experience) due to the fact that engineers spend 6 hours a day poring over vague and conflicting sets of documents, and 2 hours writing code.

[Via Patrick Cauldwell’s Blog]

As a developer friend of mine used to say,

This job would be great if we didn’t have clients.

Of course I prefer the paycheck.

comments suggest edit

I’m happily married, but if you’re not, and you live in L.A., I recommend you take your dog for a walk at the Barrington Dog Park, on the east side of Barrington just south of Sunset. We were playing a game of soccer, but we couldn’t help noticing that all the talent was in the dog park area adjacent to us. Looked like a bonafide LA pickup joint.

comments suggest edit

If you’re a lazy mofo like me, there’s one thing you’re not lazy about, getting out of having to do a lot of work. An important skill that should be on every lazy developer’s utility belt is the ability to understand requirements.

However, there’s a subtle technique to this. When I say, “understand requirements”, I don’t necessarily mean understanding what the client tells you the requirement is, but taking a moment to dig deeper and get to the real purpose of the requirement. Let me tell you a story based on a true story.

WeLikeCustomers.com has a two email systems for communicating to its clients. One system sends news letters and “Please come back” emails to clients who have opted in. The other system sends emails welcoming newly registered users as well as password reminders etc… The second system is designed to send only the most important emails so as not to get blacklisted.

Millard in Marketing decides that the second system should route emails through an email delivery service that guarantees delivery. He tells Damien the developer that only emails in a certain category should be sent through the new delivery service, and all others should be sent in the current manner.

Well Damien thinks about it and tells Millard it will take a day or so to add this routing within the second system. Currently the system only supports sending emails to one configured SMTP server. Now he has to add the ability to send some emails to another SMTP server based on certain criteria. He dives right into designing a general purpose email routing system. After a bit of time, he has the sudden thought,

“Now why the hell are they routing only a portion of emails to this new service anyways?”

So he asks Millard, who replies,

“To make it easier for you of course.”

After some discussion, Damien finds out that Millard would prefer to send all emails to the delivery service. So he changes one setting in his config file pointing to the service’s SMTP address, closes his office door, and takes a nap. Meanwhile, Millard is sending out emails complimenting Damien for going above and beyond the call of duty.

Be like Damien.

personal comments suggest edit

UPDATE: I cannot help you move to Alaska. This post was written in 2004. If you want to move to Alaska, please don’t contact me. Try looking for information at the official Alaska homepage. Thanks!

Alaska’s Flag As a relocated Alaskan, I read Rory’s post about moving Canada to the right a bit so that Alaska could join the “lower 48” with amusement. I don’t think that idea would go over too well with Alaskans, as we…er…they take great pride in their independence, not only in spirit, but in location. Not only that, it could affect the fishing season, and you don’t mess with the fishing season in Alaska, the biggest state in the Union. That’s right Texas, the stars at night are even bigger and brighter in Alaska. Get over it, we rule you.

Alaska Map However, Alaska is not paradise on Earth (except in the summer when the sun stays out quite late drinking and carousing). Did you know they pay residents to live there? It’s a little thing called the “Permanent Fund Dividend”. The Permanent Fund is a huge investment of oil revenues in a variety of stocks and bonds. Around October of every year, the interest earned from the fund pays each resident anywhere between 1 to 2 grand a year, depending on how well the market does. Just in time for some good Christmas shopping.

Not only that, there’s no sales tax and no income tax in Alaska. Yeah. Starting to sound like a good place to live, no? Well, before you rush off to go panning for gold, keep in mind that winter lasts around 8 to 9 months of the year, so I hope you like winter sports.

comments suggest edit

There ought to be a law that requires employers to give their charges the day off due to great weather. Back in Alaska, we’d have the day off when it dropped to -60 F with snow up to your nostrils. They call it a “snow day”. Well there ought to be a “good weather day”.

Not that I needed it, I’m on vacation fool! No, I ask for this for all you suckers trudging to work while I was kayaking away on Lake Union in Seattle. We paddled our way through a channel to Lake Washington and had fun limboing underneath the freeway. Yes, underneath. There’s a portion of the freeway directly over the lake, and portions of it are only three feet above the water. My wife and I nearly scraped our noses trying to glide underneath laying on our backs against the kayak.

Earlier today we cashed in my REI dividend buying my wife some nice sandals. Seattle has the nicest REI store I’ve ever seen, complete with a huge climbing wall. It made me nostalgic for the wilderness of Alaska. I had the sudden urge to climb mountains, wrestle bears and sleep in caves. Then again, L.A. has its own version of wilderness that’s pretty wild too.

comments suggest edit

twilight samuraiWe saw a fantastic movie at the Landmark last night called Twilight Samurai. It stars Hiroyuki Sanada who played the bad ass in The Last Samurai who beats the crap out of Tom Cruise and was the Samurai leader’s second in command.

What sets this movie apart from most Japanese Samurai movies is how vulnerable the hero is. He’s trying to lead a simple life and struggles to make ends meet. He is sensitive to his daughters and senile mother (his wife died of consumption) encouraging the daughters to study the classics (quite atypical) along with their needlework. He isn’t a typical bushido super-hero with perfect valor, but a petty Samurai who wants nothing more than to watch his daughters blossom into young women. As you might expect, it isn’t always that easy to maintain a simple life for a Samurai with duties.

This isn’t the movie for you if you’re looking for one gratuitious battle after another. The fighting is scarce, but significant to the story, well done, and have a realistic look to them. There are no crouching tigers nor hidden dragons here.

comments suggest edit

My friend Julie showed me and Akumi around the campus today. She works for the Finance department in Building 4. When I say “campus”, Microsoft really has a collegiate feel to it. We even saw a group sitting in a circle in a plot of grass having what appeared to be a discussion group. We had lunch at the cafeteria there and the food reminded me of my college dining hall, not terrible, but nothing to write blog about.

Seems like everyone we know in Seattle is trying to pimp it to us. Grace, whom we’re staying with, is constantly pointing out how wonderful it is while Julie is selling both Seattle and Microsoft. She certainly knows how to sell Microsoft to a person like me (not that I need any selling). After lunch, she immediately took us by the soccer field where two lunchtime games were going on a perfect turf field. Need I see more?

After walking around a bit, we headed over to the Microsoft Store and Museum. The store didn’t impress me too much, but the museum was neat, and not just because of the X-Box consoles. The old pictures of Bill and crew inspire a laugh and it was interesting to see the box for “Microsoft Adventure”, one of their first games.

After our impromptu tour was over, we sat in the lobby of building 4 when I noticed a group of people on the other side of the glass walk by. I noticed a guy in an orange XML Web Services shirt and told my wife that the huge book in my backpack that adds all the bulk to my luggage was written by him. I would’ve liked to have introduced myself to Chris, but we were already on the outside of the glass.

comments suggest edit

I’m here in Seattle hooking an unsuspecting Dan Kalish on the drug I call RSS Bandit. He just finished the Washington Bar (after having passed the Connecticut bar only a couple of years ago. Sucks to be a lawyer and move.) and is enjoying a quiet repose. This is my first attempt to a non geek on RSS.

comments suggest edit

We fly off tomorrow to Seattle and don’t let me catch you even thinking about trying to snake my stuff. I live in a very bad neighborhood, you wouldn’t want to be there after dark or in the nude. We have vicious blind dogs in the yard that have been raised on human meat and will bite anything that breathes or walks funny. And robots in the house with lasers and duct tape and … and Barry Manilow CDs!

And…um.. we have cameras all over the place, and a direct upload to America’s Funniest Home Videos so everyone can laugh at you when you’re attacked by blind dogs, including the girl in HR who never looks in your direction you pathetic slob.

Besides, my stereo is out-dated and sounds crappy. But I hear the neighbors just bought a huge flat panel HDTV with a Bang & Olufsen stereo. You should steal their shit instead.

comments suggest edit

I have a bad habit of writing specs in the future tense. Since the system I’m spec’ing doesn’t yet exist, it’s so easy for me to fall in the habit of saying things like:

  • This page will look like this:
  • At this point the system will do this.
  • The system will display that control here.

instead of:

  • This page looks like this:
  • At this point the system does this.
  • The system displays that control here.

So what’s the big deal? Well there’s two big deals. Number one is a question of written aesthetics and the other is a more practical consideration. Ok, so they’re not all that BIG a deal. But let me continue.

Aesthetically speaking, using the present tense sounds more active and interesting. Remember all those lessons about active voice and passive voice in high school English? I think it’s made me paranoid.

Secondly, and more importantly, is that after you build the system and someone comes along and refers to you spec as a piece of documentation, it sounds kinda funny to say the system “will look like this” when it already “does look like this”. You can argue that the system doesn’t respond till the user interacts with it so that saying “When the user clicks here, the system will navigate there” isn’t so off the mark. See my first point in response to that point. I thank you for your time.

comments suggest edit

I’m no professional musician, but I thought the acoustics at the Hollywood bowl was fantastic. They’ve been touting the improved sound with the new shell and improvements. The sound was sharp and clear.

The opening act played an Eastern influenced set with guitar, bongos, and even some Drum & Bass mixed in there. They were accompanied by Indian dancers on one side and break-dancers on the other. East meets west. I don’t believe for a second that the break dancers are from this world. I think they are former Gumby rejects in the way they contorted and flipped. Of course, I’ve seen just as impressive dancers on 3rd Street Promenade.

Sidestepper soothed us with “In Beats We Trust”, a very reggae-ish funky tune. Nortec Collective picked up the intensity a notch with some thumping music. The Crystal Method then brought it home with a nice selection of a few new tracks and several classics, ending with “Busy Child.”

comments suggest edit

Google originally wanted to raise $2,718,281,828, but based on this article, I predict they’ll be closer to $3,141,592,653. In any case, that’s a big piece of pi. (sorry. really. don’t shoot me.)

LOS ANGELES (Reuters) - Google Inc., the world’s No. 1 Web search provider, said on Monday its highly anticipated initial public offer could be worth as much as $3.3 billion as it prices its stock in a range that could value the company at more than $36 billion.

[Via Reuters: Top News]

comments suggest edit

Soccer ball My team, Nothend United, tied the Westsiders 2 to 2. Our goalie was phenomenal in staving off their aggresive attack, but sadly the opponents scored twice by capitalizing on deflections of our defense that took our goalie out of position. I scored one off of a penalty kick. For the record, I didn’t dive. I never do.