原文网址:http://www.cnblogs.com/xianghang123/archive/2011/08/11/2134927.html

·线程创建

 

  函数原型: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++不允许。

 
分类: Linux C/C++

linux下C/C++,多线程pthread《转载》的更多相关文章

  1. [转帖]Linux下fork函数及pthread函数的总结

    Linux下fork函数及pthread函数的总结 https://blog.csdn.net/wangdd_199326/article/details/76180514 fork Linux多进程 ...

  2. linux下C语言多线程编程实例

    用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...

  3. Linux下高cpu解决方案(转载)

    Linux下高cpu解决方案(转载 1.用top命令查看哪个进程占用CPU高gateway网关进程14094占用CPU高达891%,这个数值是进程内各个线程占用CPU的累加值.   PID USER  ...

  4. linux 下 多进程与多线程

    [Linux]多进程与多线程之间的区别 http://blog.csdn.net/byrsongqq/article/details/6339240 网络编程中设计并发服务器,使用多进程与多线程 ,请 ...

  5. linux下C++的多线程编程

    1. 引言 线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(proces ...

  6. Linux下不同服务器间数据传输--转载

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...

  7. Linux下history命令详解---转载

    Linux下History命令主要用于显示历史指令记录内容, 下达历史纪录中的指令 . >History命令语法:[www.linuxidc.com@linux]# history [n][ww ...

  8. linux下如何安装软件(转载)

    来源:http://zhidao.baidu.com/link?url=5oR8WxygPvVMhSZvXQahYKm01JPTmQnEUjbQF562Yxgd3r6bYpki1ZPcHAsij6E4 ...

  9. Linux 下安装配置 JDK7(转载)

    转自:http://dawndiy.com/archives/155/ 自从从Oracle收购Sun近三年来,已经有很多变化.早在8月,甲骨文将“Operating System Distributo ...

随机推荐

  1. 转:CI引入外部js与css

    其实不管是在用CI还是ZF都有同样一个问题,就是路径的问题.前期,我在用ZF做CMS时,我在.htaccess文件中设置了如遇到js,css,img等资源文件都不重定向.但今天在用CI时,却忘记了,搞 ...

  2. stm32内部的CAN总线

    功能概述: bxCAN是基本扩展CAN(Basic Extended CAN)的缩写,它支持CAN协议2.0A和2.0B:它的设计目标是以最小的CPU负载来高效处理大量的报文.它也支持报文发送的优先级 ...

  3. 【转载】openCV轮廓操作

    声明:非原创,转载自互联网,有问题联系博主 1.轮廓的提取 从图片中将目标提取出来,常常用到的是提取目标的轮廓. OpenCV里提取目标轮廓的函数是findContours(), 它的输入图像是一幅二 ...

  4. arm: 使用结构体操作寄存器

    使用结构体操作寄存器: //寄存器赋值和取值的时候,要注意寄存器的长度,有的寄存器的值只有8位. //还要注意,使用volatile修饰寄存器变量.volatile 参考http://www.cnbl ...

  5. linux eclipse中运行android AVD 错误

    当使用android的AVD时提示以下错误: Starting emulator for AVD 'NexusOne' ERROR: 32-bit Linux Android emulator bin ...

  6. 演练5-7:Contoso大学校园管理系统(实现继承)

    ***操作视频下载:1     *** 在上一次教程中,你已经能够处理并发异常.这个教程将会展示如何在数据模型中实现继承. 在面向对象的程序设计中,你可以通过继承来清除冗余的代码.在这个教程中,你将要 ...

  7. Android NFC传输联系人VCF

    import android.app.Activity; import android.content.ContentResolver; import android.content.Context; ...

  8. 基于visual Studio2013解决C语言竞赛题之0903文件读写

       题目

  9. 深入理解 Spring 事务原理【转】

    本文转自码农网 – 吴极心原创  连接地址:http://www.codeceo.com/article/spring-transactions.html 一.事务的基本原理 Spring事务的本质其 ...

  10. WebStorm开发Nodejs环境搭建,包括破解最新的WebStom11破解

    先放上链接:http://pan.baidu.com/s/1eQUJZGm 文件内包含注册码和WebStom11安装包,希望能够帮助到大家,少些周折