Hi all,
I was debugging an issue we're seeing and noticed some strange behavior from tbb::concurrent_vector. I was hoping someone could tell me whether or not its expected.
The bottom line is that after a push_back() call actually completes, the size() of the vector does not reflect this. I've narrowed it down to be capacity related because if I capture capacity() and size(), size() == capacity() which leads me to believe that size() is returning the capacity() and not the actual number of elements.
I have created a simple program that duplicates this issue, which triggers most often when the vector is empty. For simplicity, this program simply ASSERTs that size() != 0 immediately after a push_back() call returns. I hope someone can please tell me if this is expected behavior or if its a bug.
-------------------------------------------
#include
#include
#include
#include
typedef tbb::concurrent_vector vec_type;
void *invokePushBack(void * vector)
{
vec_type *vec = (vec_type *)vector;
for(int i = 0 ; i < 10 ; ++i)
{
vec_type::iterator it = vec->push_back(1);
assert(vec->size() != 0);
}
pthread_exit(NULL);
}
int main()
{
int rc;
pthread_t tg[5];
// note: the race condition doesn't always trigger,
// so we loop until it does.
while(true)
{
vec_type vec;
for(int i = 0 ; i < 5 ; ++i)
{
rc = pthread_create(&tg[i], NULL, invokePushBack, &vec);
if (rc != 0)
{
std::cerr << "pthread_create failed: " << strerror(errno) << std::endl;
}
}
for (int i = 0 ; i < 5 ; ++i)
pthread_join(tg[i], NULL);
}
}
-------------------------------------- I compiled with the command: g++ test.cpp -ltbb Thanks! Jared




