Creating a Class that Uses Static Data to Track State of a thread - C++ Thread

C++ examples for Thread:Thread Creation

Description

Creating a Class that Uses Static Data to Track State of a thread

Demo Code

                                                                                           
#include <cstdlib>
#include <iostream>
#include <stack>
#include <thread>
#include <vector>
                                                                                           
using namespace std;
                                                                                           
class MyObject/*from  w w w .j  a v a  2  s .  c  o m*/
{
private:
    static const unsigned int MAX_OBJECTS{ 4 };
                                                                                           
    using MyCollection = vector < MyObject > ;
    static MyCollection s_ManagedObjects;
                                                                                           
    static stack<unsigned int> s_FreeList;
                                                                                           
    unsigned int m_Value{ 0xFFFFFFFF };
                                                                                           
public:
    MyObject() = default;
    MyObject(unsigned int value)
        : m_Value{ value }
    {
    }
                                                                                           
    void* operator new(size_t numBytes)
    {
        void* objectMemory{};
                                                                                           
        if (s_ManagedObjects.capacity() < MAX_OBJECTS)
        {
            s_ManagedObjects.reserve(MAX_OBJECTS);
        }
                                                                                           
        if (numBytes == sizeof(MyObject) &&
            s_ManagedObjects.size() < s_ManagedObjects.capacity())
        {
            unsigned int index{ 0xFFFFFFFF };
            if (s_FreeList.size() > 0)
            {
                index = s_FreeList.top();
                s_FreeList.pop();
            }
                                                                                           
            if (index == 0xFFFFFFFF)
            {
                s_ManagedObjects.push_back({});
                index = s_ManagedObjects.size() - 1;
            }
                                                                                           
            objectMemory = s_ManagedObjects.data() + index;
        }
        else
        {
            objectMemory = malloc(numBytes);
        }
                                                                                           
        return objectMemory;
    }
                                                                                           
    void operator delete(void* pMem)
    {
        const intptr_t index{
            (static_cast<MyObject*>(pMem) - s_ManagedObjects.data()) /
            static_cast<intptr_t>(sizeof(MyObject)) 
        };
        if (0 <= index && index < static_cast<intptr_t>(s_ManagedObjects.size())){
            s_FreeList.emplace(static_cast<unsigned int>(index));
        }else{
            free(pMem);
        }
    }
};
                                                                                           
MyObject::MyCollection MyObject::s_ManagedObjects{};
stack<unsigned int> MyObject::s_FreeList{};
                                                                                           
int main(int argc, char* argv[])
{
    cout << hex << showbase;
                                                                                           
    MyObject* pObject1{ new MyObject(1) };
                                                                                           
    cout << "pObject1: " << pObject1 << endl;
                                                                                           
    MyObject* pObject2{ new MyObject(2) };
                                                                                           
    cout << "pObject2: " << pObject2 << endl;
                                                                                           
    delete pObject1;
    pObject1 = nullptr;
                                                                                           
    MyObject* pObject3{ new MyObject(3) };
                                                                                           
    cout << "pObject3: " << pObject3 << endl;
                                                                                           
    pObject1 = new MyObject(4);
                                                                                           
    cout << "pObject1: " << pObject1 << endl;
                                                                                           
    delete pObject2;
    pObject2 = nullptr;
                                                                                           
    delete pObject3;
    pObject3 = nullptr;
                                                                                           
    delete pObject1;
    pObject1 = nullptr;
                                                                                           
    return 0;
}

Result


Related Tutorials