基礎_多線程
Q1 gdb調試多線程 如何解死鎖問題?
A1
gdb基本用法
- info threads(show all thread)
- thread thread number (switch )
- thread apply all break demo.cpp:42(all)
eg:
同一個功能A,創建N個線程
同一個功能B,創建M個線程
來搶奪和釋放資源C,D
不清楚那個線程 有限占用或者釋放資源
- [ ] 產生問題1 跟蹤那個線程ID
代碼實現順序實際執行順序是不一致的,
一般無法通過查看代碼快速定位
thread ID id用那個呀?
thread apply all break demo.cpp:19
thread apply all break demo.cpp:42
pthread_mutex_t mymutex
公共資源:兩個線程同時搶占
p mymutex
顯示了當前占用的線程(上圖)
當前目前線程位于5882 但是被
__owner = 5883鎖住
* 1 Thread 0x7ffff7fe1780 (LWP 5882。 2 Thread 0x7ffff6d6d700 (LWP 5883)
thread ID找到啦 5882
總結:
pthread_mutex_t.__data.__owner is a TID. pthread_t (frompthread_self()).
pthread_mutex_t 結構體
typedef union { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; #if __WORDSIZE == 64 unsigned int __nusers; #endif /* KIND must stay at this position in the structure to maintain binary compatibility. */ int __kind; #if __WORDSIZE == 64 int __spins; __pthread_list_t __list; # define __PTHREAD_MUTEX_HAVE_PREV 1 #else unsigned int __nusers; __extension__ union { int __spins; __pthread_slist_t __list; }; #endif } __data; char __size[__SIZEOF_PTHREAD_MUTEX_T]; long int __align; } pthread_mutex_t;
- [ ] 產生問題2
gdb默認調試當前主線程
thread apply all command 表示
all 所有線程中相應的行上設置斷點
你發現一個問題
調試期間(next)不斷的不同線程來回切換,
(如果誰發現不是麻煩告知)
線程是cpu調度的最小單位 因為分片原因
cpu不斷在不同線程之間切換
注意不是進程進程可以理解為一個主線程
set scheduler-locking on 只調試當前線程
- [ ] 產生問題3
如果進程有fork 如何辦?
If you need to debug the child process, after the start gdb:
(Gdb) set follow-fork-mode child off
查詢正在調試的進程:info inferiors
切換調試的進程:inferior id
如何分析思路
- 不用gdb:
假如100個線程 此時10個線程因為資源問題產生了死鎖
gdb調試會影響業務
可通過日志或者其他方式打印超時鎖
然后pstack +進程ID 查看堆棧信息
用gdb
1 通過gcore或者gdb -p方式 進入
2 設置斷點 thread apply all commd
等待一段時間產生死鎖
3 p pthread_mutex_t
確定目前那個線程占用
至少2個 一個不會產生死鎖 加鎖順序其他工具
Valgrind 的 helgrind
工具也可以檢測死鎖。
用法:
valgrind --tool=helgrind ./deadlock
http://valgrind.org/docs/manual/hg-manual.html
給出了詳細的例子和說明
#include <pthread.h>
int var = 0;
void* child_fn ( void* arg ) {
var++; /* Unprotected relative to parent */ /* this is line 6 */
return NULL;
}
int main ( void ) {
pthread_t child;
pthread_create(&child, NULL, child_fn, NULL);
var++; /* Unprotected relative to child */ /* this is line 13 */
pthread_join(child, NULL);
return 0;
}
變量var沒有加鎖
==7066== Possible data race during read of size 4 at 0x601040 by thread #1
==7066== Locks held: none
==7066== at 0x4006C1: main (lock.cpp:13)
==7066==
==7066== This conflicts with a previous write of size 4 by thread #2
==7066== Locks held: none
==7066== at 0x400691: child_fn(void*) (lock.cpp:6)
==7066== by 0x4C3094E: mythread_wrapper (hg_intercepts.c:389)
==7066== by 0x50B2DF4: start_thread (in /usr/lib64/libpthread-2.17.so)
==7066== by 0x5BDD1AC: clone (in /usr/lib64/libc-2.17.so)
==7066== Address 0x601040 is 0 bytes inside data symbol "var"
參考
http://www.cnblogs.com/zhuyp1015/p/3618863.html kill -11不可取 用gcore
http://blog.csdn.net/pbymw8iwm/article/details/7876797
pthread_mutex_t struct: What does lock stand for
http://stackoverflow.com/questions/23449508/pthread-mutex-t-struct-what-does-lock-stand-for
Understanding deadlock behavior with gdb
http://stackoverflow.com/questions/21017794/understanding-deadlock-behavior-with-gdb
Helgrind: a thread error detector
http://valgrind.org/docs/manual/hg-manual.html