Here is the code in that 2 threads write to a shared vector (to different items in the vector).
I know std::vector is not thread safe, but the 2 threads are writing to different items. I am not sure how vector is implemented. Could there be any potential race condition?
Code:
void* foo0(void* vec)
{
vector<int>* arr = (vector<int>*)vec ;
int i = (int)(rand()*10);
arr->at(0) = i;
}
void* foo1(void* vec)
{
vector<int>* arr = (vector<int>*)vec ;
int i = (int)(rand()*10);
arr->at(1) = i;
}
int main()
{
vector<int> vec;
vec.push_back(0);
vec.push_back(0);
pthread_t t1, t2;
pthread_create(&t1, NULL, foo1, (void*)&vec);
pthread_create(&t1, NULL, foo2, (void*)&vec);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}