Data race with detached pthread detected by valgrind

有些话、适合烂在心里 提交于 2020-03-18 07:26:10

问题


I am creating two detached 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;

}

With the above code valgrind (helgrind tool) shows data race:

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

Am I doing something wrong?

Note: It needs to be pthread only.

Info:

Compiling with (g++ 5.4.0)

g++ testmv.cpp -pthread

Testing with (valgrind 3.11.0):

valgrind  --tool=helgrind ./testmv

EDIT:

The above works with drd tool:

valgrind  --tool=drd ./testmv

May be I hit a bug in helgrind!

来源:https://stackoverflow.com/questions/42674043/data-race-with-detached-pthread-detected-by-valgrind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!