Skip to content

Commit

Permalink
Added code to make pointer vector be thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
muthusaravanan3186 committed Feb 26, 2024
1 parent a93506c commit 9fc9884
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions Common++/header/PointerVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdint.h>
#include <vector>
#include <mutex>

/// @file

Expand Down Expand Up @@ -43,6 +44,7 @@ namespace pcpp
*/
~PointerVector()
{
std::unique_lock<std::mutex> lk(m_Mutex);
for (auto iter : m_Vector)
{
delete iter;
Expand All @@ -55,6 +57,7 @@ namespace pcpp
*/
PointerVector(const PointerVector& other)
{
std::unique_lock<std::mutex> lk(m_Mutex);
for (const auto iter : other)
{
T* objCopy = new T(*iter);
Expand All @@ -67,6 +70,7 @@ namespace pcpp
*/
void clear()
{
std::unique_lock<std::mutex> lk(m_Mutex);
for (auto iter : m_Vector)
{
delete iter;
Expand All @@ -78,31 +82,51 @@ namespace pcpp
/**
* Add a new (pointer to an) element to the vector
*/
void pushBack(T* element) { m_Vector.push_back(element); }
void pushBack(T* element)
{
std::unique_lock<std::mutex> lk(m_Mutex);
m_Vector.push_back(element);
}

/**
* Get the first element of the vector
* @return An iterator object pointing to the first element of the vector
*/
VectorIterator begin() { return m_Vector.begin(); }
VectorIterator begin()
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.begin();
}

/**
* Get the first element of a constant vector
* @return A const iterator object pointing to the first element of the vector
*/
ConstVectorIterator begin() const { return m_Vector.begin(); }
ConstVectorIterator begin() const
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.begin();
}

/**
* Get the last element of the vector
* @return An iterator object pointing to the last element of the vector
*/
VectorIterator end() { return m_Vector.end(); }
VectorIterator end()
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.end();
}

/**
* Get the last element of a constant vector
* @return A const iterator object pointing to the last element of the vector
*/
ConstVectorIterator end() const { return m_Vector.end(); }
ConstVectorIterator end() const
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.end();
}


//inline size_t size() { return m_Vector.size(); }
Expand All @@ -111,13 +135,21 @@ namespace pcpp
* Get number of elements in the vector
* @return The number of elements in the vector
*/
size_t size() const { return m_Vector.size(); }
size_t size() const
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.size();
}

/**
* Returns a pointer of the first element in the vector
* @return A pointer of the first element in the vector
*/
T* front() { return m_Vector.front(); }
T* front()
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.front();
}

/**
* Removes from the vector a single element (position). Once the element is erased, it's also freed
Expand All @@ -126,6 +158,7 @@ namespace pcpp
*/
VectorIterator erase(VectorIterator position)
{
std::unique_lock<std::mutex> lk(m_Mutex);
delete (*position);
return m_Vector.erase(position);
}
Expand All @@ -139,7 +172,10 @@ namespace pcpp
{
T* result = (*position);
VectorIterator tempPos = position;
tempPos = m_Vector.erase(tempPos);
{
std::unique_lock<std::mutex> lk(m_Mutex);
tempPos = m_Vector.erase(tempPos);
}
position = tempPos;
return result;
}
Expand All @@ -151,6 +187,7 @@ namespace pcpp
*/
T* at(int index)
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.at(index);
}

Expand All @@ -161,11 +198,13 @@ namespace pcpp
*/
const T* at(int index) const
{
std::unique_lock<std::mutex> lk(m_Mutex);
return m_Vector.at(index);
}

private:
std::vector<T*> m_Vector;
mutable std::mutex m_Mutex;
};

} // namespace pcpp

0 comments on commit 9fc9884

Please sign in to comment.