Prince Of Persia: The Original Game

It is said some lives are linked across time..

Connected by an ancient calling that echoes through the ages…

Destiny.

This is the passage that is shown at the beginning of the movie “Prince of Persia, Sands of Time”. It is a great beginning for a fantastic film.

For the game, as it is said by the author, “… One of the most successful and enduring video game franchises of all time, with over 17 million games sold to date…” (http://jordanmechner.com/bio/).

My Prince of Persia adventure started with its 2D game on the DOS machines. The music was great then and is great now. Eastern breezes flew upon you when you listened it… And then, when you acquainted with the story, you liked it more. For me, it was the best entertainment ever.

Then came the 3D version from UBI Soft, The Sands of Times. Great experience and again great music. it was the only game I took pleasure playing it over and over. Others always bored me after some time. As a software engineer, it gave purpose to do on the coming years. I did a gaming software in the following years. But still the taste of it, I remember…

The story is interesting…

It first came out for an Apple II computer, that is also interesting.

The source code of the first game is now on github (https://github.com/jmechner/Prince-of-Persia-Apple-II). The original author made it open source. It is written in assembly language for Apple II computer. As it is written on the page, they have worked out to save it from years old floppy disks. Thank you!

From some YouTube videos (http://jordanmechner.com/ebook/), you can see the making of the first game. They were using actual videos for the movements.

Thank you guys for creating (or being a reason for creating) such a good journey…

Hadoop – 2 : Map/Reduce

M/R (Map/Reduce) model is the name of two distinct feature mostly found in the functional programming languages. Map is the process of applying a mass function to each member of a list. After that, resulting list contains the processed items which may or may not have the same number of elements. Reduce on the other hand takes this map input and combines them with recursive means.

The most interesting part of these methods is they are very much suitable for parallel operations. Let us dive into why they are….

The Best Way of Synchronization

Today most of the computation revolves around parallelization techniques. From the time when the processing power stopped increasing, the manufacturers started putting two of the same processing engine to the same box. After that, forcibly, streamlined parallel processing age has begun. Whether this parallel processing was in a single box or be distributed across the planet, what it brings is the change of mentality in programming. Previously, all a programmer had to do was to design the software with a single processing engine in mind. Why not? Even the compilers until that time were optimizing their code in this way. But when more power is needed you had to dive into the parallel processing paradigm and, man, it was hard. When doing parallel processing, you must be very careful synchronizing your code. Whether the problem at hand was the producer/consumer problem or it was the dining philosophers’ problem, synchronizing was the most important part. Also grasping the structures was not an easy job for everyone.

Now, let us say, one knows everything about the synchronization and applies to the design, but this time, he realizes that, synchronization comes with a price: it is slow. The whole idea of parallelization is to make things fast, but when doing parallelization, synchronization puts burdens and makes your job harder. Hardware supported techniques also did not help too much. It was time to think the best way of doing it and it was clear: To get the most out of hardware, you should avoid using synchronization. And there it was, the best way of parallel computation was not using synchronization techniques at all.

So, is it possible? Yes, it is possible, but it will depend on your problem.

Let us think a producer/consumer problem. Let us have an object reading a file line by line and then another object processing those lines and outputting to another file.  When doing this, we have two ways:

The first way: the consumer is greedy and is taking a line whenever it is ready. In this method, the consumer object can use a list-like structure the producer provides and checks its count property and whenever an item is ready, it will take it from there. The list itself must be synchronized for parallel processing.

The second way: The consumer waits until the number of lines reaches to a certain count and then start taking it without draining it. If the count is lower than this count than it will sleep. If the producer indicates that it has finished (this happens once in the object lifetime so no way to mess up), the consumer could drain the source and then finish.

In the first way, synchronizing makes things slow. To that, if the objects reside in different processes or in different machines it will make things worse. Because inter-process communication is slow, dangerous and more expensive.

In the second way, no synchronization is required and apart from the initial time, no waiting required; perfect for the long running operations.

M/R works in much the same manner of the second way. It does not use any synchronization techniques (of course, when you have many threads working at the same time, for other purposes you can use them, but for the main job, processing the data, this will be in a minimum level). First it divides the problem with mapping stages and then “reduce” stage takes these mapped values and process it whenever some input is ready. Initial waiting times put some burden on the operation but the overall time will always be better for long running operations.

Hadoop – 1 : What is it?

Hadoop is an answer to distributed computing with massive data.

BRIEF HISTORY

When Google started with its operations and got good results out of it [1] [2], others started to investigate same thing to be done. Because working with data as big as web required some radical methods and current methods stayed short for.

A web search engine was being developed by Doug Cutting and his colleagues, called Nutch [3], in their spare times, in 2002. They made it work with a couple computers but to scale it to web it should have worked with thousands of computers. They soon realized this was not a project to be done with so few resources.

In 2003, Google, successful in this kind of work, released a paper about their GFS (Google File System) [1]. Their system was working on commodity servers with thousands of nodes. They could also scale those easily. With this file system, managing massive data could be operated easily. Doug Cutting and his team, with in the next year, could be able to have a working file system like GFS. They have called it NDFS (Nutch Distributed File System).

In 2004, Google again published a paper on MapReduce [2], their implementation of distributed computing. In 2005, Doug Cutting and his team already had an implementation of MapReduce. Most of the routines of the Nutch converted to NDFS and MapReduce.

Later on, Doug Cutting Joined Yahoo! and the resources provided for the project provided the team the big leaps. In 2008, Yahoo! announced that it had 10.000-core Hadoop clusters [5].

After Some times around that, the big hype around Hadoop started.

With Java… Really?

Aside from the fact that, Java made massive progress, both in systems integration and in performance issues, some would not still believe the kind of massive data is processed with it. As it came out, the bottleneck in the massive data processing was the seek time, not the speed of the computers, not the amount of the RAM available. As it is put in [5], the seek time of the hard drives is improving more slowly than their transfer rate. In this case, Java makes perfect sense;

  • first it is an open sourced project for the contribution to be made from all over the world.
  • Also the companies other than Microsoft first helped it shape and
  • For Java is one of the most advanced general programming languages.
  • Java is also platform independent without any suspicion like the Mono system.
  • Also Java has a wide programmer stock and Hadoop would need a gigantic number of map and reduce classes to be implemented.

With these and with many other reasons, I think, it made it to be what it is now.

Why not use Databases (RDBMS: Relational database management systems)

It is always misleading if we think about the Hadoop as a RDBMs like system. It is true that the Hadoop ecosystems are equipped with database like systems, but what Hadoop and its ecosystem really is a distributed computing processing engine. There are some similarities though, as you can query in both systems with SQL like languages, but I will talk about the differences in the coming words.

Seek Time

As previously mentioned, seek time is improving more slowly than transfer rate. That is to say, if you are making more seeks you will be slower. But if you are making less seeks and transferring more, you get more work done. This is the reason why Hadoop is more capable in big data than the RDBMs.  In fact, that is the design philosophy of the B-Tree structures: Index the records in a tree and keep the disk pointers in RAM and when needed, just seek to the place in the hard drive to read the records. As it can be seen, seek time is very important in RDBMSs.

Update Process

RDBMSs are more capable of updating a small sets set of data. In that they are very fast, if the set of the data increases, they become short in that. This is how it is done in RDBMS

  1. 1-Seek to one record,
  2. 2-update the record
  • or make the record deleted,
  • insert another one
    • for that go to another place – another seek
    • insert the record there and get the location info and update the index.

As it can be seen, there can be one or many seek. Of course, that depends on the operation but the general idea is as it is defined.

Structure Based Incrimination

RDBMS requires a rigid structure on the data to process it. Also, data duplication is strictly forbidden; from this normalization was born. But as the data size starts growing, normalization does not help the performance, it puts more seeks on the data to be traversed. So, the denormalization process is to be taken to make the process more performing. But this is thought to be the last resort in RDBMSs [6].

On the other hand, Hadoop is more relaxed on the structure of the data. It can take any format, any type of file to be processed. For example the big log files are good example of the data to be processed in Hadoop.

Scaling

When more power is needed in RDBMSs, the answer to that is more powerful computers to do the job. But in Hadoop, it is only adding more commodity servers to the cluster that are sometimes one hundred times cheaper.

RDBMs vs. Hadoop

Also, all of this does not mean that, the two systems are not capable of integrating both ideas. As Tom White indicates [7], the gap between them will slowly disappear. Oracle and many other companies already started working on it [8].

What about Grid Computing or Volunteer Computing? Don’t they do the same job?

Grid or volunteer computing is more about compute intensive jobs, rather than disk intensive jobs. In disk based jobs, Hadoop is the answer.

What about the network Delays?

Hadoop simply runs on the locality principle. That is to say, the data and the processing part of the systems should be on the same nodes or near to each other. With that, the power of the local area networks could be exploited. This simple principle and the ideas on the disk seeking give its power to Hadoop.

What about the Cloud, is it the same or different?

Everybody (or at least anybody that took network based classes or read about network structures) must have already seen the cloud picture representing the internet. Cloud is the internet and Hadoop brings the ability processing huge amount of data on the cloud, meaning with servers on all over the world, keeping above mentioned principles, of course. Amazon S3 etc. systems are all cloud based systems.

I want to write in languages other than Java, is it possible?

Of course, Hadoop provides means of writing map and reduce functions in languages other than Java. Also, functional languages like OCaml or F# is best suited for the jobs at hand [10] [11] [12]. I will be mentioning them in the coming articles.

What about ETL (Extract Transform Load) possibilities in Hadoop?

There exist many ETL processor engines and many more implemented by private companies for in house use. As a person who spent most of its professional life in ETL included projects, if I need to say something about the ETL, it would be “Hadoop is the future of ETL“. With small data, others would prevail or at least continue to stay alive, but in processing gigantic data, they will slowly fade away.

The Hadoop Ecosystem

Hadoop is an Apache top project. In that there are a dozen of other projects that collectively called Apache Hadoop project. In the next article, I will go over each one of them shortly.

 

References:

 

1-GFS: Google File System.

Abstract:http://research.google.com/archive/gfs.html

PDF:http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en//archive/gfs-sosp2003.pdf

2-MapReduce:

Abstract:http://research.google.com/archive/mapreduce.html

PDF:http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en//archive/mapreduce-osdi04.pdf

Slides:http://research.google.com/archive/mapreduce-osdi04-slides/index.html

3-Nutch: http://nutch.apache.org/

4-Hadoop The Definitive Guide 2nd Edition, Chapter 1 Meet Hadoop, page 5, Tom O’Reilly, White

5-Hadoop The Definitive Guide 2nd Edition, Chapter 1 Meet Hadoop, page 10, Tom O’Reilly, White

6-Date, C.J. Database in Depth: Relational Theory for Practitioners. O’Reilly (2005), p. 152.

7-Hadoop The Definitive Guide 2nd Edition, Chapter 1 Meet Hadoop, page 6, Tom O’Reilly, White

8-Oracle Big Data, http://www.oracle.com/us/technologies/big-data/index.html

9-Apache Hadoop, http://hadoop.apache.org/

10-Functinal languages and Hadoop, http://stackoverflow.com/questions/6087834/how-scalable-is-mapreduce-in-the-original-functional-languages

11-Making Hadoop more “functional” with Scala ,http://heuristic-fencepost.blogspot.com/2010/11/making-hadoop-more-functional-with.html

12-Parallel and Distributed Computational Intelligence,  Francisco Fernández de Vega, pg.14, “Hadoop built upon map and reduce primitives present in functional languages”.

 

“volatile” – 3: Java

What java brought to the programming languages was extraordinary. Not that it was the first to implement it, it was the first implementing it in a way that revolutionized the way for other programming languages.

If we say java is only a programming language, it would be wrong. In fact it is a platform does many things an operating system does. User interface, security, executable discovery, file system and many more libraries are defined in itself. In that platform memory management and different programming languages are supported.

One can say that java has a machine language for itself, it won’t be wrong. But different from its predecessors, it started as an interpreting platform (anyways, what real hardware does is, in fact, interpreting the machine language, but in a much more faster way, nothing more), later advancements made it close to real machine execution (JIT itself is a miraculous invention).

From all these mumbo-jumbo, what java brings is better productivity and platform independency. Independency is copy paste independency not preprocessing helped recompilation. Take your code to another platform and voila! It will work.

This explanation may not come as an extraordinary invention to the contemporary engineers; it is a game changing establishment in the history of computer science.

From all of this, if original inventors of java have omitted some features in the mist of the revolution, this, i think, is not only forgivable, it is also a clear indication that humans are capable of doing such things, so also you can manage such implementation.

What it was like before

Now that we have said this, the original implementation of “volatile” in java is implemented in a way off [1] [2]. One would expect a volatile variable should behave as a memory barrier, but the first implementation did not provide this. What original implementation provided was only cache invalidation. Code reordering still was possible.

In fact, this was what java platform behaved like. Java was a bad boy when reordering for performance was needed. No matter what, it would have rearranged that [3] [4]. This is in fact is influenced from the optimizations for single threaded applications. Java first developed in a world of single threaded applications and in that time, it was mostly OK. But as the time passed, as the multithreaded applications surfaced, engineers started to find out oddities in the execution and later it become clear that even the famous double checking lock was not guaranteed in java in those days. Only synchronization that would definitely work was “synchronized” keyword. If you did that, it would work else do not even try. Whatever you would do, in some scenario it would fail.

What it has become

But everything changed in the last months of 2004 when the new java version of 2 (J2SE 5.0) came out [5]. “volatile” started to behave as it should be; A memory barrier and a cache invalidator.

Java is very keen on the backward compatibility. For example, when implementing generics, because of these concerns, generics did not become runtime templates but c style compile time templates [6]. Some intricacies of generics come from this fact and the famous “sometype<?>” notation owes its existence to this fact. “volatile” on the other hand is an exception that brakes this. It has been changed from one version to the other.

This is the story of volatile in Java, in a nutshell.

References

  1. Java theory and practice: Fixing the Java Memory Model, Part 1, http://www.ibm.com/developerworks/java/library/j-jtp02244/index.html
  2. Java theory and practice: Fixing the Java Memory Model, Part 2, http://www.ibm.com/developerworks/library/j-jtp03304/
  3. Double-checked locking: Clever, but broken, http://www.javaworld.com/javaworld/jw-02-2001/jw-0209-double.html
  4. Can double-checked locking be fixed?, http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-double.html
  5. JSR 133: Java™ Memory Model and Thread Specification Revision, http://jcp.org/en/jsr/detail?id=133
  6. Learning Java Third Edition 2005 (O’Reilly) by Patrick Niemeyer & Jonathan Knudsen, Chapter 8: Generics, Sections: “There Is No Spoon”/Erasure.

“volatile” – 2: Memory Models

Whenever a talk begins about the “volatile” constructs in programming languages, the chat goes all the way to “Memory Models“. Unless one is familiar with the concept of the memory models, it will be hard to grasp the concept of “volatile” inner workings.

It is already known that “volatile” is about the threading operations. As the name implies, it is about volatile variables that may be subject to change between thread calls (in a nutshell this sentence describes the motivation behind it). But what it really is always behind the closed doors of compilers, CPUs, CPU caches, main memories etc. So what happens when a multithreaded code with shared variables runs or compiled?

Usually with higher level languages (especially that are first compiled to an intermediate byte code) developers need not to give much attention to these details. Because the platforms that will later compile this byte code to the native language of the machine in that the code runs (JIT Compilers, Native Image Generators [NGENs]) give assurance on the behavior of the underlying hardware and of the compiler that will compile the higher level language to byte code or to the machine code. This assurance is what we call the “Software Memory Model“. So a developer could write one code, and by the magic of the byte code, it will run on many processor architectures without fine tuning. On the software side, it is a contract between the developer and the software platform provider. “Hardware Memory Model”, on the other side, is an assurance from the hardware platform provider to the software platform provider for code execution in hardware.

As it can be seen, the concept of “memory model” is twofold. The first one is according to the developer point view. It is how a developer sees a memory model. The software platform provider is responsible to obey by it. On the hardware side, the second one, the manufacturers also define memory models for their processors. So it becomes a contract between the software platform provider and the hardware platform provider. Let us first go over hardware memory models and try to understand what they really are.

Hardware Memory Models

Strong Memory Models (older models of processors)

(This is something I have made up to draw the distinction between the memory models.) “Strong Memory model” name can be given to the memory models that were used before. The code that is given to the processor is executed as is. No reordering, no optimization, and no cache coherency (cache coherency: in multi-processed systems caches holding the copy of the same variable from the memory). What is written to the cache is directly will be available to other threads no matter in which processor they execute on (If a single processor system, which most of them were, then no worries on the cache coherency). What is read from the cache will be up-to-date one. So no optimization that will change the behavior of the code will occur.

Weak Memory Models (newer models of processors)

Weak memory model is an attempt to improve the performance of software [1]. In this model, CPU is given the freedom to reorder [3] or optimize the program sequences (that reside in memory) whenever it sees fit. If a reorder is at hand and if it will not break the real execution order of the code, then a reorder could be implemented. So, what this means is the processor is not giving any assurance on the order of code but in the overall execution. This is what it is called the “as-if-sequential” execution. The overall effect of the software would be the same and the user or the developer will see the execution as if completed in the same order. Also some other optimization of code could occur and the CPU will honor the optimization if it will not break the overall execution effect.

The Ups and Downs of the Both Systems

Strong memory model are easy to be developed in and has less performance characteristics. It is this feature that lead to the weak memory models. CPU would reorder or optimize the code if it sees some optimization is ahead.

The Hardware Structure That Gives Way to Race Conditions

For the sake of simplicity, we first assume that we have a system of one processor having only one level of cache system. Then we will talk about what happens when the system is made up with two of those processors.

In this system, cache always is up-to-date (if no mapped I/O operation occurs). Because there is only one cache then we cannot talk about cache coherency problems. The only problem that will surface will be the reordering operation in multithreaded applications. If a thread is reading a variable, it is seeing this from the cache value and because there is no other cache (or CPU) that will change this variable in the main memory, then the operations will be safe according to the last value of the variable. Multithreaded applications, in this type of systems, must take cautions on memory reordering problems, though. What can be done to overcome the memory ordering in these systems? Memory Barriers can be used to overcome this reordering. In that, volatile can be expensive but for the sake of portability for other platforms, they can be suitable

If the system is made of two processors and two caches (each one for a CPU), then a variable will have the danger of being manipulated by more than one thread executing on different CPUs. If a thread is executing on the CPU1 and writing to Cache1, and another thread is executing on CPU2 and writing on Cache2, the same variable could have two different values on two different caches at the same time. Of course, the cache value will be written to the main memory but the timing of it could lead to race conditions. The value may be changed long before another thread is trying to access the variable which it read from the local CPU cache. In this situation, cache coherency protocols [2] take the action for the consistency of the caches in a shared memory model. This consistency model can be applied in many ways to many processor architectures. For example, Intel x86 processors, writes are always volatile, meaning if you are writing to a variable, the cache will be invalidated and the value will be written to the memory at once.

Software Memory Models

The first language to define a software memory model was Java (It will be the subject of another article). Because Java made a promise to the developers that if you have written one code then this code will work in every platform Java runs on. But hardware platforms are different when it comes to memory management. Every platform had its own memory model. What Java did this time was to implement its own memory model which Java would promise to the developers that if you write code according to this memory model, it would work everywhere [4].  Later on, other programming languages implemented their own memory models, including C # [5].

 

Conclusion

Now that we have learned what a memory model is, then we are ready to go on the java concept of volatile and its bitter history. See you on the next article…

References

1-Weak Ordering – A New Definition

http://www.cs.utexas.edu/users/dburger/teaching/cs395t-s08/papers/13_weak.pdf

2-The Performance and Scalability of Distributed Shared Memory Cache Coherence Protocols, Chapter 2: Cache Coherence Protocols, Mark Heinrich Ph.D. Dissertation Computer Systems Laboratory Stanford University October 1998

http://www.csl.cornell.edu/~heinrich/dissertation/ChapterTwo.pdf

3-Chapter 8.2 Memory Ordering, Chapter 11 Cache Control, Intel® 64 and IA-32 Architectures Developer’s Manual: Vol. 3A

http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html

4- Chapter 17.4 Memory Model, the Java Language Specification Third Edition, James Gosling, Bill Joy, Guy Steele, Gilad Bracha

http://docs.oracle.com/javase/specs/jls/se5.0/jls3.pdf

5- Volatile keyword in C# – memory model explained

http://igoro.com/archive/volatile-keyword-in-c-memory-model-explained/

A Tribute to Tony Hoare

Tony Hoare (aka C.A.R. Hoare, age 78) is one the pioneer in the computer science field and the creator of the QuickSort algorithm. Also, he is the man behind the Z specification language, CSP (Communicating Sequential Processes) concurrent programming model (like dining philosophers problem) and Hoare logic (correctness of software).

He is also the creator of the Elliott ALGOL compiler (and implementation of ALGOL 60).

He currently works as a principle researcher (after decades of academic career) at Microsoft Research, Cambridge, England [1].

Here is a couple of quotes from him:

1-I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. In recent years, a number of program analysers like PREfix and PREfast in Microsoft have been used to check references, and give warnings if there is a risk they may be non-null. More recent programming languages like Spec# have introduced declarations for non-null references. This is the solution, which I rejected in 1965 [2].

2-“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult” [3].

3-“[About Algol 60] Here is a language so far ahead of its time, that it was not only an improvement on its predecessors, but also on nearly all its successors [4].

5-Science  vs. Engineering [5]

Science Engineering
long-term short-term
idealism compromise
certainty adequacy
perfection risk
generality specificity
separation amalgamation
unification diversity
originality best practise
formality intuition
correctness dependability

6-“concurrent programs wait faster”[6].

 

References:

1-http://research.microsoft.com/en-us/people/thoare/
2-http://qconlondon.com/london-2009/speaker/Tony+Hoare
3-The 1980 ACM Turing Award Lecture
4-http://web.eecs.umich.edu/~bchandra/courses/papers/Hoare_Hints.pdf
5-http://research.microsoft.com/en-us/people/thoare/scienceandengineering.pptx
6-http://research.microsoft.com/en-us/people/thoare/concurrentprogramswaitfaster.ppt

Plate Tectonics: Expanding Earth or Moving Plates?

Recently, in the History Channel there was a documentary named “How the Earth Was Made”. This documentary was about the movement of the earth’s crust and the explanations around the “Plate Tectonics” theory. It was a two season series that in each episode, looked at some mysterious or unexplained earth structures and tried to explain them with the “Plate Tectonics” theory.

It is an incredible subject. According to this theory, earth’s crust is made up from distinct plates. These plates are in motion. Imagine that the plates are moving, bumping to each other and creating mountains and changing the world climate.

Just watch the documentary and you will learn a lot from it, at least some explanations for the geography that seem unbelievable to the naked eye.

We can give examples of the movement and the effects of it. Plate tectonics explains the Himalaya Mountain Ranges with the moving plates. According to it, the Indian Subcontinent once was together with the African Plate and after they separated, it crushed to the Asian Plate. Two plates are now pressing each other. From this the Himalaya Mountains are born. The pressure still continues and the Everest is still rising up a couple inches a year.

It also explains some of the earthquakes and the creation of the volcanoes in the Belt of Fire or elsewhere. The concept of a hot point is a curious thing  which caused the creation of Hawaiian Islands.

According to the theory, what makes the plates move is the inner pressure of the molten rocks, the magma.

If you think that way, everything seem to fit perfectly.

But there is another theory. Instead of the moving plates, this theory tries to explain the changes in the earth’s crust with the Expanding Earth theory. According to it, the process of the changes are the result of the expanding earth.

Check out

Expanding Earth and the Pangaea Theory
http://www.youtube.com/watch?v=3HDb9Ijynfo

and

Plate Tectonics and the Expanding Earth
http://www.geologynet.com/tectonics1.htm

“volatile” – 1: C#

Volatile constructs are primitive thread synchronization constructs in the .Net platform. The main usage of them can be listed as:
  1. Disable the optimisation intended for single threaded applications, which most of the compilers assume. Meaning, they are flags for the compilers instructing them not to optimize the code according to the single threading.
  2. Disable caching in hardware, either in a thread variable or in a CPU register.
  3. Behave as a memory barrier construct (this will be explained later in this article).
1-Disabling the Optimisations
(to make the optimisations work, compile your code with “/platform:x86″  and “/optimize+” switches)
Take a look at the following code (taken from “CLR Via C#” 3rd Edition, Jeffrey Richter)
private static void OptimizedAway() { 
   // Constant expression is computed at compile time resulting in zero  
   Int32 value = (1 * 100) – (50 * 2);  
   // If value is 0, the loop never executes 
   for (Int32 x = 0; x < value; x++) {  
       // There is no need to compile the code in the loop since it can never execute 
      Console.WriteLine(“Jeff”); 
   } 
}
Compiler will see
Int32 value = (1 * 100) – (50 * 2);  
and will convert this to
Int32 value = 0;
and because the value is zero, then the “for” statement will not work, so the compiler will remove the code (optimize away).
There may be many situations like this. To tell the compiler no to implement this one must define the variable “value” “volatile“.
2-Disable the Caching
When a variable is declared to be volatile, when ever its value is read or written, it will be done via the main memory. So the caching mechanism will be disabled, i.e., the CPU registers. It will be the up-to-date fresh value from the real place it resides, the main memory. Because of this, volatile fields are slow in respect to other type of variable access.
3-Behave as a Memory Barrier
volatile” keyword, in C#, is a shorthand for Thread’s “VolatileWrite” and “VolatileRead” methods.
Compilers (even CPU) can order the execution of a program different than the one indicated by the programmer. This is OK in most situations or in a single threaded application. But in some situation, you need your codes to be executed in the same order you entered. This is where a memory barrier comes in to the picture. Let us see by examples:
[Wikipedia Sample]
Processor #1:
 while f == 0
  ;
 //A- Memory Barrier required here
 print x;
Processor #2:
 x = 42;
 //B- Memory Barrier required here
 f = 1;
if any of the operations are executed out of the order by the compiler or by the CPU, then the result will be quite unexpected. To disable this, memory barrier should be used.
In C# three constructs provides this feature and they are
public sealed class Thread {  
   public static  void  VolatileWrite(ref  Int32 address,  Int32 value);   
   public static  Int32 VolatileRead(ref  Int32 address);  
   public static  void  MemoryBarrier(); 
}
a-Thread.VolatileWrite
In the above example, if I write Thread.VolatileWrite in places indicated, I would ensure that, this code (Thread.VolatileWrite) will work in the place I intended to. And any operations that I wrote before will work before this code (Thread.VolatileWrite). But the operations written before does not necessarily must be in the same order I have written them.
b-Thread.VolatileRead
It is similar to write construct with only one difference. This is a read construct and the operations after that will execute after the code (Thread.VolatileRead). Still, Thread.VolatileRead will execute at the point where I intended it.
c-MemoryBarrier
It is somewhat the combination of the two above mentioned methods. It does not read or write any variables but it will execute at the same point the program intended and the calls before it will work before it and the calls after it will work after it.

“static” Keyword in Programming Languages

Many programming languages have integrated the notion of “static” into their nomenclature. Most of the “Object Oriented“ languages have integrated it as “class variable” (as opposed to instance variable).

Now, if we want to describe the notions of class and instance variables we would say a class variable is instantiated when first a type is referenced via the static members or when first instance is created (.Net and Java is like that) or type is loaded into the memory (C\C++ is like that). An instance variable, on the other hand, is created when any of the class constructors is called.

There is also the notion of “static class constructor“s. In .Net you would create one as:

sealed class MyObj
{
      static MyObj()
      {
            //Access static members and initiate
      }
}

In Java, this is accomplished with “static block“s. It is the same as its .Net counterpart, only the syntax is different.

final class MyObj
{
      static
      {
            //Access static members and initiate
      }
}

In C\C++, it is a bit different tough. “static” is either a “storage class” definition or is the same as above. If it modifies a variable in the top level (out side of any function call and any “class” definition) then the variable it defines will be visible to the file only. There no similarity to the “static” word in the other languages for this use. For example, below is a header file, named “myinclude.h”:

#ifndef _MYINCLUDE_H_
#define _MYINCLUDE_H_

static uint mynumber = 0;

#endif

In this file, “mynumber” will only be visible to this file and the files that include “myinclude.h”.

On the other hand, in C++, if a “static” variable is defined inside of a “class” then it is the same usage with C# or Java.

struct Request {
      static int count;
}