大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void *thread_proc(void *data)
{
printf("a new thread creaded\n");
while (1)
{
sleep(10);
}
return (void *)0;
}
int main()
{
pthread_t tid;
pthread_create(&tid, NULL, thread_proc, NULL);
pthread_join(tid, NULL);
return 0;
}
#0 0x0000003c0f2a6a8d in nanosleep () from /lib64/libc.so.6
#1 0x0000003c0f2a6900 in sleep () from /lib64/libc.so.6
#2 0x0000000000400624 in thread_proc ()
#3 0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0
#4 0x0000003c0f2e153d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f7c78fc4700 (LWP 3197)):
#0 0x0000003c0f60803d in pthread_join () from /lib64/libpthread.so.0
#1 0x000000000040065a in main ()
17 backtrace="bt"
18 if test -d /proc/$1/task ; then
19 # Newer kernel; has a task/ directory.
20 if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
21 backtrace="thread apply all bt"
22 fi
23 elif test -f /proc/$1/maps ; then
24 # Older kernel; go by it loading libpthread.
25 if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
26 backtrace="thread apply all bt"
27 fi
28 fi
38 # Run GDB, strip out unwanted noise.
39 $GDB --quiet $readnever -nx /proc/$1/exe $1 <<EOF 2>&1 |
40 set width 0
41 set height 0
42 set pagination no
43 $backtrace
44 EOF
45 /bin/sed -n \
46 -e 's/^\((gdb) \)*//' \
47 -e '/^#/p' \
48 -e '/^Thread/p'
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex_1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_2 = PTHREAD_MUTEX_INITIALIZER;
void *thread1_proc(void *data)
{
pthread_mutex_lock(&mutex_1);
sleep(1);
pthread_mutex_lock(&mutex_2);
pthread_mutex_unlock(&mutex_2);
pthread_mutex_unlock(&mutex_1);
return (void *)0;
}
void *thread2_proc(void *data)
{
pthread_mutex_lock(&mutex_2);
sleep(1);
pthread_mutex_lock(&mutex_1);
pthread_mutex_unlock(&mutex_1);
pthread_mutex_unlock(&mutex_2);
return (void *)0;
}
int main()
{
pthread_t tid1, tid2;
pthread_create(&tid1, NULL, thread1_proc, NULL);
pthread_create(&tid2, NULL, thread2_proc, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
#0 0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x0000003c0f609345 in _L_lock_870 () from /lib64/libpthread.so.0
#2 0x0000003c0f609217 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x000000000040068e in thread1_proc ()
#4 0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0
#5 0x0000003c0f2e153d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f048938d710 (LWP 3617)):
#0 0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0
#1 0x0000003c0f609345 in _L_lock_870 () from /lib64/libpthread.so.0
#2 0x0000003c0f609217 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x00000000004006d3 in thread2_proc ()
#4 0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0
#5 0x0000003c0f2e153d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f0489d90700 (LWP 3615)):
#0 0x0000003c0f60803d in pthread_join () from /lib64/libpthread.so.0
#1 0x000000000040073d in main ()
2 Thread 0x7f645e122710 (LWP 3688) 0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0
* 1 Thread 0x7f645eb25700 (LWP 3686) 0x0000003c0f60803d in pthread_join () from /lib64/libpthread.so.0
#1 0x0000003c0f609345 in _L_lock_870 () from /lib64/libpthread.so.0
#2 0x0000003c0f609217 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3 0x00000000004006d3 in thread2_proc (data=0x0) at dead_lock.c:23
#4 0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0
#5 0x0000003c0f2e153d in clone () from /lib64/libc.so.6
23 pthread_mutex_lock(&mutex_1);
__owner = 3687, __nusers = 1, __kind = 0, __spins = 0, __list = {__prev = 0x0,
__next = 0x0}}, __size = “\002\000\000\000\000\000\000\000g\016\000\000\001”, ‘\000’ <repeats 26 times>, __align = 2}
# thread 3
$2 = {__data = {__lock = 2, __count = 0,
__owner = 3688, __nusers = 1, __kind = 0, __spins = 0, __list = {__prev = 0x0,
__next = 0x0}}, __size = “\002\000\000\000\000\000\000\000h\016\000\000\001”, ‘\000’ <repeats 26 times>, __align = 2}
#define pthread_mutex_lock(arg1, arg2) \
do { \
printf("the thread %s try to get the lock %s\n", arg1, #arg2); \
pthread_mutex_lock(arg2); \
printf("the thread %s get the lock %s\n", arg1, #arg2); \
}while(0);
pthread_mutex_lock("thread1", &mutex_1); // thread2部分相同
sleep(1);
pthread_mutex_lock("thread1", &mutex_2);
...
the thread thread2 get the lock &mutex_2
the thread thread1 try to get the lock &mutex_1
the thread thread1 get the lock &mutex_1
the thread thread2 try to get the lock &mutex_1
the thread thread1 try to get the lock &mutex_2
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/193818.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...