I have a standard vector and multiple threads. I'm using the following code to lock when it's required:
boost::mutex::scoped_lock lock(mutex);
This works properly, the application runs without any problems, but now I created a small class for the vector to make my life easier:
template <class T> class FVector
{
private:
std::vector<T> standard_vector;
mutable boost::mutex mutex;
public:
typedef typename std::vector<T>::iterator iterator;
typedef typename std::vector<T>::size_type size_type;
FVector(void)
{
}
iterator begin(void)
{
boost::mutex::scoped_lock lock(mutex);
return standard_vector.begin();
}
iterator end(void)
{
boost::mutex::scoped_lock lock(mutex);
return standard_vector.end();
}
void push_back(T & item)
{
boost::mutex::scoped_lock lock(mutex);
standard_vector.push_back(item);
}
void erase(iterator it)
{
boost::mutex::scoped_lock lock(mutex);
standard_vector.erase(it);
}
};
But unfortunately it doesn't work. I'm simply getting xx.exe has triggered a breakpoint. exception, which means there is something wrong with the locks and multiple threads trying to write and read in the same time.
I'm using the following code for testing:
#include <Windows.h>
#include <process.h>
#include "thread_safe_vector.h"
struct TValue
{
int value;
};
FVector<TValue> vec_Safe;
boost::mutex testMutex;
void thread2(void* pArg)
{
while (true)
{
//boost::mutex::scoped_lock lock(testMutex);
for (FVector<TValue>::iterator it = vec_Safe.begin(); it != vec_Safe.end(); it++)
{
if (it->value == 5)
{
vec_Safe.erase(it);
break;
}
}
}
}
void thread1(void* pArg)
{
while (true)
{
TValue value;
value.value = 5;
//boost::mutex::scoped_lock lock(testMutex);
vec_Safe.push_back(value);
}
}
void main(void)
{
HANDLE hThreads[50];
for (size_t i = 0; i < 50; i++)
{
hThreads[i] = (HANDLE)_beginthread(i % 2 == 0 ? thread1 : thread2, NULL, NULL);
}
system("pause");
for (size_t i = 0; i < 50; i++)
{
TerminateThread(hThreads[i], 0);
}
}
I'm absolutely out of ideas, I tried to figure out the problem for hours... Is there anything what I'm doing wrong?