简体   繁体   中英

Pthread_create seems to flush the argument passed to the input function

I am working on a pthread code to do repeated matrix vector product. While doing so, I first wrote the serial matrix vector code for multiplication and then later I attempted to put the matrix vector product into separate threads.

The code https://github.com/viswans/parallel-computing-cs525/blob/pthread/pthread_page_rank/src/pthread/pagerankPthread.cpp does what I just described. Particularly when the number of threads is increased from 8 to 9, the binary results in a segmentation fault.

On debugging using gdb I noticed that there was a null pointer being dereferenced, and I added a watch point on that pointer to see if it is being set properly. What I noticed was that the argument to the function being called from pthread_create seems to be flushed and set to 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      

Any insight about why pthread_create would flush the arguments would be much appreciated. Thanks Sudharshan

You call push_back on the tstruct vector, which invalidates all pointers into that vector, causing the threads to access structures that have moved. One simple fix is to add tstruct.reserve(num_threads); after std::vector< ThreadStruct > tstruct; .

But you should really rethink this and do things in a more sensible way. Is a vector of structures a suitable collection to use when you need a pointer into the collection to remain valid as the collection is modified?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM