[Q] how to break a concurrent program without proper use of MB

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

 



Hi,
As mentioned in the perfbook, without proper use of memory barrier, concurrent
program will be error prone. For example, in this program:

    int a=0;
    int b=0;
    
    void* T1(void* dummy)
    {
        a = 1;
        b = 1;
        return NULL;
    }
    
    void* T2(void* dummy)
    {
        while(0 == b)
            ;
        assert(1 == a);
        return NULL;
    }
    
    int main()
    {
        pthread_t threads[2] = {PTHREAD_ONCE_INIT, PTHREAD_ONCE_INIT};

        pthread_create(&threads[0], NULL, T1, NULL);
        pthread_create(&threads[1], NULL, T2, NULL);

        pthread_join(threads[0], NULL);
        pthread_join(threads[1], NULL);

        return 0;
    }

there is chances that the assertion in T2 would fail, because there is no MB used
in the program.

However, after testing it so many times, the assertion never get throwed.

Adding a loop to increase the chance:

        for(int i=0; i< 500; i++){
            a = b = 0;
    
            pthread_create(&threads[0], NULL, T1, NULL);
            pthread_create(&threads[1], NULL, T2, NULL);
    
            pthread_join(threads[0], NULL);
            pthread_join(threads[1], NULL);
        }

the result is the same.

How can I make the assertion fail? Any trick?
(and I am using a X64 laptop)

Thanks,
Yubin
--
To unsubscribe from this list: send the line "unsubscribe perfbook" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux