HAARP, which stands for High-frequency Active Auroral Research Program, is a research facility located in Gakona, Alaska. Its primary purpose is to study the ionosphere, the uppermost layer of Earth's atmosphere. The ionosphere is ionized by radiation from the Sun, and it plays an important role in radio communications and navigation.
Tuesday, February 13, 2024
Are we heading for a doomsday - HAARP - the earthquake weapon of USA...
Sunday, February 4, 2024
jthread in C++ 20 concurrency - a wrapper around std::thread...
jthread is a new entrant in C++ 20. It's a simple wrapper around std::thread which is based upon RAII objective - that is resource acquisition is resource initialization.
The j in the jthread stands for automatic joining.
This means that there is no need to call join on the newly created thread object - the program won't terminate abnormally.
When a jthread object goes out of scope or is otherwise destroyed, it automatically calls join() to ensure the thread completes before the object is destroyed. This prevents potential resource leaks or undefined behavior that could occur with std::thread.
The other aspect of jthread is the cooperative interruption.
We will discuss it in the next post.
Saturday, January 20, 2024
The Barrier in C++ 20 concurrency - the programmer in me is still thriving...
Enjoy my training video on the C++ barrier...
Suppose three workers prepare data at different speeds, but a computation must begin only after all workers are ready. How do we synchronize them?
std::barrier is the answer in C++.
The std::barrier class is a synchronization primitive introduced in C++20. It allows a set of threads to synchronize at a certain point in their execution. It is similar to the std::latch class, but it can be reused multiple times.
A std::barrier object is initialized with a count, which specifies the number of threads that must reach the barrier before any of them can proceed. When a thread reaches the barrier, it calls the wait() method. If the count is not yet zero, the thread will be blocked until the count reaches zero. Once the count reaches zero, all of the threads that are waiting on the barrier will be released and can proceed.
The std::barrier class can be used to implement a variety of synchronization patterns, such as producer-consumer queues, parallel algorithms, and race condition prevention.
Here's my application in which I used barrier to showcase how it can be used.
Class Student
#include <iostream>
#include <string>
#include <thread>
#include <barrier>
#include <chrono>
#include <vector>
#include <syncstream> // For thread-safe console output
#include <format> // For C++20 string formatting
class Student {
private:
std::string name;
int timeLapseBeforeStarting; // in milliseconds
int timeToFinish; // in milliseconds
public:
// Optimized constructor using std::move to avoid redundant copies
Student(std::string studentName, int lapse, int finish)
: name(std::move(studentName)),
timeLapseBeforeStarting(lapse),
timeToFinish(finish) {}
// Virtual destructor is fine if subclassing, but defaulted here
virtual ~Student() = default;
void task(std::barrier<>& b) {
// 1. Simulate varying arrival times
std::this_thread::sleep_for(std::chrono::milliseconds(timeLapseBeforeStarting));
// 2. Synchronize at the barrier
b.arrive_and_wait();
auto now = std::chrono::system_clock::now();
// Locate the current system timezone and convert UTC to local time
auto local_time = std::chrono::current_zone()->to_local(now);
// std::osyncstream guarantees this block of output won't interleave with other threads
std::osyncstream(std::cout)
<< name << " is Starting the task at "
<< std::format("{:%F %T}", local_time) << "\n";
// 4. Simulate performing the task
std::this_thread::sleep_for(std::chrono::milliseconds(timeToFinish));
std::osyncstream(std::cout) << name << " finished the task.\n";
}
};
// ==========================================
// Class classETC
// ==========================================
#include "Student.h"
class classETC {
public:
Student ridit {"Ridit", 1000, 2000};
Student ishan {"Ishan", 3000, 1000};
Student rajdeep {"Rajdeep", 900, 1500};
classETC() = default;
virtual ~classETC() = default;
// FIX: Returns a container of jthreads to main() to keep them alive
auto giveTaskToStudent(std::barrier<>& b) {
std::vector<std::jthread> workers;
workers.reserve(3);
// Emplace threads directly into the vector
workers.emplace_back(&Student::task, &this->ridit, std::ref(b));
workers.emplace_back(&Student::task, &this->ishan, std::ref(b));
workers.emplace_back(&Student::task, &this->rajdeep, std::ref(b));
return workers; // Named Return Value Optimization (NRVO) handles this cleanly
}
};
#include <iostream>
#include <string>
#include <thread>
#include <barrier>
#include <chrono>
#include <vector>
#include <syncstream> // For thread-safe console output
#include <format> // For C++20 string formatting
class Student {
private:
std::string name;
int timeLapseBeforeStarting; // in milliseconds
int timeToFinish; // in milliseconds
public:
// Optimized constructor using std::move to avoid redundant copies
Student(std::string studentName, int lapse, int finish)
: name(std::move(studentName)),
timeLapseBeforeStarting(lapse),
timeToFinish(finish) {}
// Virtual destructor is fine if subclassing, but defaulted here
virtual ~Student() = default;
void task(std::barrier<>& b) {
// 1. Simulate varying arrival times
std::this_thread::sleep_for(std::chrono::milliseconds(timeLapseBeforeStarting));
// 2. Synchronize at the barrier
b.arrive_and_wait();
auto now = std::chrono::system_clock::now();
// Locate the current system timezone and convert UTC to local time
auto local_time = std::chrono::current_zone()->to_local(now);
// std::osyncstream guarantees this block of output won't interleave with other threads
std::osyncstream(std::cout)
<< name << " is Starting the task at "
<< std::format("{:%F %T}", local_time) << "\n";
// 4. Simulate performing the task
std::this_thread::sleep_for(std::chrono::milliseconds(timeToFinish));
std::osyncstream(std::cout) << name << " finished the task.\n";
}
};
// ==========================================
// Class classETC
// ==========================================
#include "Student.h"
class classETC {
public:
Student ridit {"Ridit", 1000, 2000};
Student ishan {"Ishan", 3000, 1000};
Student rajdeep {"Rajdeep", 900, 1500};
classETC() = default;
virtual ~classETC() = default;
// FIX: Returns a container of jthreads to main() to keep them alive
auto giveTaskToStudent(std::barrier<>& b) {
std::vector<std::jthread> workers;
workers.reserve(3);
// Emplace threads directly into the vector
workers.emplace_back(&Student::task, &this->ridit, std::ref(b));
workers.emplace_back(&Student::task, &this->ishan, std::ref(b));
workers.emplace_back(&Student::task, &this->rajdeep, std::ref(b));
return workers; // Named Return Value Optimization (NRVO) handles this cleanly
}
};
The Main method
// ==========================================
// Main Method
// ==========================================
#include "classETC.h"
int main() {
// Print C++ standard validation
std::cout << "C++ Version: ";
if (__cplusplus == 202101L) std::cout << "C++23\n";
else if (__cplusplus == 202002L) std::cout << "C++20\n";
else std::cout << "Other/Experimental (" << __cplusplus << ")\n";
std::cout << "-------------------------------------------\n";
std::barrier b(3);
classETC etc;
// FIX: Capturing the threads in main() preserves their lifetimes.
// They will execute concurrently and automatically join when main() ends.
auto threads = etc.giveTaskToStudent(b);
return 0;
}
// ==========================================
// Main Method
// ==========================================
#include "classETC.h"
int main() {
// Print C++ standard validation
std::cout << "C++ Version: ";
if (__cplusplus == 202101L) std::cout << "C++23\n";
else if (__cplusplus == 202002L) std::cout << "C++20\n";
else std::cout << "Other/Experimental (" << __cplusplus << ")\n";
std::cout << "-------------------------------------------\n";
std::barrier b(3);
classETC etc;
// FIX: Capturing the threads in main() preserves their lifetimes.
// They will execute concurrently and automatically join when main() ends.
auto threads = etc.giveTaskToStudent(b);
return 0;
}
The heart of the above concurrent program is
b.arrive_and_wait();
arrive() decrements the participant count.
wait() blocks until the count reaches zero.
arrive_and_wait() combines both.
The Output:
C++ Version: C++20
-------------------------------------------
Ridit is Starting the task at 2026-06-26 00:19:59.803930207
Ishan is Starting the task at 2026-06-26 00:19:59.803930524
Rajdeep is Starting the task at 2026-06-26 00:19:59.803929510
Ishan finished the task.
Rajdeep finished the task.
Ridit finished the task.
Have a look at the time when the three different threads are starting - all of them start at the same time - 00:19:59 - why?
It's because of the barrier!!!
And for the inquisitive mind...
Why is there a mismatch in the starting time in the range of just a few microseconds?
A variation of 1 to 20 microseconds is incredibly tight and proves that our std::barrier system is doing its job perfectly. In a multi-threaded system running on top of a general-purpose operating system (like Linux/Ubuntu), achieving true 0.000000 synchronicity is physically impossible due to these hardware and scheduling realities. To get any closer, we would need a hard Real-Time Operating System (RTOS) with core pinning and disabled interrupts!
Enjoy...
Friday, January 12, 2024
National Youth Day - here's is a clarion call to the engineers of Bharat - wake up and embrace Sanskrit...
Nikola Tesla and Swami Vivekananda
Here's why we must declare Sanskrit as the national language of Bharat...
Here's my wife Reema reciting her own poem on wisdom
Sunday, January 7, 2024
Latches in C++ 20 concurrency - just like the CountdownLatch of Java concurrency package...
Multithreaded programming is inherently difficult. One of the reasons is that we can't have control over how a thread will start and finish - in which order - it all depends upon the thread scheduling algorithm of the OS. This makes the reproduction of test cases difficult. Moreover, there are race conditions and deadlocks.
When I was teaching the Countdown latch - a thread synchronization technique used in the Java Concurrency package, there was none like that available in C++. I am happy to see that the concept of latch is introduced in C++20.
So...
What is a Latch in C++?
- A synchronization primitive was introduced in C++20.
- It allows one or more threads to wait for a certain number of operations to complete before proceeding.
- Acts like a countdown counter that blocks threads until it reaches zero.
/*
* Student.h
*
* Created on: Jan 7, 2024
* Author: som
*/
#ifndef STUDENT_H_
#define STUDENT_H_
#include <iostream>
#include <string>
#include <thread>
#include <latch>
#include <chrono>
#include <vector>
#include <syncstream> // For thread-safe console output
#include <format> // For C++20 string formatting
class Student {
private:
std::string name;
int timeToFinish;
public:
Student(std::string name, int finish)
: name(std::move(name)),
timeToFinish(finish) {}
virtual ~Student() = default;
void task(std::latch& l){
auto now = std::chrono::system_clock::now();
// Locate the current system timezone and convert UTC to local time
auto local_time = std::chrono::current_zone()->to_local(now);
std::osyncstream(std::cout)
<< name << " is Starting the task at "
<< std::format("{:%F %T}", local_time) << "\n";
// 4. Simulate performing the task
std::this_thread::sleep_for(std::chrono::milliseconds(timeToFinish));
std::osyncstream(std::cout) << name << " finished the task.\n";
l.count_down();
}
};
#endif /* STUDENT_H_ */
/*
* classETC.h
*
* Created on: Jan 7, 2024
* Author: som
*/
#ifndef CLASSETC_H_
#define CLASSETC_H_
#include "Student.h"
class classETC {
public:
Student ridit {"Ridit", 1000};
Student ishan {"Ishan", 3000};
Student rajdeep {"Rajdeep", 900};
classETC() = default;
virtual ~classETC() = default;
auto giveTaskToStudent(std::latch& l){
std::vector<std::jthread> workers;
workers.reserve(3);
// Emplace threads directly into the vector
workers.emplace_back(&Student::task, &this->ridit, std::ref(l));
workers.emplace_back(&Student::task, &this->ishan, std::ref(l));
workers.emplace_back(&Student::task, &this->rajdeep, std::ref(l));
std::cout<<"Teacher is waiting for all the students to finish their task"<<std::endl;
l.wait();
std::cout<<"All students submitted their task... Teacher is leaving the class"<<std::endl;
return workers; // Named Return Value Optimization (NRVO) handles this cleanly
}
};
#endif /* CLASSETC_H_ */
Main:
// ==========================================
// Main Method
// ==========================================
#include "classETC.h"
int main() {
// Print C++ standard validation
std::cout << "C++ Version: ";
if (__cplusplus == 202101L) std::cout << "C++23\n";
else if (__cplusplus == 202002L) std::cout << "C++20\n";
else std::cout << "Other/Experimental (" << __cplusplus << ")\n";
std::cout << "-------------------------------------------\n";
std::latch l(3);
classETC etc;
// FIX: Capturing the threads in main() preserves their lifetimes.
// They will execute concurrently and automatically join when main() ends.
auto threads = etc.giveTaskToStudent(l);
return 0;
}
Explanation of the code:
For your reference, here's my son Ridit on Java CountdownLatch.
#Enjoy
Wednesday, December 6, 2023
Why I taught my son Design Pattern even before Data Structure...
Let me give you an example.
You know callback mechanism, observer pattern, and event listener pattern - all do kind of the same work - notify some objects when a specific event occurs. However, without the knowledge of design patterns, we will simply try to understand this phenomenon from just a programmer's point of view - and definitely, we will miss the big picture behind writing such kinds of object-oriented code.
I remember, when I studied the command routing architecture of Visual C++/MFC source code, I tried to map it with the Gang of Four design patterns and found out that this architecture uses two common design patterns at the same time - Command pattern and Chain of responsibility pattern.
Studying design patterns helps to decipher object-oriented code more like a professional - and not like a novice programmer.
It helps to figure out the source code not only from what and how's point of view, but also from Why's point of view. We can explain why a piece of code has been or should be written in a particular way.
Understanding the Why's is very essential for a software developer.
Let me tell you about my journey in NOKIA India in 2007 using Symbian S60 C++ framework.
There is a concept in Symbian called a two-phase constructor. I asked people about the Why's point of view. Nobody gave me the answer - everybody said this is the norm in Symbian. While breaking my head about this "why", I went to Japan and then realized the reason for such a two-phase constructor - to avoid the exception during constructor in case of low memory. Because in the earlier days of mobile phones, there was limited memory and moreover there were no template concepts in C++. Hence there was no smart pointer. This led me to study Boost pointer in Japan in 2008 and voila, in 2009 Boost library became a part of the standard C++ library.
In concisely, to get the big picture of any object-oriented code structure, it's important to know Design Patterns. It clarifies many questions - particularly why a piece of code should be written in a specific manner.
Here's the evolutionary journey of Ridit, my son and a young Computer Scientist of Bharat - vis-a-vis Design Pattern...
My bragging right - as a Guru of my son...
State Pattern in Java : 8 yrs old
State pattern in C++ : 11 yrs old
State pattern in Python : 12 yrs old
import FreeCAD as App
# Create a new document
doc = App.newDocument()
# Create a composite container (e.g., Part::Compound or App::Part)
composite = doc.addObject("App::Part", "CompositePart")
# Create child objects
box = doc.addObject("Part::Box", "Box")
sphere = doc.addObject("Part::Sphere", "Sphere")
# Set positions for better visualization
box.Placement.Base = App.Vector(0, 0, 0)
sphere.Placement.Base = App.Vector(5, 0, 0)
# Add child objects to the composite container
composite.addObject(box)
composite.addObject(sphere)
# Recompute the document to reflect changes
doc.recompute()
print("Composite Part created with Box and Sphere as children.")
Thursday, November 23, 2023
A baby step in the wonderland of Quantum Computing...
I am of the opinion that there is no age limit for trying out new things in life.
I consider myself a perpetual student.
Today will be a memorable day for me.
I have started my journey into the world of Quantum Computing.
From almost no knowledge in college (even didn't know how to start the Basic development environment) to taking concrete steps to pick up Quantum Computing - it's a checkered journey - a lot of midnight oil is burnt - a lot of frustration, many times it became unbearable, traveled many places - but one thing always drove me - the intrinsic motivation - never did anything just for monetary gain - remained a perpetual student. I started my software journey when there was no Google. it was naturally challenging. but worth the effort...
So, here we go - my first coding in the area of Quantum Programming.
Here is the output
Output counts: {'1': 464, '0': 536}
And here goes another simple program of a basic entangler.
Enjoy...
Here goes the output of the above piece of code
Output counts: {'00': 489, '11': 511}
To start something absolutely new is challenging yet satisfying. I am always motivated by intrinsic motivation. Hence the joy of learning is still there in me, a perpetual student…




.png)