# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [18:32:28] $ gcc -Wall creat1.c -o creat1 -lpthread creat1.c: In function ‘func’: creat1.c:18:18: warning: passing argument 1 of ‘pthread_exit’ makes pointer from integer without a cast [-Wint-conversion] 18 | pthread_exit(3); | ^ | | | int In file included from creat1.c:12: /usr/include/pthread.h:207:33: note: expected ‘void *’ but argument is of type ‘int’ 207 | extern void pthread_exit (void *__retval) __attribute__ ((__noreturn__)); | ~~~~~~^~~~~~~~ creat1.c: In function ‘main’: creat1.c:44:23: warning: passing argument 2 of ‘pthread_join’ from incompatible pointer type [-Wincompatible-pointer-types] 44 | pthread_join(tid, &retval); //类似进程的wati() | ^~~~~~~ | | | int * In file included from creat1.c:12: /usr/include/pthread.h:215:49: note: expected ‘void **’ but argument is of type ‘int *’ 215 | extern int pthread_join (pthread_t __th, void **__thread_return); | ~~~~~~~^~~~~~~~~~~~~~~
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [18:40:25] $ ./creat1 begin main func 2 end 3
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [18:40:27] $
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:07:58] $ gcc -Wall cleanup.c -o cleanup -lpthread #demo0
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:00] $ ./cleanup begin pthread func is working push over cleanup3 end
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:04] $ vi cleanup.c
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:13] $ gcc -Wall cleanup.c -o cleanup -lpthread #demo1
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:24] $ ./cleanup begin pthread func is working push over cleanup3 cleanup2 cleanup1 end
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [19:08:26] $
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [21:08:38] $ gcc -Wall primer0.c -o primer0 -lpthread primer0.c: In function ‘main’: primer0.c:25:66: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 25 | err = pthread_create(tid + (i - LEFT), NULL, thr_primer, (void *)i); //如果把i的地址则会有竞争 | ^ primer0.c: In function ‘thr_primer’: primer0.c:45:9: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 45 | i = (int)p; | ^
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [21:08:40] $ ./primer0 30000059 is a primer 30000071 is a primer 30000083 is a primer 30000023 is a primer 30000137 is a primer 30000001 is a primer 30000041 is a primer 30000199 is a primer 30000193 is a primer 30000037 is a primer 30000149 is a primer 30000049 is a primer 30000109 is a primer 30000169 is a primer 30000163 is a primer 30000167 is a primer 30000133 is a primer 30000079 is a primer
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [21:09:12] $
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [11:20:10] C:142 $ gcc -Wall abcd.c -o abcd -lpthread abcd.c: In function ‘thr_abcd’: abcd.c:29:13: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 29 | int n = (int)p; | ^ abcd.c:30:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 30 | int c = 'a' + (int)p; | ^ abcd.c: In function ‘main’: abcd.c:49:55: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 49 | err = pthread_create(tid + i, NULL, thr_abcd, (void *)i); | ^
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [11:21:54] $ ./abcd abcdabcdabcdabcdabcdabcdabcda... [1] 20197 alarm ./abcd
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [15:10:33] $ gcc -Wall primer0_pool.c -o primer0_pool -lpthread primer0_pool.c: In function ‘main’: primer0_pool.c:30:57: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] 30 | err = pthread_create(tid + i, NULL, thr_primer, (void *)i); | ^ primer0_pool.c: In function ‘thr_primer’: primer0_pool.c:115:45: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast] 115 | printf("[%d] %d is a primer\n", (int)p, i); | ^
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [15:17:54] $ ./primer0_pool [0] 30000001 is a primer [1] 30000023 is a primer [3] 30000041 is a primer [2] 30000037 is a primer [4] 30000049 is a primer [0] 30000059 is a primer [4] 30000083 is a primer [3] 30000071 is a primer [2] 30000109 is a primer [1] 30000079 is a primer [4] 30000137 is a primer [3] 30000149 is a primer [0] 30000133 is a primer [4] 30000169 is a primer [3] 30000193 is a primer [2] 30000163 is a primer [1] 30000167 is a primer [0] 30000199 is a primer
# li @ evpower in ~/humble/tmp/lhq/parallel/thread/posix on git:master x [15:17:56] $
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:09] C:2 $ tree . ├── main.c ├── Makefile ├── mytbf.c └── mytbf.h
0 directories, 4 files
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:10] $ make cc -Wall -c -o main.o main.c cc -Wall -c -o mytbf.o mytbf.c gcc -Wall main.o mytbf.o -o mytbf -lpthread
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:16] $ ./mytbf /etc/services # Network services, Internet style # # Note that i^C
# mi @ evpower in ~/humble/tmp/lhq/parallel/thread/posix/mytbf_mt on git:master x [19:17:29] C:130 $
上面的程序经过测试,发现 CPU 正在满负荷工作,说明程序中出现了忙等。就是 mytbf_fetchtoken() 函数获得锁的时候采用了忙等的方式。异步程序有两种处理方式,一种是通知法,一种是查询法。我们这里用的就是查询法,下面改成通知法实现。