[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

__sync_add_and_fetch in objc block for global variable on ARM

From: Alexey Perevalov
Subject: __sync_add_and_fetch in objc block for global variable on ARM
Date: Wed, 8 Apr 2015 11:17:01 +0300

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;


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);
        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);


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?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]