一、举例说明基本函数
1 | #include <stdio.h> |
2 | #include <unistd.h> |
3 | #include <stdlib.h> |
4 | #include <pthread.h> |
5 | |
6 | void *thread_function(void *arg); |
7 | char message[] = "Hello World"; |
8 | int main() |
9 | { |
10 | int res; |
11 | pthread_t a_thread; |
12 | void *thread_result; |
13 | |
14 | res = pthread_create(&a_thread, NULL, |
15 | thread_function, (void *)message); |
16 | if (res != 0) |
17 | { |
18 | perror("Thread creation failed"); |
19 | exit(EXIT_FAILURE); |
20 | } |
21 | |
22 | printf("Waiting for thread to finish.../n"); |
23 | res = pthread_join(a_thread,&thread_result); |
24 | if (res != 0) |
25 | { |
26 | perror("Thread join failed"); |
27 | exit(EXIT_FAILURE); |
28 | } |
29 | |
30 | printf("Thread joined, it returned %s/n",(char *)thread_result); |
31 | printf("Message is now %s/n", message); |
32 | exit(EXIT_SUCCESS); |
33 | } |
34 | void *thread_function(void *arg) |
35 | { |
36 | printf("thread_function is running. Argument |
37 | was %s/n", (char *)arg); |
38 | sleep(3); |
39 | strcpy(message, "Bye!"); |
40 | pthread_exit("Thank you for the CPU time"); |
41 | } |
①pthread_create
pthread_create(&a_thread,NULL,thread_function,(void *)&share_int);
参数说明:
&a_thread:表示线程描述符号。
NULL:是将要被创建的线程运行属性,在这里取NULL为默认。
thread_function:线程运行函数的起始地址。
(void *)&share_int:运行函数的参数。
线程的入口函数的调用方法:当主线程CREATE后,线程函数会自动直接开始运行。
两线程间参数传递的方式:通过pthread_create的最后一个参数进行传递。
②pthread_join
头文件:#include <pthread.h>
函数原型:int pthread_join( pthread_t *th,void **thread_return )
—th:等待线程的标识符
—thread_return:用户定义指针,用来存储被等待线程的返回值——*retval
③退出:void pthread_exit( void *retval )
二、互斥锁
②程序
1 | #include <stdio.h> |
2 | #include <unistd.h> |
3 | #include <stdlib.h> |
4 | #include <pthread.h> |
5 | #include <semaphore.h> |
6 | |
7 | void *thread_function(void *arg); |
8 | pthread_mutex_t work_mutex; //互斥锁的一个结构 |
9 | |
10 | #define WORK_SIZE 1024 |
11 | char work_area[WORK_SIZE]; |
12 | int time_to_exit = 0; |
13 | |
14 | int main() |
15 | { |
16 | int res; |
17 | pthread_t a_thread;//线程标识符号 |
18 | void *thread_result; |
19 | //初始化互斥锁 |
20 | res = pthread_mutex_init(&work_mutex, NULL); |
21 | if (res != 0) |
22 | { |
23 | perror("Mutex initialization failed"); |
24 | exit(EXIT_FAILURE); |
25 | } |
26 | //创建线程 |
27 | res = pthread_create(&a_thread, NULL, thread_function, NULL); |
28 | if (res != 0) |
29 | { |
30 | perror("Thread creation failed"); |
31 | exit(EXIT_FAILURE); |
32 | } |
33 | //要开始输入内容时,锁上互斥锁,使得线程不能进行上锁操作 |
34 | pthread_mutex_lock(&work_mutex); |
35 | printf("Input some text. Enter 'end' to finish/n"); |
36 | while(!time_to_exit) |
37 | { |
38 | fgets(work_area, WORK_SIZE, stdin); |
39 | //输入完后,解锁,子线程马上上锁,准备输出字符个数 |
40 | pthread_mutex_unlock(&work_mutex); |
41 | |
42 | while(1) |
43 | { |
44 | //子线输出了字符个数,解完锁 |
45 | //主线程马上上锁,退出本层循环,继续下次输入(没有输入end) |
46 | pthread_mutex_lock(&work_mutex); |
47 | if (work_area[0] != '/0') |
48 | { |
49 | pthread_mutex_unlock(&work_mutex); |
50 | sleep(1); |
51 | } |
52 | else |
53 | { |
54 | break; |
55 | } |
56 | } |
57 | } |
58 | pthread_mutex_unlock(&work_mutex); |
59 | |
60 | printf("/nWaiting for thread to finish.../n"); |
61 | res = pthread_join(a_thread, &thread_result); |
62 | if (res != 0) |
63 | { |
64 | perror("Thread join failed"); |
65 | exit(EXIT_FAILURE); |
66 | } |
67 | printf("Thread joined/n"); |
68 | pthread_mutex_destroy(&work_mutex); |
69 | exit(EXIT_SUCCESS); |
70 | } |
71 | |
72 | void *thread_function(void *arg) |
73 | { |
74 | sleep(1); |
75 | pthread_mutex_lock(&work_mutex); |
76 | |
77 | while(strncmp("end", work_area, 3) != 0) |
78 | { |
79 | printf("You input %d characters/n", strlen(work_area) -1); |
80 | work_area[0] = '/0'; |
81 | pthread_mutex_unlock(&work_mutex); |
82 | |
83 | sleep(1); |
84 | //该处目的是实现初始情况 |
85 | //sleep(1); |
86 | //pthread_mutex_lock(&work_mutex); |
87 | //这两句的效果,当第一次输入,输出完成,主线程在第二次输入的时候,子线程一直在这里挂起 |
88 | pthread_mutex_lock(&work_mutex); |
89 | while (work_area[0] == '/0' ) |
90 | { |
91 | pthread_mutex_unlock(&work_mutex); |
92 | sleep(1); |
93 | pthread_mutex_lock(&work_mutex); |
94 | } |
95 | } |
96 | time_to_exit = 1; |
97 | work_area[0] = '/0'; |
98 | pthread_mutex_unlock(&work_mutex); |
99 | pthread_exit(0); |
100 | } |