在 Linux 系统中,进程和线程是两种重要的并发执行单元。本文将详细介绍它们的区别、使用场景、以及多线程编程中的关键API和示例代码。
进程与线程的区别
进程
- 进程是程序运行的一个实例,承担分配系统资源的基本单位。
- 每个进程都有独立的地址空间,一个进程崩溃不会影响其他进程。
- 进程的创建和切换消耗较多资源。
线程
- 线程是进程中的一个执行路径,是CPU调度的基本单位。
- 线程共享进程的地址空间,但每个线程有自己的堆栈和局部变量。
- 线程的创建和切换开销较小。
- 如果一个线程崩溃,会导致整个进程崩溃。
使用线程的理由
- 节省资源:创建进程需要分配独立的地址空间,建立多个数据表来维护其代码段、数据段和堆栈段,这种方式十分昂贵。线程共享同一进程的地址空间和大部分数据,启动一个线程比启动一个进程要快很多。一个进程的开销大约是一个线程的30倍。
- 方便的通信机制:不同进程之间的数据传递需要通过通信机制,如管道、信号等,这种方式耗时且复杂。而线程共享进程的数据空间,数据共享非常方便和快捷,但需要注意数据同步的问题。
多线程开发及API
多线程开发主要包含三点:线程、互斥锁、条件变量。以下是具体的操作和API介绍:
线程操作
线程的创建
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
线程的获取和比较
#include <pthread.h>
pthread_t pthread_self(void);
线程的等待
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
// 参数:
// pthread_t thread:等待的线程
// void **rval:线程退出状态的收回,NULL表示不收回
线程的退出
#include <pthread.h>
int pthread_exit(void *rval_ptr);
线程的创建、退出、等待示例
#include <stdio.h>
#include <pthread.h>
void *func1(void *arg)
{
static int ret = 10;
printf("t1:%ld thread is created\n", (unsigned long)pthread_self());
printf("t1: parameter is %d\n", *((int *)arg));
pthread_exit((void*)&ret); // 线程退出
}
int main()
{
int ret;
int param = 100;
int *pret = NULL;
pthread_t t1;
ret = pthread_create(&t1, NULL, func1, (void*)¶m); // 创建线程
if(ret == 0){
printf("main: create t1 success\n");
}
printf("main: %ld\n", (unsigned long)pthread_self()); // 获取线程ID
pthread_join(t1, (void**)&pret); // 等待线程退出
printf("main: t1 quit with %d\n", *pret);
return 0;
}
传入一个结构体的线程创建示例
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
struct data
{
int a;
char *s;
};
void *func1(void *arg)
{
static char *x = "t1 run out";
struct data *temp;
temp = (struct data*)arg;
printf("t1:%ld pthread is created\n", (unsigned long)pthread_self());
printf("t1: %d\n", temp->a);
printf("t1: %s\n", temp->s);
pthread_exit((void*)x);
}
int main()
{
int ret;
pthread_t t1;
char *pret = NULL;
struct data *p = (struct data*)malloc(sizeof(struct data));
p->a = 1;
p->s = "xiancheng";
ret = pthread_create(&t1, NULL, func1, (void*)p);
if(ret == 0){
printf("main: create t1 success\n");
}
printf("main: %ld\n", (unsigned long)pthread_self());
pthread_join(t1, (void**)&pret);
printf("main: t1 quit with %s\n", pret);
free(p);
return 0;
}
线程共享空间验证示例
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int g_data = 10;
void *func1(void *arg)
{
printf("t1:%ld thread is created\n", (unsigned long)pthread_self());
printf("t1: parameter is %d\n", *((int*)arg));
while(1){
printf("%d\n", g_data++);
sleep(1);
if(g_data == 3){
pthread_exit(NULL);
}
}
}
void *func2(void *arg)
{
printf("t2:%ld thread is created\n", (unsigned long)pthread_self());
printf("t2: parameter is %d\n", *((int*)arg));
while(1){
printf("%d\n", g_data++);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
ret = pthread_create(&t1, NULL, func1, (void*)¶m);
if(ret == 0){
printf("main: create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2, (void*)¶m);
if(ret == 0){
printf("main: create t2 success\n");
}
printf("main: %ld\n", (unsigned long)pthread_self());
while(1){
printf("%d\n", g_data++);
sleep(1);
}
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
互斥锁(Mutex)
互斥锁API
创建及销毁互斥锁
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *restrict mutex);
加锁及解锁
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *restrict mutex);
int pthread_mutex_trylock(pthread_mutex_t *restrict mutex);
int pthread_mutex_unlock(pthread_mutex_t *restrict mutex);
使用互斥锁的示例
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int g_data = 0;
pthread_mutex_t mutex; // 定义锁
void *func1(void *arg)
{
pthread_mutex_lock(&mutex); // 加锁
for(int i = 0; i < 5; i++){
printf("t1: %ld thread is created\n", (unsigned long)pthread_self());
printf("t1: parameter is %d\n", *((int*)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex); // 解锁
}
void *func2(void *arg)
{
pthread_mutex_lock(&mutex);
for(int i = 0; i < 5; i++){
printf("t2: %ld thread is created\n", (unsigned long)pthread_self());
printf("t2: parameter is %d\n", *((int*)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex);
}
void *func3(void *arg)
{
pthread_mutex_lock(&mutex);
for(int i = 0; i < 5; i++){
printf("t3: %ld thread is created\n", (unsigned long)pthread_self());
printf("t3: parameter is %d\n", *((int*)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_t t3;
pthread_mutex_init(&mutex, NULL); // 初始化锁
ret = pthread_create(&t1, NULL, func1, (void*)¶m);
if(ret == 0){
printf("main: create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2, (void*)¶m);
if(ret == 0){
printf("main: create t2 success\n");
}
ret = pthread_create(&t3, NULL, func3, (void*)¶m);
if(ret == 0){
printf("main: create t3 success\n");
}
printf("main: %ld\n", (unsigned long)pthread_self());
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
pthread_mutex_destroy(&mutex); // 收回锁
return 0;
}
互斥锁限制共享资源的访问示例
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int g_data = 0;
pthread_mutex_t mutex;
void *func1(void *arg)
{
printf("t1: %ld pthread is created\n", (unsigned long)pthread_self());
printf("t1: parameter is %d\n", *((int*)arg));
while(1){
pthread_mutex_lock(&mutex);
printf("%d\n", g_data++);
sleep(1);
if(g_data == 3){
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
}
}
void *func2(void *arg)
{
printf("t2: %ld pthread is created\n", (unsigned long)pthread_self());
printf("t2: parameter is %d\n", *((int*)arg));
while(1){
printf("%d\n", g_data);
pthread_mutex_lock(&mutex);
g_data++;
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_mutex_init(&mutex, NULL);
ret = pthread_create(&t1, NULL, func1, (void*)¶m);
if(ret == 0){
printf("main: create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2, (void*)¶m);
if(ret == 0){
printf("main: create t2 success\n");
}
printf("main: %ld\n", (unsigned long)pthread_self());
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
死锁示例
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex;
pthread_mutex_t mutex2;
void *func1(void *arg)
{
pthread_mutex_lock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex2);
for(int i = 0; i < 5; i++){
printf("t1: %ld\n", (unsigned long)pthread_self());
printf("t1: parameter is %d\n", *((int *)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&mutex2);
}
void *func2(void *arg)
{
pthread_mutex_lock(&mutex2);
sleep(1);
pthread_mutex_lock(&mutex);
for(int i = 0; i < 5; i++){
printf("t2: %ld\n", (unsigned long)pthread_self());
printf("t2: parameter is %d\n", *((int *)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex);
}
void *func3(void *arg)
{
pthread_mutex_lock(&mutex);
for(int i = 0; i < 5; i++){
printf("t3: %ld\n", (unsigned long)pthread_self());
printf("t3: parameter is %d\n", *((int *)arg));
sleep(1);
}
pthread_mutex_unlock(&mutex);
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_t t3;
pthread_mutex_init(&mutex, NULL);
pthread_mutex_init(&mutex2, NULL);
ret = pthread_create(&t1, NULL, func1, (void*)¶m);
if(ret == 0){
printf("main: create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2, (void*)¶m);
if(ret == 0){
printf("main: create t2 success\n");
}
ret = pthread_create(&t3, NULL, func3, (void*)¶m);
if(ret == 0){
printf("main: create t3 success\n");
}
printf("main: %ld\n", (unsigned long)pthread_self());
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_join(t3, NULL);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&mutex2);
return 0;
}
条件变量实现线程同步
条件变量API
创建及销毁条件变量
#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t cond);
等待
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict timeout);
触发
#include <pthread.h>
int pthread_cond_signal(pthread_cond_t *restrict cond);
int pthread_cond_broadcast(pthread_cond_t cond);
使用条件变量的示例
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
int g_data = 0;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *func1(void *arg)
{
static int cnt = 10;
printf("t1: %ld pthread is created\n", (unsigned long)pthread_self());
printf("t1: parameter is %d\n", *((int*)arg));
while(1){
pthread_cond_wait(&cond, &mutex); // 等待
if(g_data == 3){
printf("t1 run=========================\n");
}
printf("t1: %d\n", g_data);
g_data = 0;
sleep(1);
if(cnt++ == 10){
exit(1);
}
}
}
void *func2(void *arg)
{
printf("t2: %ld pthread is created\n", (unsigned long)pthread_self());
printf("t2: parameter is %d\n", *((int*)arg));
while(1){
printf("t2: %d\n", g_data);
pthread_mutex_lock(&mutex);
printf("%d\n", g_data++);
if(g_data == 3){
pthread_cond_signal(&cond); // 触发
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
int param = 100;
pthread_t t1;
pthread_t t2;
pthread_cond_init(&cond, NULL);
pthread_mutex_init(&mutex, NULL);
ret = pthread_create(&t1, NULL, func1, (void *)¶m);
if(ret == 0){
printf("main: create t1 success\n");
}
ret = pthread_create(&t2, NULL, func2, (void *)¶m);
if(ret == 0){
printf("main: create t2 success\n");
}
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
通过上述示例和API的讲解,本文详细介绍了Linux下进程与线程的区别、多线程开发的基本操作以及常见问题和解决方案。希望能够帮助大家更好地理解和使用多线程编程。