Thread Data Race | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Thread Data Race

Hi I have a thread pool which has 12 threads. I have a function which is used as a thread function for all these twelve threads. Last argument is output parameter which gets filled by thread function. Thread function is something as below: void myFunc(int a,int b,int c,vector<int>& out); out vector is filled by this function. vector<thread> vThreads; vThreads.resize(12); vector<vector<int>> resVals(12); for (int iCurThread = 0; iCurThread < intThreadCnt; ++iCurThread) { vThreads.at(iCurThread) = thread(&myFunc,1,2,3, ref(resVals[iCurThread])); } for (std::thread& active_thread : vThreads) { active_thread.join(); } Is it right that there is no data race in this case even though I have not used mutex? I think no data race as each thread is modifying specific vector from vector of vector. Right?

3rd Nov 2022, 4:57 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Answers
+ 1
I dont think that happen some data race because you are using a distinct vector for thread and the data is an native type as int (no shared obects then)
3rd Nov 2022, 10:31 PM
KrOW
KrOW - avatar
0
Yeah vector is different for each thread ... should not be a data race. But I am confused as each vector passed to a thread is a member of same vector of vector. Main container is same and specified idexed vector is passed to thread function
4th Nov 2022, 3:34 AM
Ketan Lalcheta
Ketan Lalcheta - avatar