+ 2

Thread on entire class object

Hi I have a class and it does some operation. For example, class has methods to open a file, read data from file , process data and complete a count of number of lines. All these are just a example of class and it has some variables and the member function. There is a for loop which calls method of this class and each time file provided is different. Can I do threading on this worrying about mutex and lock ? All class members are provided thread safety automatically if I don't call the method from one object ? I mean I call 5 objects of class and from each for loop iteration, I provide a each object to handle one one file . Is it ok not to worry about thread safety as object is totally different?

19th Mar 2022, 7:19 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
2 Respuestas
+ 1
It depends on shared resources. If several threads share one object there is a point to care about thread safety and you are recommended to add some locks (mutexes) in methods of class definition. If each thread has his own object and objects have not shared resources such as static members or file descriptors for example. Would be great if you attach a certain code example to discuss about.
9th Apr 2022, 9:39 AM
Дмитрий Золотов
Дмитрий Золотов - avatar
0
Thanks a lot... Not yet coded... Was planning to do performance improvement by threading But your input is very useful and can decide data members of class accordingly to avoid mutex and all
9th Apr 2022, 10:56 AM
Ketan Lalcheta
Ketan Lalcheta - avatar