Why Atomic operations are not always thread safe

No comments

Sunday, September 21, 2008

A question I saw on StackOverflow:

Is accessing a bool field atomic in C#? In particular, do I need to put a lock around:

class Foo
private bool _bar;

//... in some function on any thread (or many threads)
_bar = true;

//... same for a read
if (_bar) { ... }

This question was answered quickly by several users that quoted C# spec, similar posts and the holy bible (ok maybe not the holy bible :) ).

The answer given was that reading or writing a boolean value is atomic (as well as other basic value types such as byte, int and so on).

Although the variable is atomic it does not mean that it's impossible to create a Racer condition while using it.


So How can I Create Racer Condition with an Atomic Operation?

Very Simple: All you have to do is use it in an if statement:

//Do Something

What If _bar value is altered by another thread while we're inside the curly brackets? obviously it depends.
In case we're planing on _bar value to remain true while we're inside the condition we can be sadly mistaken.

And so the atomic value is only "thread safe" per operation not for the whole application.

It's very easy to fix this - just put a lock around the if statement:

//Do Something

Synchronized Collections

Many .NET collection have a synchronized version that can be created by calling Synchronized(...) function.

Ex. to create a new synchronized ArrayList call ArrayList.Synchronized(new ArrayList()). To create a Sync Queue call Queue.Synchronized(new Queue()) and so on.

The synchronized collection is Thread safe per operation as well meaning that you can call each method from multiple threads without worrying that a race condition might occur.

However there is a problem in the following code:

if(queue.Count > 0)
var item = queue..Dequeue();
// do something

Can you guess what it is?

I think that it happen to me at least once in each computer I own or worked on: While running Windows (or Microsoft) update I get an error claiming that the update could not be installed. This used to be a source of frustration for me. I needed to Google around and try solutions and tips from the Internet - some would work and some not. Over the time I have found three solution that helped me fixed this annoying problem.

1. Add Windows Update to your trusted sites

This used to solve the problem in the past. What you need to do is add Windows update to the trusted sties.

This can be easily done by opening IE and going to Internet Options -> Security -> Trusted Sites -> Sites -> Add this Web site to the zone:

Uncheck "Require server verification" checkbox and add:



2. Install Service Pack 3

Download manually from http://www.microsoft.com/downloads/details.aspx?FamilyId=5B33B5A8-5E76-401F-BE08-1E1555D4F3D4&displaylang=en. after installing service pack 3 - the problem should be solved.

3. Re-installing Windows Update Agent (WUA)

This worked for me once.

  1. Download WindowsUpdateAgent20-x86.exe or WindowsUpdateAgent20-x64.exe.
  2. Run the command "WindowsUpdateAgent20-x86.exe /wuforce"
  3. Go to windows update site and try to update your computer.


I know that all of those tips can be found on the Internet but I wanted to have a single reference to what works for me. If you have other solution please feel free to post it in the comments.

SharpPDB - .NET Class that reads and writes palm OS files

No comments

Monday, September 01, 2008

I'm the proud owner of a Palm Tungsten E2 device. It's amazing how this old device is still much better then newer mobile windows devices. It has great battery life, quick response time and easy to use PIM.
And it seems I'm not only one that thinks so - you can read about it download squad - Pokin' to the oldies: why Palm OS 5 still rocks

About a year ego I needed to sync between an application I had on my PC to an application on my Palm device and so I've written a library in C# that can read and write pdb (palm database) files.

Like many other projects I forgot about it and left it half complete. A few days ago I stumbled upon it while searching my old C# projects directory and found it. And It seemed a shame to loose code I've worked on and that could benefit my follow coders.

Palm OS uses pdb files to store application related data. you can download the palm file format specification from here.

Project Overview

The PDB is combined from four logical part:

  1. Header that describe the file properties and attributes.
  2. Record list that defines the structure of the raw data.
  3. Raw data block.
  4. SortInfo and AppInfo blocks

In my code you can see the following:

  1. PDBFile - class that represents an actual pdb file.
  2. PDBHeader - has all header related information.
  3. RecordList - contains all record related information and a collection of RecordEntry that contains the raw data information.
  4. SortInfo & AppInfo - should have hold that information but I haven't implemented them yet.

I only properly implemented and tested reading from pdb file, writing pdb file is only partly implemented (and poorly tested) or as my calculus lecturer use to say when he didn't want to solve a problem - I'm leaving it as an exercise to the readers (you?).

How To Run

I've included a (very) simple winform project that has simple load/save functionality.


That's it - The source code is available here.

in case you have questions or improvements please leave a comment.

Related Posts Plugin for WordPress, Blogger...