Without seeing the precise code that is creating the locks and accessing the 
variables, it is hard to be certain that it is in fact safe.  Can you reduce it 
to a minimal sample?  Are you using only POSIX pthread primitives, or properly 
describing the lock primitives you are using?  If you can create a small sample 
with no proprietary code that exhibits the problems you are seeing, someone 
with more experience using Helgrind (or one of the tool's authors) can comment 
on it.

I attached the source code and helgrind log file. Thanks.
#include <stdio.h>
#include <pthread.h>

class Data {
    unsigned int the_red:16;
    unsigned int the_black:16;

    static pthread_mutex_t  the_mutex;
    static void lock() {
        pthread_mutex_lock(&the_mutex);
    }
    static void unlock() {
        pthread_mutex_unlock(&the_mutex);
    }

public:
    int red() {
        return the_red;
    }
    void red(int r) {
        the_red = r;
    }
    int black() {
        return the_black;
    }
    void black(int b) {
        the_black = b;
    }

    Data() {
        the_red = 0;
        the_black = 0;
    }

    static void *thread_worker(void *);
    static void init() {
        pthread_mutex_init(&the_mutex, 0);
    }
    static void cleanup() {
        pthread_mutex_destroy(&the_mutex);
    }
};

pthread_mutex_t  Data::the_mutex;
// only read red, and write black
void *
Data::thread_worker(void *in_data)
{
    const int max = 10000;
    Data *data = (Data *)in_data;
    int sum = 0;
    int red = data->red();
    for (int i = 0; i < max; i++) {
        lock();
        if (red % 2) {
            data->black(data->black() + 1);
        } else {
            data->black(data->black() + 2);
        }
        unlock();
        red = data->red() + i;
    }

    lock();
    printf("RED = %d BLACK = %d\n", data->red(), data->black());
    unlock();

    return 0;
}

int main()
{
    Data::init();
    const int num_thread = 4;
    pthread_t thread[num_thread];
    pthread_attr_t attr[num_thread];;
    Data data;

    for (int i = 0; i < num_thread; i++) {
        pthread_attr_init(attr + i);
        pthread_create(thread + i, attr + i, Data::thread_worker, &data);
    }
    for (int i = 0; i < num_thread; i++) {
        pthread_join(thread[i], 0);
        pthread_attr_destroy(attr + i);
    }

    Data::cleanup();
    return 0;
}

Attachment: helgrind26240.log
Description: helgrind26240.log

------------------------------------------------------------------------------
Own the Future-Intel&reg; Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest.
Compete for recognition, cash, and the chance to get your game 
on Steam. $5K grand prize plus 10 genre and skill prizes. 
Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to