您的位置:首页 > 运维架构 > Linux

linux下C/C++,多线程pthread

2015-09-15 19:22 579 查看

linux下C/C++,多线程pthread

 ·线程创建

  函数原型:int
pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void),void
*restrict arg);

  返回值:若是成功建立线程返回0,否则返回错误的编号。

  形式参数:pthread_t *restrict tidp要创建的线程的线程id指针;const pthread_attr_t *restrict attr创建线程时的线程属性;void* (start_rtn)(void)返回值是void类型的指针函数;void *restrict arg start_rtn的形参。

  ·线程挂起:该函数的作用使得当前线程挂起,等待另一个线程返回才继续执行。也就是说当程序运行到这个地方时,程序会先停止,然后等线程id为thread的这个线程返回,然后程序才会断续执行。

  函数原型:int
pthread_join( pthread_t thread, void **value_ptr);

  参数说明如下:thread等待退出线程的线程号;value_ptr退出线程的返回值。

  ·线程退出

  函数原型:void
pthread_exit(void *rval_ptr);

  ·获取当前线程id

  函数原型:pthread_t
pthread_self(void);

  ·互斥锁

  创建pthread_mutex_init;销毁pthread_mutex_destroy;加锁pthread_mutex_lock;解锁pthread_mutex_unlock。

  ·条件锁

  创建pthread_cond_init;销毁pthread_cond_destroy;触发pthread_cond_signal;广播pthread_cond_broadcast S;等待pthread_cond_wait。 

  ·正确处理Linux平台下的线程结束问题

  在Linux平台下,当处理线程结束时需要注意的一个问题就是如何让一个线程善始善终,让其所占资源得到正确释放。在Linux平台默认情况下,虽然各个线程之间是相互独立的,一个线程的终止不会去通知或影响其他的线程。但是已经终止的线程的资源并不会随着线程的终止而得到释放,我们需要调用pthread_join() 来获得另一个线程的终止状态并且释放该线程所占的资源。

【用C】

/* example.c*/

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

void thread(void)

{

int i;

for(i=0;i<3;i++){

sleep(1);

printf("This is a pthread.\n");}

}

int main(void)

{

pthread_t id;

int i,ret;

ret=pthread_create(&id,NULL,(void *) thread,NULL);

if(ret!=0){

printf ("Create pthread error!\n");

exit (1);

}

for(i=0;i<3;i++){

printf("This is the main process.\n");

sleep(1);

}

pthread_join(id,NULL);

return (0);

}

编译命令 gcc -o example example.c -lpthread

注:【1】pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。

【2】还要添加头文件pthread.h

【3】因为C库自带sleep,所以可以不添加unistd.h头文件

【用C++】

/* example.cpp*/

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

#include <unistd.h>

void *thread(void *ptr)

{

int i;

for(i=0;i<3;i++){

sleep(1);

printf("This is a pthread.\n");}

}

int main(void)

{

pthread_t id;

int i,ret;

ret=pthread_create(&id,NULL,thread,NULL);

if(ret!=0){

printf ("Create pthread error!\n");

exit (1);

}

for(i=0;i<3;i++){

printf("This is the main process.\n");

sleep(1);

}

pthread_join(id,NULL);

return (0);

}

编译链接命令 g++ -o example2 example.c -lpthread

【注】 标亮部分,注意C++开发时与C的区别,解释如下:

我们注意在Posix定义建立线程函数的原型:

extern int pthread_create (pthread_t *__restrict __threadp,

__const pthread_attr_t *__restrict __attr,

void *(*__start_routine) (void *),

void *__restrict __arg) __THROW;

这个call中的第三个参数是载入一个函数,这个函数有一个参数可以传入,返回一个 通用指针。

我们再来看看原来函数中是怎样调用这一原型的,基本上就是类似一下的调用方式:

(void
*) thread 或是
(void *) &thread

这个表达式的含义:取一个指向函数main_thread的指针,然后将其转换为一个通用指针。

这就是说显然上述两个事情并非同一个事情,故而正确的调用方式是

ret=pthread_create(&id,NULL,thread,NULL);

处理函数的定义如下:

void *thread(void *ptr)

值得注意的是在gcc编译时不会出错,但是用g++就会有问题:invalid
conversion from `void*' to `void*(*)(void*),究其原因就是C语言编译器允许隐含性的将一个通用指针转换为任意类型的指针,而C++不允许。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: