Linux下的多线程实现—提高程序运行效率 (linux 多线程实现)

随着计算机技术的发展,程序的运行效率已经成为了时刻追求的目标。而多线程技术作为其中的一种关键技术,成为了不可或缺的一部分。Linux作为一个优秀的操作系统,最早在多线程技术的研究和应用上成为了先驱,本文将会介绍Linux下的多线程实现以及如何进行编程来提高程序运行效率。

一、多线程技术介绍

多线程技术是一种能够让程序同时执行多个任务的技术,在一个进程中可以同时创建多个线程,这些线程共享进程资源,相当于同时运行多个程序。相较于单线程的应用,多线程技术的优势体现在以下方面:

1. 程序执行速度加快了

多线程可以让程序同时处理多个任务,充分发挥了多核心CPU的性能,减少了空闲等待的时间,从而加快了程序的运行速度。

2. 提高系统资源利用率

多线程可以让操作系统的资源充分利用,使用多任务处理的方式让CPU在处理任务时轮流执行多个线程,从而充分利用了CPU的时间,提高了系统的资源利用率。

3. 优化用户体验

多线程技术可以让程序在执行过程中保持响应,同时也保证了线程之间的相互独立。这使得程序不会因为某个线程的执行出现问题而导致整个程序崩溃,从而提升了用户的体验。

二、Linux下的多线程实现

1. 线程的创建和结束

在Linux下,线程的创建和结束是通过调用线程库函数来实现的。pthread_create()用于创建线程,该函数原型如下:

“`

#include

extern int pthread_create(pthread_t* tid, const pthread_attr_t* attr, void (*fn)(void*), void* arg);

“`

其中,tid是指向线程标识符的指针,attr是指向线程属性的指针,fn是指向线程函数的指针,arg是传递给线程函数的参数,该函数执行成功时返回0,否则返回错误码。

pthread_exit()用于线程的结束,该函数原型如下:

“`

#include

extern void pthread_exit(void* retVal);

“`

其中,retVal是线程返回的参数,该函数不返回值,直接退出线程。

2. 线程同步

线程同步是为了保证多个线程之间的有序执行,以避免由于多个线程并发执行而导致的问题。Linux下提供了多个方法来实现线程同步,如:互斥锁(mutex)、条件变量(condition variable)等。

pthread_mutex_t用于互斥锁的创建,该函数原型如下:

“`

#include

extern int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr);

“`

其中,mutex是指向互斥锁变量的指针,attr是指向互斥锁属性变量的指针。该函数执行成功返回0,否则返回错误码。pthread_mutex_lock()用于互斥锁的加锁,该函数原型如下:

“`

#include

extern int pthread_mutex_lock(pthread_mutex_t* mutex);

“`

其中,mutex是互斥锁变量。pthread_mutex_unlock()用于互斥锁的解锁,该函数原型如下:

“`

#include

extern int pthread_mutex_unlock(pthread_mutex_t* mutex);

“`

其中,mutex是互斥锁变量。

3. 线程池

线程池是一种线程复用的技术,它可以复用一组线程来处理多个任务。线程池中的线程数量可配置,在需要执行任务时,从线程池中取出空闲的线程来处理任务。线程池技术在多线程应用中广泛应用,可以有效降低因创建线程带来的开销,提升程序的执行效率。

Linux下提供了线程池的实现pthreadpool,该线程池库提供了线程池的创建、销毁、任务的添加等操作,可以方便地实现线程池的使用。

三、多线程编程的实践

1. 线程函数的实现

线程函数是一个重要的部分,是执行多线程任务的核心代码,下面通过一个简单的例子来说明线程函数的实现方式。

“`

#include

#include

void* thread_fun(void* arg)

{

printf(“thread start…\n”);

printf(“thread end…\n”);

pthread_exit(NULL);

}

int mn(int argc, char** argv)

{

pthread_t tid;

pthread_create(&tid, NULL, thread_fun, NULL);

pthread_join(tid, NULL);

return 0;

}

“`

在上面的代码中,我们使用pthread_create()函数创建了一个线程,指定了线程函数thread_fun()作为线程的入口点。该线程函数只是简单地打印了一些信息,并最终使用pthread_exit()函数退出线程。

2. 线程同步的实现

线程同步是一个重要的概念,它可以实现多个线程之间的有序执行。下面通过一个简单的例子来说明如何使用互斥锁来实现线程同步。

“`

#include

#include

pthread_mutex_t mutex;

void* thread_fun(void* arg)

{

pthread_mutex_lock(&mutex);

printf(“thread start…\n”);

sleep(2);

printf(“thread end…\n”);

pthread_mutex_unlock(&mutex);

pthread_exit(NULL);

}

int mn(int argc, char** argv)

{

pthread_t tid[10];

pthread_mutex_init(&mutex, NULL);

int i;

for (i = 0; i

{

pthread_create(&tid[i], NULL, thread_fun, NULL);

}

for (i =0; i

{

pthread_join(tid[i], NULL);

}

pthread_mutex_destroy(&mutex);

return 0;

}

“`

在上面的代码中,我们使用了pthread_mutex_lock()函数和pthread_mutex_unlock()函数来实现线程同步。线程函数在执行的时候会先对互斥锁加锁,然后在最后释放互斥锁,以此来保证多个线程之间的有序执行。

3. 线程池的实现

线程池是一种线程复用的技术,能够大大提高程序的运行效率。下面通过一个简单的例子来说明如何使用Linux提供的pthreadpool线程池来实现多线程任务的执行。

“`

#include

#include

void task_fun(void* arg)

{

printf(“task start…\n”);

sleep(2);

printf(“task end…\n”);

}

int mn(int argc, char** argv)

{

pthreadpool_t pool;

pthreadpool_create(&pool, 5);

int i;

for (i = 0; i

{

pthreadpool_add(&pool, task_fun, NULL);

}

pthreadpool_wt(&pool);

pthreadpool_destroy(&pool);

return 0;

}

“`

在上面的代码中,我们创建了一个线程池,设置了5个线程的数量,然后添加了10个任务到线程池中,最后等待线程池中的所有任务执行完成,这里我们使用了pthreadpool_add()函数和pthreadpool_wt()函数来实现任务的添加和等待线程池中任务的执行。

四、

相关问题拓展阅读:

关于linux下多线程编程

pthread_attr_t attr;

pthread_attr_init(&attr);

pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);//禁止join, 分离的线程对象

//线拿亮腔程…前进键带吧….

if(0 == pthread_create( &(thread->id), &attr, wrapper_fn, thread )){//wrapper_fn 是函数指针 thread是函数参数

pool->tp_total++; //池中的已装入线程总数数+1

}else{

ret = -1;

printf(“Can’t create thread\n”);

pthread_mutex_destroy( &(thread->mutex));

pthread_cond_destroy( &(thread->cond));

free(thread);

}

估消衫计是你没有处理join, 我这个线程池封了好久了. 所以对pthread没什么印象了

pthread_join 线程停止等待函数没有调用

pthread_create 线程生成后,没有等子线程停止,主线程就先停止了。

主线程停止后,整个程序停止,子线程在没有printf的时候就被结束宏旅纳了。

结论:不是你没有看到结果,而是在镇凳子线蔽没程printf(“………………\n”);之前整个程序就已经停止了。

#include

#include

#include

#include

#include

#include

#define FALSE -1

#define TRUE 0

void *shuchu( void *dumy )

{

int j;

printf(“………………\n”);

}

int main()

{

int i = 0;

int rc = 0;

int ret1;

pthread_t p_thread1;

if(0!=(ret1 = pthread_create(&p_thread1, NULL, shuchu, NULL)))printf(“sfdfsdfi\n”);

printf(“\n”,p_thread1);

pthread_join(p_thread1, NULL);

return TRUE;

}

main()方法怎么回返回int型值?

而且也看不出多线程在哪啊??

返回列表

上一篇:linux struct 数组

关于linux 多线程实现的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » Linux下的多线程实现—提高程序运行效率 (linux 多线程实现)