valgrind

时间:2017-03-08 14:30:30

标签: c++ multithreading pthreads

我正在创建两个分离的 pthread:

pthread_attr_t tha1;

void* fun1(void*){ return 0; }
void* fun2(void*){ return 0; }

int main(){
    pthread_t th1, th2;
    pthread_attr_init(&tha1); 
    pthread_attr_setdetachstate(&tha1, PTHREAD_CREATE_DETACHED);

    pthread_create( &th1, &tha1, fun1, 0);
    pthread_create( &th2, &tha1, fun2, 0);

    pthread_attr_destroy(&tha1);
    pthread_exit((void*) 0);
    return 0;

}

使用上面的代码valgrind(helgrind工具)显示数据竞争:

Possible data race during write of size 1 at 0x68C7677 by thread #1
==13976== Locks held: none
==13976==    at 0x4C3A3CC: mempcpy (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==13976==    by 0x401357F: _dl_allocate_tls_init (dl-tls.c:515)
==13976==    by 0x51C9CED: get_cached_stack (allocatestack.c:254)
==13976==    by 0x51C9CED: allocate_stack (allocatestack.c:501)
==13976==    by 0x51C9CED: pthread_create@@GLIBC_2.2.5 (pthread_create.c:539)
==13976==    by 0x4C34BB7: ??? (in /usr/lib/valgrind/vgpreload_helgrind-amd64-linux.so)
==13976==    by 0x400919: main (in /home/jahid/Git/Github/jpcre2/jpcre2/src/testmv)
==13976== 
==13976== This conflicts with a previous write of size 1 by thread #2
==13976== Locks held: none
==13976==    at 0x51C9622: start_thread (pthread_create.c:265)
==13976==    by 0x54E582C: clone (clone.S:109)
==13976==  Address 0x68c7677 is in a rw- anonymous segment

我做错了吗?

注意:它只需要是pthread。

的信息:

使用(g ++ 5.4.0)进行编译

g++ testmv.cpp -pthread

使用(valgrind 3.11.0)进行测试:

valgrind  --tool=helgrind ./testmv

修改

上述内容适用于drd工具:

valgrind  --tool=drd ./testmv

可能是我在helgrind中遇到了一个错误!

0 个答案:

没有答案