Hello community,
I faced with bug in multithread environment in objective C code which using dispatch_async and block,
__sync_add_and_fetch increments global variable. But in case of many..many threads> 5, after every
__sync_add_and_fetch got damaged
...
int32_t count = 0;
...
int
main(int argc, char *argv)
{
for (i = 1; i < 32; ++i) {
...
char* name;
asprintf(&name, "test.overcommit.%d", i);
dispatch\_queue\_t queue = dispatch\_queue\_create\(name, NULL\);
free\(name\);
dispatch\_set\_target\_queue\(queue, dispatch\_get\_global\_queue\(0,
dispatch\_get\_global\_queue\(DISPATCH\_QUEUE\_PRIORITY\_LOW, 0\)\)\);
/\* async queue \*/
dispatch\_async\(queue, ^\{
\_\_sync\_add\_and\_fetch\(&count, 1\); //<< Here count is corrupted in case of number of threads> \~5
printf\("count addr %p, value %d\\n", &count, count\);
fflush\(stdout\);
\}\);
}
...
dispatch_main();
}
in case of count is local variable in scope of main function and has __block attribute, all is fine.
I'm using
clang version 3.3 (tags/RELEASE_33/final)
Target: armv7l-unknown-linux-gnueabi
Thread model: posix
libBlockRuntime 0.3
libdispatch for linux 1.2
CPU is ARMv7,
in disas I see dmb ish instruction, but I don't know is it enough.
I understand, my clang is out of date. Moving to new version could be painful )
Maybe somebody knows, was that bug fixed?
BR,
Alexey