Pthread_create似乎刷新传递给输入函数的参数

时间:2015-10-26 18:36:34

标签: c++ pthreads

我正在研究一个pthread代码来做重复的矩阵向量积。在这样做时,我首先编写了用于乘法的串行矩阵向量代码,然后我尝试将矩阵向量产品放入单独的线程中。

代码https://github.com/viswans/parallel-computing-cs525/blob/pthread/pthread_page_rank/src/pthread/pagerankPthread.cpp执行我刚才描述的内容。特别是当线程数从8增加到9时,二进制导致分段错误。

在使用gdb进行调试时,我注意到有一个空指针被解除引用,我在该指针上添加了一个监视点,看它是否正确设置。我注意到,从pthread_create调用的函数的参数似乎被刷新并设置为0!

Old value = 37843                                                                                                                                    
New value = 45242576                                                                                                                                 
0x0000000000403436 in __gnu_cxx::new_allocator<(anonymous namespace)::ThreadStruct>::construct<(anonymous namespace)::ThreadStruct, (anonymous namespace)::ThreadStruct> (this=0x2b25970, __p=0x2b260e0) at /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/ext/new_allocator.h:120                     
120             { ::new((void *)__p) _Up(std::forward<_Args>(__args)...);  }                                                                         
(gdb) c                                                                                                                                              
Continuing.                                                                                                                                          
[New Thread 0x7ffff2985700 (LWP 3390)]                                                                                                               
[New Thread 0x7ffff2184700 (LWP 3391)]                                                                                                               
[New Thread 0x7ffff1983700 (LWP 3392)]                                                                                                               
[New Thread 0x7ffff1182700 (LWP 3393)]                                                                                                               
Hardware watchpoint 3: *(0x2b260e8)                                                                                                                  

Old value = 45242576                                                                                                                                 
New value = 0                                                                                                                                        
0x00007ffff708eedb in __memset_sse2 () from /lib64/libc.so.6                                                                                         
(gdb) bt                                                                                                                                             
#0  0x00007ffff708eedb in __memset_sse2 () from /lib64/libc.so.6                                                                                     
#1  0x00007ffff7ded2e2 in allocate_dtv () from /lib64/ld-linux-x86-64.so.2                                                                           
#2  0x00007ffff7ded9be in _dl_allocate_tls () from /lib64/ld-linux-x86-64.so.2                                                                       
#3  0x00007ffff7bc9fc5 in pthread_create@@GLIBC_2.2.5 () from /lib64/libpthread.so.0                                                                 
#4  0x0000000000402b47 in PageRank::PageRankPthread::calculatePageRank (matrix=std::shared_ptr (count 1, weak 0) 0x2b258d0,�                         
    input=std::vector of length 196591, capacity 196591 = {...}, num_threads=9, criterion=...) at src/pthread/pagerankPthread.cpp:84                 
    #5  0x0000000000401d5d in mainPthread (argc=3, argv=0x7fffffffe6b8) at src/pthread/mainPthread.cpp:31                                            
    #6  0x000000000040be47 in main (argc=3, argv=0x7fffffffe6b8) at src/main.cpp:9      

有关pthread_create为什么会刷新参数的任何见解将非常感激。 谢谢 Sudharshan

1 个答案:

答案 0 :(得分:0)

您在push_back向量上调用tstruct,这会使指向该向量的所有指针无效,从而导致线程访问已移动的结构。一个简单的解决方法是在tstruct.reserve(num_threads);之后添加std::vector< ThreadStruct > tstruct;

但是你应该重新考虑这一点并以更明智的方式做事。当需要指向集合的指针以便在修改集合时保持有效时,结构向量是否适合使用?