Atomicity and Atomic operations

Atomic Operation What is an atomic operation? An idea of atomic operation helps in understanding reentrancy, critical section, thread safety, synchronization primitives, etc… (we will have upcoming articles on each). Atomicity, Atomic Operation: In simple terms, atomicity is unbreakability, i.e. an uninterrupted operation. If two users issue a print command, each print should go in single attempt. If the printer driver is sending parts of data from two users, the printout will not be as expected. [Read More]

Can two threads call a synchronized method and normal method at the same time?

Problem You are given a class with synchronized method A, and a normal method C. If you have two threads in one instance of a program, can they call A at the same time? Can they call A and C at the same time? Solution: Java provides two ways to achieve synchronization: synchronized method and synchronized statement. Synchronized method: Methods of a class which need to be synchronized are declared with “synchronized” keyword. [Read More]

Scheduling method calls in sequence

Problem Suppose we have the following code: class Foo { public: A(.....); // If A is called, a new thread will be created // and the corresponding function will be executed. B(.....); // same as above C(.....); // same as above } Foo f; f.A(.....); f.B(.....); f.C(.....); i) Can you design a mechanism to make sure that B is executed after A, and C is executed after B? ii) Suppose we have the following code to use class Foo We do not know how the threads will be scheduled in the OS: [Read More]

Thread safe and exception safe singleton design pattern

Problem Implement a singleton design pattern as a template such that, for any given class Foo, you can call Singleton::instance() and get a pointer to an instance of a singleton of type Foo Assume the existence of a class Lock which has acquire() and release() methods How could you make your implementation thread safe and exception safe? Solution Here is the code in cpp: using namespace std; // Place holder for thread synchronization lock class Lock { public: Lock() { // placeholder code to create the lock } ~Lock() { // placeholder code to deallocate the lock } void AcquireLock() { // placeholder to acquire the lock } void ReleaseLock() { // placeholder to release the lock } }; // Singleton class with a method that creates a new instance // of the \* class of the type of the passed in template // if it does not already exist. [Read More]

How to measure the time spent in a context switch

Problem How can you measure the time spent in a context switch? Solution This is a tricky question, but let’s start with a possible solution. A context switch is the time spent switching between two processes (e.g., bringing a waiting process into execution and sending an executing process into waiting/terminated state). i.e. it is the computing process of storing and restoring the state (context) of a CPU so that execution can be resumed from the same point at a later time. [Read More]