本文来自网易云社区。
作者:盛长存
3.2、演示过程
(gdb) 21 printf ("thread1 : I'm thread 1\n"); 22 23 for (i = 0; i < MAX; i++) 24 25 { 26 27 printf("thread1 : number = %d\n",number); 28 29 pthread_mutex_lock(&mut); (gdb) b 27 Breakpoint 1 at 0x40079e: file good_thread.c, line 27. (gdb) 51 for (i = 0; i < MAX; i++) 52 53 { 54 55 printf("thread2 : number = %d\n",number); 56 57 pthread_mutex_lock(&mut); 58 59 number++; (gdb) b 57 Breakpoint 2 at 0x400838: file good_thread.c, line 57. (gdb) r Starting program: /home/work/testers/sgc/study/goodthread [Thread debugging using libthread_db enabled] [New Thread 182894112416 (LWP 22783)] ÎÒÊÇÖ÷º¯ÊýŶ£¬ÎÒÕýÔÚ´´½¨Ị̈߳¬ºÇºÇ [New Thread 1084229984 (LWP 22786)] Ïß³Ì1±»´´½¨ thread1 : I'm thread 1 [Switching to Thread 1084229984 (LWP 22786)] Breakpoint 1, thread1 () at good_thread.c:27 27 printf("thread1 : number = %d\n",number); (gdb) bt#0 thread1 () at good_thread.c:27#1 0x000000302b80610a in start_thread () from /lib64/tls/libpthread.so.0#2 0x000000302afc6003 in clone () from /lib64/tls/libc.so.6#3 0x0000000000000000 in ?? ()(gdb) info threads [New Thread 1094719840 (LWP 22787)] 3 Thread 1094719840 (LWP 22787) 0x000000302afc5fc4 in clone () from /lib64/tls/libc.so.6 * 2 Thread 1084229984 (LWP 22786) thread1 () at good_thread.c:27 1 Thread 182894112416 (LWP 22783) 0x000000302afc5fc4 in clone () from /lib64/tls/libc.so.6 (gdb) thread 1 [Switching to thread 1 (Thread 182894112416 (LWP 22783))]#0 0x000000302afc5fc4 in clone () from /lib64/tls/libc.so.6(gdb) bt#0 0x000000302afc5fc4 in clone () from /lib64/tls/libc.so.6#1 0x000000302b805d86 in do_clone () from /lib64/tls/libpthread.so.0#2 0x000000302b806846 in pthread_create@@GLIBC_2.2.5 () from /lib64/tls/libpthread.so.0#3 0x00000000004008fd in thread_create () at good_thread.c:91#4 0x00000000004009a9 in main () at good_thread.c:135
3.3、死锁示例程序(multi_thread.c)
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#define THREAD_NUM 20pthread_mutex_t AccountA_mutex; pthread_mutex_t AccountB_mutex;struct Account { char account_name[1]; int balance; };struct Account accountA = {'A', 100000};struct Account accountB = {'B', 200000};void * accountAB (void* amount_ptr) { int amount = *((int*)amount_ptr); pthread_mutex_lock(&AccountA_mutex); if (accountA.balance < amount) { printf("There is not enough memory in Account A!\n"); pthread_mutex_unlock(&AccountA_mutex); pthread_exit((void *)1); } accountA.balance -=amount; sleep(2); pthread_mutex_lock(&AccountB_mutex); accountB.balance +=amount; pthread_mutex_unlock(&AccountA_mutex); pthread_mutex_unlock(&AccountB_mutex); }void * accountBA (void* amount_ptr) { int amount = *((int*)amount_ptr); pthread_mutex_lock(&AccountB_mutex); if (accountB.balance < amount) { printf("There is not enough memory in Account B!\n"); pthread_mutex_unlock(&AccountB_mutex); pthread_exit((void *)1); } accountB.balance -=amount; pthread_mutex_lock(&AccountA_mutex); accountA.balance +=amount; pthread_mutex_unlock(&AccountB_mutex); pthread_mutex_unlock(&AccountA_mutex); }int main(int argc, char* argv[]) { int threadid[THREAD_NUM]; pthread_t pthread[THREAD_NUM]; void* thResState; int res, flag; int transfer_amount[THREAD_NUM] = {100, 200, 300, 400,100,200,300,400,500,600,700,800,900,800,700,600,500,400,300,200}; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); for(flag=0; flag<THREAD_NUM; flag++){ if(flag%2 == 0){ if (threadid[flag] = pthread_create(&pthread[flag], &attr , accountAB, \ (void*)&transfer_amount[flag]) < 0){ printf("Thread %d creation failed\n", flag); exit (1); } } else{ if (threadid[flag] = pthread_create(&pthread[flag], &attr , accountBA, \ (void*)&transfer_amount[flag]) < 0) { printf("Thread %d creation failed\n", flag); exit (1); } } } for(flag=0; flag<THREAD_NUM; flag++){ res = pthread_join(pthread[flag], &thResState); if (res != 0){ perror("Thread join failed"); exit(-1); } printf("thread success id %u state code %d\n",threadid[flag],thResState); } printf("Transitions are in progress.."); printf("\nAll the money is transferred !!\n"); }
网易云免费体验馆,0成本体验20+款云产品!
更多网易研发、产品、运营经验分享请访问网易云社区。
相关文章:
【推荐】 谈谈iOS开发如何写个人中心这类页面--静态tableView页面的编写
【推荐】 初识Continuation