I've an simple array of string of character, and I'm trying to access and write to them through different multiple threads. Infact I've fixed size char[10]
each since I'll always have length of each string < 500. I know if I would access it, without modification in between, from different threads, there's going to be no problem with it.
The problem occurs when I'm writing to those strings from different threads and when I change some string at arr[2]
from different threads, the results are kind of unpredictable, since threads might be running ahead of one another and output is unpredicatble and often mix of those 2 strings. This is because the strings are composed of multiple characters and different threads are accessing different chars of same string at same time producing a mix of strings.
I've looked into mutexes and atomic variables. Mutexes require to lock the variable and then write the string and then unlock it. Would this be a good approach to solving the problem? I tried to understand the atomic approach but it went straight over my mind :/
Another question would be, if the array was composed of real atomic operations like an array of bool, where I would just have to set true or false to each indices, would this problem occur there too? Since there are no multiple characters, the problem shouldn't be there, right?
Aucun commentaire:
Enregistrer un commentaire