简介
- Linux POSIX
详解
Linux POSIX 详解
在 Linux POSIX 系统中,<pthread.h>
是用于多线程编程的头文件。POSIX 线程(Pthreads)是一种遵循 POSIX 标准的线程模型,允许程序在单个进程中创建和管理多个并发执行的线程。
以下是关于 <pthread.h>
头文件的详细解释:
功能
<pthread.h>
头文件中定义了用于线程管理的函数、类型和常量,允许程序创建、控制和同步线程的执行。- 通过该头文件提供的函数,可以创建线程、等待线程结束、线程互斥、条件变量等。
常用函数
pthread_create()
:用于创建一个新的线程。pthread_join()
:用于等待一个线程的结束。pthread_exit()
:用于退出当前线程。pthread_mutex_init()
和pthread_mutex_lock()
等:用于创建和操作互斥锁。pthread_cond_init()
和pthread_cond_wait()
等:用于创建和操作条件变量。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *print_message(void *ptr) {
char *message = (char *)ptr;
printf("%s\n", message);
pthread_exit(NULL);
}
int main() {
pthread_t thread1, thread2;
const char *message1 = "线程1";
const char *message2 = "线程2";
// 创建线程1
pthread_create(&thread1, NULL, print_message, (void *)message1);
// 创建线程2
pthread_create(&thread2, NULL, print_message, (void *)message2);
// 等待线程1结束
pthread_join(thread1, NULL);
// 等待线程2结束
pthread_join(thread2, NULL);
return 0;
}
注意事项
- 多线程编程需要小心处理共享资源,避免出现数据竞争和死锁等问题。
- 使用线程时需要谨慎管理线程的生命周期,确保适当地创建、等待和退出线程,以免出现资源泄露或未预期的行为。
<pthread.h>
头文件提供了在 POSIX 系统中进行多线程编程所需的函数和工具,允许程序员创建并发执行的线程,以实现更高效的并发和异步操作。
Linux POSIX pthread_create() 详解
pthread_create()
是 Linux POSIX 系统中 <pthread.h>
头文件中提供的函数,用于创建新的线程。
以下是关于 pthread_create()
函数的详细解释:
函数原型
1
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
参数
thread
:指向pthread_t
类型的指针,用于存储新线程的标识符。attr
:指向pthread_attr_t
类型的指针,用于指定新线程的属性(通常为NULL
,表示使用默认属性)。start_routine
:是一个指向线程函数的指针,新线程将从这个函数开始执行。arg
:传递给start_routine
函数的参数。
返回值
pthread_create()
函数成功创建新线程时,返回0;如果出现错误,则返回相应的错误码。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <pthread.h>
void *thread_function(void *arg) {
int value = *(int *)arg;
printf("传入的值是:%d\n", value);
pthread_exit(NULL);
}
int main() {
pthread_t my_thread;
int value = 42;
// 创建新线程
int result = pthread_create(&my_thread, NULL, thread_function, (void *)&value);
if (result == 0) {
printf("新线程创建成功\n");
pthread_join(my_thread, NULL); // 等待新线程结束
} else {
printf("新线程创建失败\n");
}
return 0;
}
注意事项
- 线程函数的签名必须为
void *function(void *arg)
,并且参数和返回值必须是void *
类型。 - 在使用
pthread_create()
创建线程时,需要传递一个函数指针作为新线程的入口点,并可以传递参数给新线程的函数。
pthread_create()
函数是在 Linux POSIX 环境下创建新线程的标准方法。它允许程序员指定新线程的入口函数,并传递参数给新线程。成功创建线程后,可以通过 pthread_join()
等待线程的结束。
Linux POSIX pthread_join() 详解
pthread_join()
是 Linux POSIX 系统中 <pthread.h>
头文件提供的函数,用于等待一个特定线程的结束。
以下是关于 pthread_join()
函数的详细解释:
函数原型
1
int pthread_join(pthread_t thread, void **retval);
参数
thread
:表示要等待的线程标识符,即被等待线程的pthread_t
。retval
:用于接收线程的返回值的指针,可以为NULL
(如果不需要获取线程的返回值)。
返回值
pthread_join()
函数成功时返回0;如果出现错误,则返回相应的错误码。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <pthread.h>
void *thread_function(void *arg) {
printf("子线程执行中...\n");
pthread_exit((void *)42);
}
int main() {
pthread_t my_thread;
void *thread_result;
// 创建新线程
pthread_create(&my_thread, NULL, thread_function, NULL);
// 等待新线程结束并获取返回值
if (pthread_join(my_thread, &thread_result) == 0) {
printf("子线程执行完成,返回值为:%ld\n", (long)thread_result);
} else {
printf("无法等待子线程\n");
}
return 0;
}
注意事项
pthread_join()
函数允许主线程等待指定的线程结束,并且获取线程的返回值。- 如果主线程不关心线程的返回值,可以将
retval
参数设置为NULL
。 - 如果线程已经在运行并且成功完成,调用
pthread_join()
将立即返回;否则,它将阻塞主线程,直到被等待的线程结束。
pthread_join()
函数用于实现线程之间的同步,允许一个线程等待另一个线程的完成,并获取其返回值(如果有)。
Linux POSIX pthread_exit() 详解
pthread_exit()
是 Linux POSIX 系统中 <pthread.h>
头文件提供的函数,用于在线程执行过程中退出线程。
以下是关于 pthread_exit()
函数的详细解释:
函数原型
1
void pthread_exit(void *retval);
参数
retval
:表示线程的返回值,可以是任何类型的指针(void *
)。线程的返回值可以在其他线程中通过pthread_join()
获取。
返回值
pthread_exit()
函数本身没有返回值,它将导致调用它的线程立即退出。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <pthread.h>
void *thread_function(void *arg) {
printf("子线程执行中...\n");
pthread_exit((void *)42);
}
int main() {
pthread_t my_thread;
void *thread_result;
// 创建新线程
pthread_create(&my_thread, NULL, thread_function, NULL);
// 等待新线程结束并获取返回值
pthread_join(my_thread, &thread_result);
printf("子线程执行完成,返回值为:%ld\n", (long)thread_result);
return 0;
}
注意事项
- 在线程执行过程中,调用
pthread_exit()
可以立即退出线程,不必等到线程执行完毕。 - 线程的返回值可以通过
pthread_join()
获取,这个返回值也可以是指向堆中分配内存的指针。
pthread_exit()
函数允许线程在执行期间提前退出,同时可以指定一个返回值,这个返回值可以在其他线程中获取。
Linux POSIX pthread_mutex_init() 详解
pthread_mutex_init()
是 Linux POSIX 系统中 <pthread.h>
头文件提供的函数,用于初始化互斥锁(mutex)。
以下是关于 pthread_mutex_init()
函数的详细解释:
函数原型
1
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr);
参数
mutex
:指向pthread_mutex_t
类型的指针,用于指定要初始化的互斥锁。attr
:指向pthread_mutexattr_t
类型的指针,用于指定互斥锁的属性,通常为NULL
表示使用默认属性。
返回值
pthread_mutex_init()
函数成功时返回0;如果出现错误,则返回相应的错误码。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS 5
pthread_mutex_t mutex; // 全局互斥锁
void *thread_function(void *arg) {
pthread_mutex_lock(&mutex); // 锁定互斥锁
printf("线程 %ld 运行中...\n", (long)arg);
pthread_mutex_unlock(&mutex); // 解锁互斥锁
pthread_exit(NULL);
}
int main() {
pthread_t threads[NUM_THREADS];
int i;
// 初始化互斥锁
if (pthread_mutex_init(&mutex, NULL) != 0) {
printf("互斥锁初始化失败\n");
return 1;
}
// 创建多个线程
for (i = 0; i < NUM_THREADS; ++i) {
pthread_create(&threads[i], NULL, thread_function, (void *)(long)i);
}
// 等待线程结束
for (i = 0; i < NUM_THREADS; ++i) {
pthread_join(threads[i], NULL);
}
// 销毁互斥锁
pthread_mutex_destroy(&mutex);
return 0;
}
注意事项
pthread_mutex_init()
用于初始化互斥锁,它为指定的互斥锁分配资源并设置初始属性。- 在使用互斥锁之前,必须先初始化互斥锁,否则会导致未定义的行为。
pthread_mutex_init()
函数允许程序员初始化互斥锁,以确保多线程环境中对共享资源的访问是安全的。
Linux POSIX pthread_mutex_lock() 详解
pthread_mutex_lock()
是 Linux POSIX 系统中 <pthread.h>
头文件提供的函数,用于对互斥锁(mutex)进行加锁操作。
以下是关于 pthread_mutex_lock()
函数的详细解释:
函数原型
1
int pthread_mutex_lock(pthread_mutex_t *mutex);
参数
mutex
:指向pthread_mutex_t
类型的指针,表示要加锁的互斥锁。
返回值
pthread_mutex_lock()
函数成功时返回0;如果出现错误,则返回相应的错误码。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 静态初始化互斥锁
void *thread_function(void *arg) {
pthread_mutex_lock(&mutex); // 加锁互斥锁
printf("线程 %ld 运行中...\n", (long)arg);
pthread_mutex_unlock(&mutex); // 解锁互斥锁
pthread_exit(NULL);
}
int main() {
pthread_t threads[3];
int i;
// 创建多个线程
for (i = 0; i < 3; ++i) {
pthread_create(&threads[i], NULL, thread_function, (void *)(long)i);
}
// 等待线程结束
for (i = 0; i < 3; ++i) {
pthread_join(threads[i], NULL);
}
return 0;
}
注意事项
pthread_mutex_lock()
用于对互斥锁进行加锁操作,如果互斥锁已经被其他线程锁定,则调用线程会阻塞直到获得锁。- 加锁的互斥锁应该在使用后及时解锁,以免造成死锁或其他线程无法获得锁的情况。
pthread_mutex_lock()
函数用于保护共享资源,确保在多线程环境中对共享资源的访问是互斥且安全的。
Linux POSIX pthread_cond_init() 详解
pthread_cond_init()
是 Linux POSIX 系统中 <pthread.h>
头文件提供的函数,用于初始化条件变量(condition variable)。
以下是关于 pthread_cond_init()
函数的详细解释:
函数原型
1
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
参数
cond
:指向pthread_cond_t
类型的指针,表示要初始化的条件变量。attr
:指向pthread_condattr_t
类型的指针,用于指定条件变量的属性,通常为NULL
表示使用默认属性。
返回值
pthread_cond_init()
函数成功时返回0;如果出现错误,则返回相应的错误码。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <pthread.h>
pthread_cond_t condition = PTHREAD_COND_INITIALIZER; // 静态初始化条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 静态初始化互斥锁
void *waiter_function(void *arg) {
pthread_mutex_lock(&mutex);
printf("等待线程开始等待...\n");
pthread_cond_wait(&condition, &mutex); // 等待条件变量满足
printf("等待线程收到信号,继续执行...\n");
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
void *signaler_function(void *arg) {
printf("信号发送线程睡眠一秒...\n");
sleep(1); // 模拟延迟
printf("信号发送线程发送信号...\n");
pthread_mutex_lock(&mutex);
pthread_cond_signal(&condition); // 发送信号通知等待线程
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main() {
pthread_t waiter_thread, signaler_thread;
// 创建等待线程和发送信号线程
pthread_create(&waiter_thread, NULL, waiter_function, NULL);
pthread_create(&signaler_thread, NULL, signaler_function, NULL);
// 等待线程结束
pthread_join(waiter_thread, NULL);
pthread_join(signaler_thread, NULL);
return 0;
}
注意事项
pthread_cond_init()
用于初始化条件变量,在使用条件变量之前必须先进行初始化。- 条件变量通常与互斥锁结合使用,以确保线程在等待条件变量时能够安全地访问共享资源。
条件变量允许线程在满足特定条件时等待或者唤醒其他线程。结合互斥锁,它可以用于线程之间的同步和通信。
Linux POSIX pthread_cond_wait() 详解
pthread_cond_wait()
是 Linux POSIX 系统中 <pthread.h>
头文件提供的函数,用于等待条件变量的信号。
以下是关于 pthread_cond_wait()
函数的详细解释:
函数原型
1
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
参数
cond
:指向pthread_cond_t
类型的指针,表示要等待的条件变量。mutex
:指向pthread_mutex_t
类型的指针,表示与条件变量关联的互斥锁。在等待条件变量之前,线程必须持有这个互斥锁。
返回值
pthread_cond_wait()
函数成功时返回0;如果出现错误,则返回相应的错误码。
示例用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>
#include <pthread.h>
pthread_cond_t condition = PTHREAD_COND_INITIALIZER; // 静态初始化条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // 静态初始化互斥锁
void *waiter_function(void *arg) {
pthread_mutex_lock(&mutex);
printf("等待线程开始等待...\n");
pthread_cond_wait(&condition, &mutex); // 等待条件变量满足
printf("等待线程收到信号,继续执行...\n");
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
void *signaler_function(void *arg) {
printf("信号发送线程睡眠一秒...\n");
sleep(1); // 模拟延迟
printf("信号发送线程发送信号...\n");
pthread_mutex_lock(&mutex);
pthread_cond_signal(&condition); // 发送信号通知等待线程
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main() {
pthread_t waiter_thread, signaler_thread;
// 创建等待线程和发送信号线程
pthread_create(&waiter_thread, NULL, waiter_function, NULL);
pthread_create(&signaler_thread, NULL, signaler_function, NULL);
// 等待线程结束
pthread_join(waiter_thread, NULL);
pthread_join(signaler_thread, NULL);
return 0;
}
注意事项
pthread_cond_wait()
用于让线程等待条件变量满足,它在等待之前会释放关联的互斥锁,并在返回时重新获取这个锁。- 在使用
pthread_cond_wait()
时,必须与关联的互斥锁配合使用,以确保线程等待和接收条件变量信号的正确性。
pthread_cond_wait()
函数允许线程在等待特定条件变量时释放互斥锁,这样其他线程可以修改共享资源并发送信号通知正在等待的线程。