Tag: static

  • C++ Static Class Member

    a static member in a class is like a shared resource that all objects of that class can use together. Imagine you have a group of people who all need access to a single file. To make sure they don’t interfere with each other, you set up a single “lock” that everyone can see. If one person sees that the lock is open, they can go ahead and use the file, but they immediately close the lock so no one else can get in until they’re done. Once they finish, they open the lock again so the next person can access it.

    In programming, we use a static variable for this purpose. This variable belongs to the class as a whole, not to any specific object, meaning everyone shares the same lock. For instance, in the fileProc class, the static isLocked variable keeps track of whether the file is in use. If it’s false, an object can proceed to use the file and set isLocked to true, blocking others. When finished, it resets isLocked to false, letting others know they can use it. This way, everyone plays fair and avoids conflicts.

    Here’s how the concept works in a simple example. We’ll define a class called fileProc with a static isLocked variable that acts like a shared lock. This lock ensures that only one instance of fileProc can use the file at a time. Here’s the code:

    #include <iostream>
    #include <thread>
    #include <chrono>
    
    class fileProc {
        FILE *p;  // File pointer
        static bool isLocked;  // Shared lock variable
    
    public:
        // Function to check the lock status
        bool isLockedStatus() const {
            return isLocked;
        }
    
        // Function to access the file if it’s unlocked
        bool accessFile() {
            if (!isLocked) {  // If the file isn't locked, proceed
                isLocked = true;  // Lock the file
                std::cout << "File is now locked by this instance.\n";
    
                // Simulate file processing time
                std::this_thread::sleep_for(std::chrono::seconds(2));
    
                isLocked = false;  // Unlock the file when done
                std::cout << "File has been unlocked by this instance.\n";
                return true;
            } else {
                std::cout << "File is already locked by another instance.\n";
                return false;
            }
        }
    };
    
    // Define and initialize the static member outside the class
    bool fileProc::isLocked = false;
    
    int main() {
        fileProc file1, file2;
    
        // Attempt to access the file from two different instances
        if (file1.accessFile()) {
            std::cout << "File accessed successfully by file1.\n";
        }
    
        if (file2.accessFile()) {
            std::cout << "File accessed successfully by file2.\n";
        }
    
        return 0;
    }

    Explanation:

    • Static Variable (isLocked): isLocked is declared as static inside the fileProc class, meaning it’s shared by all instances.
    • Checking Lock Status: Each instance checks isLocked. If it’s false, the file is available, so the instance sets isLocked to true and “locks” the file.
    • Unlocking the File: After processing, the instance resets isLocked to false, making the file available again.

    Output:

    Since the program waits for 2 seconds to simulate file processing, the second instance will try to access the file while it’s locked by the first instance and will display an appropriate message:

    File is now locked by this instance.
    File is already locked by another instance.
    File has been unlocked by this instance.
    File accessed successfully by file1.

    This example demonstrates how a static variable lets all instances know the file’s current status, ensuring no two objects use the file at the same time.