能夠對進程的內存空間和資源進行訪問,並與同一進程中的其餘線程共享html
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5int main(){
6 pthread_t thread_id;
7
8 thread_id=pthread_self(); // 返回調用線程的線程ID
9 printf("Thread ID: %lu.\n",thread_id);
10
11if (pthread_equal(thread_id,pthread_self())) {
12// if (thread_id==0) {
13 printf("Equal!\n");
14 } else {
15 printf("Not equal!\n");
16 }
17return0;
18 }
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5void *thrd_func(void *arg);
6 pthread_t tid;
7
8int main(){
9// 建立線程tid,且線程函數由thrd_func指向,是thrd_func的入口點,即立刻執行此線程函數
10if (pthread_create(&tid,NULL,thrd_func,NULL)!=0) {
11 printf("Create thread error!\n");
12 exit(1);
13 }
14
15 printf("TID in pthread_create function: %u.\n",tid);
16 printf("Main process: PID: %d,TID: %u.\n",getpid(),pthread_self());
17
18 sleep(1); //race
19
20return0;
21 }
22
23void *thrd_func(void *arg){
24// printf("I am new thread!\n");
25 printf("New process: PID: %d,TID: %u.\n",getpid(),pthread_self()); //why pthread_self
26 printf("New process: PID: %d,TID: %u.\n",getpid(),tid); //why pthread_self
27
28 pthread_exit(NULL); //退出線程
29// return ((void *)0);
30 }
使調用進程終止,全部線程都終止了編程
等待線程多線程
ㄠ蹵N個進程中的多個線程是共享數據段的,一般在線程退出以後,退出線程所佔用的資源並不會隨着線程的終止而獲得釋放ide
瀠琀梔爀攀愀攙開樀漀椀渀()函數函數
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5void *thrd_func1(void *arg);
6void *thrd_func2(void *arg);
7
8int main(){
9 pthread_t tid1,tid2;
10void *tret;
11// 建立線程tid1,線程函數thrd_func1
12if (pthread_create(&tid1,NULL,thrd_func1,NULL)!=0) {
13 printf("Create thread 1 error!\n");
14 exit(1);
15 }
16// 建立線程tid2,線程函數thrd_func2
17if (pthread_create(&tid2,NULL,thrd_func2,NULL)!=0) {
18 printf("Create thread 2 error!\n");
19 exit(1);
20 }
21// 等待線程tid1結束,線程函數返回值放在tret中
22if (pthread_join(tid1,&tret)!=0){
23 printf("Join thread 1 error!\n");
24 exit(1);
25 }
26
27 printf("Thread 1 exit code: %d.\n",(int *)tret);
28// 等待tid2結束,線程函數返回值放在tret中
29if (pthread_join(tid2,&tret)!=0){
30 printf("Join thread 2 error!\n");
31 exit(1);
32 }
33
34 printf("Thread 2 exit code: %d.\n",(int *)tret);
35
36return0;
37 }
38
39void *thrd_func1(void *arg){
40 printf("Thread 1 returning!\n");
41// sleep(3);
42return ((void *)1); // 自動退出線程
43 }
44
45void *thrd_func2(void *arg){
46 printf("Thread 2 exiting!\n");
47 pthread_exit((void *)2); // 線程主動退出,返回(void *)2
48 }
取消線程post
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5void *thrd_func1(void *arg);
6void *thrd_func2(void *arg);
7
8 pthread_t tid1,tid2;
9
10int main(){
11// 建立線程tid1,線程函數thrd_func1
12if (pthread_create(&tid1,NULL,thrd_func1,NULL)!=0) {
13 printf("Create thread 1 error!\n");
14 exit(1);
15 }
16// 建立線程tid2,線程函數thrd_func2
17if (pthread_create(&tid2,NULL,thrd_func2,NULL)!=0) {
18 printf("Create thread 2 error!\n");
19 exit(1);
20 }
21// 等待線程tid1退出
22if (pthread_join(tid1,NULL)!=0){
23 printf("Join thread 1 error!\n");
24 exit(1);
25 }else
26 printf("Thread 1 Joined!\n");
27// 等待線程tid2退出
28if (pthread_join(tid2,NULL)!=0){
29 printf("Join thread 2 error!\n");
30 exit(1);
31 }else
32 printf("Thread 2 Joined!\n");
33
34return0;
35 }
36
37void *thrd_func1(void *arg){
38// pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
39 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); // 設置其餘線程能夠cancel掉此線程
40
41while(1) {
42 printf("Thread 1 is running!\n");
43 sleep(1);
44 }
45 pthread_exit((void *)0);
46 }
47
48void *thrd_func2(void *arg){
49 printf("Thread 2 is running!\n");
50 sleep(5);
51if (pthread_cancel(tid1)==0) // 線程tid2向線程tid1發送cancel
52 printf("Send Cancel cmd to Thread 1.\n");
53
54 pthread_exit((void *)0);
55 }
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5#define THREAD_NUM 3
6#define REPEAT_TIMES 5
7#define DELAY 4
8
9void *thrd_func(void *arg);
10
11int main(){
12 pthread_t thread[THREAD_NUM];
13int no;
14void *tret;
15
16 srand((int)time(0)); // 初始化隨機函數發生器
17
18for(no=0;no<THREAD_NUM;no++){
19if (pthread_create(&thread[no],NULL,thrd_func,(void*)no)!=0) { // 建立THREAD_NUM個線程,傳入(void*)no做爲thrd_func的參數
20 printf("Create thread %d error!\n",no);
21 exit(1);
22 } else
23 printf("Create thread %d success!\n",no);
24 }
25
26for(no=0;no<THREAD_NUM;no++){
27if (pthread_join(thread[no],&tret)!=0){ // 等待thread[no]線程結束,線程函數返回值放在tret中
28 printf("Join thread %d error!\n",no);
29 exit(1);
30 }else
31 printf("Join thread %d success!\n",no);
32 }
33
34return0;
35 }
36
37void *thrd_func(void *arg){
38int thrd_num=(void*)arg;
39int delay_time=0;
40int count=0;
41
42 printf("Thread %d is starting.\n",thrd_num);
43for(count=0;count<REPEAT_TIMES;count++) {
44 delay_time=(int)(DELAY*(rand()/(double)RAND_MAX))+1;
45 sleep(delay_time);
46 printf("\tThread %d:job %d delay =%d.\n",thrd_num,count,delay_time);
47 }
48
49 printf("Thread %d is exiting.\n",thrd_num);
50 pthread_exit(NULL);
51 }
和上一版本的程序差別在於有沒有鎖,有鎖的狀況下,必須等"thread x is exiting."以後其餘線程才能繼續。spa
eg. 同步各線程,執行順序爲逆序。操作系統