说明:本文转自多线程编程之pthread_create函数应用,在此基础上笔者做了些许改动。

                             pthread_create函数

函数简介

  pthread_create是UNIX环境创建线程函数

头文件

  #include<pthread.h>

函数声明

  int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

返回值

  若成功则返回0,否则返回出错编号

参数

  第一个参数为指向线程标识符的指针。

  第二个参数用来设置线程属性。

  第三个参数是线程运行函数的地址。

  最后一个参数是运行函数的参数。

注意

  在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库。

                             pthread_join函数

函数简介

  函数pthread_join用来等待一个线程的结束。

函数原型为:

  extern int pthread_join __P (pthread_t __th, void **__thread_return);

参数:

  第一个参数为被等待的线程标识符

  第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。

注意

这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。

例子:

 #include<stdio.h>
#include<stdlib.h>
#include<pthread.h> /* 声明结构体 */
struct member
{
int num;
char *name;
}; /* 定义线程pthread */
static void * pthread(void *arg)
{
struct member *temp; /* 线程pthread开始运行 */
printf("pthread start!\n"); /* 令主线程继续执行 */
sleep(); /* 打印传入参数 */
temp = (struct member *)arg;
printf("member->num:%d\n",temp->num);
printf("member->name:%s\n",temp->name); return NULL;
} /* main函数 */
int main(int agrc,char* argv[])
{
pthread_t tidp;
struct member *b; /* 为结构体变量b赋值 */
b = (struct member *)malloc(sizeof(struct member));
b->num=;
b->name="mlq"; /* 创建线程pthread */
if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -)
{
printf("create error!\n");
return ;
} /* 令线程pthread先运行 */
sleep(); /* 线程pthread睡眠2s,此时main可以先执行 */
printf("mian continue!\n"); /* 等待线程pthread释放 */
if (pthread_join(tidp, NULL))
{
printf("thread is not exit...\n");
return -;
} return ;
}

编译与执行结果

    编译与执行结果如下图所示,可以看到主线程main和线程pthread交替执行。也就是说是当我们创建了线程pthread之后,两个线程都在执行,证明创建成功。另外,可以看到创建线程pthread时候,传入的参数被正确打印。

linux创建线程之pthread_create的更多相关文章

  1. 多线程编程(三)--创建线程之Thread VS Runnable

    前面写过一篇基础的创建多线程的博文: 那么本篇博文主要来对照一下这两种创建线程的差别. 继承Thread类: 还拿上篇博客的样例来说: 四个线程各自卖各自的票,说明四个线程之间没有共享,是独立的线程. ...

  2. iOS多线程之8.NSOPeration的其他用法

      本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...

  3. python 线程之_thread

    python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...

  4. Linux多线程实例练习 - pthread_create()

    Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...

  5. Java多线程之ConcurrentSkipListMap深入分析(转)

    Java多线程之ConcurrentSkipListMap深入分析   一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下, ...

  6. kali linux 系列教程之metasploit 连接postgresql可能遇见的问题

    kali linux 系列教程之metasploit 连接postgresql可能遇见的问题 文/玄魂   目录 kali linux 下metasploit 连接postgresql可能遇见的问题. ...

  7. Kali Linux系列教程之OpenVas安装

    Kali Linux系列教程之OpenVas安装 文 /玄魂 目录 Kali Linux系列教程之OpenVas安装 前言 1.  服务器层组件 2.客户层组件 安装过程 Initial setup ...

  8. 【C#】线程之Parallel

    在一些常见的编程情形中,使用任务也许能提升性能.为了简化变成,静态类System.Threading.Tasks.Parallel封装了这些常见的情形,它内部使用Task对象. Parallel.Fo ...

  9. linux创建用户和用户组

    Linux创建用户.用户组 及 删除 在创建用户时,需要为新建用户指定一用户组,如果不指定其用户所属的工作组,自动会生成一个与用户名同名的工作组.创建用户user1的时候指定其所属工作组users,例 ...

随机推荐

  1. linux-启动停止重启shell 简单shell示例

    停止: #!/bin/bashpid=`ps -ef|grep /opt/lampp|grep -v grep|awk '{print $2}'|wc -l`b=0if [ $pid -gt $b ] ...

  2. Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed

    最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了Th ...

  3. BZOJ 1040: [ZJOI2008]骑士 基环加外向树

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1190  Solved: 465[Submit][Status] ...

  4. Linux 进程管理子系统

    一.进程管理子系统 1.进程要素 (1). 程序与进程 程序:存放在磁盘上的一系列代码和数据的可执行映像,是一个静止的实体 进程:是一个执行中的程序,他是一个动态的实体. (2). 进程4要素 1.有 ...

  5. P1111 修复公路

    P1111 修复公路 550通过 1.6K提交 题目提供者该用户不存在 标签并查集 难度普及/提高- 提交该题 讨论 题解 记录   题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通 ...

  6. Fuck Sharepoint 2013

    最近遇到一个貌似是bug的问题,每次点击页面的时候页面的地址多出一行/_layouts/15/start.aspx#/ 然后跑到google上搜索出解决方案, 地址:http://social.tec ...

  7. 写过的HTML标签(一)

    HTML >   标题显示字体大小为<h1>. HTML 段落是通过标签 <p> 来定义的. HTML 链接是通过标签 <a> 来定义的.  实例: < ...

  8. 全新jquery多点滑动幻灯片——全屏动画animateSlide

    首页banner的酷炫效果多来自全屏大图的幻灯片动画,下面提供一种完美兼容的jquery动画特效:全新jquery多点滑动幻灯片——全屏动画animateSlide(代码完全原创). 直接上代码,把h ...

  9. MongoDB - MongoDB CRUD Operations, Query Documents

    Query Method MongoDB provides the db.collection.find() method to read documents from a collection. T ...

  10. AjaxPro.2使用小结

    这是我最近没事的时候研究的东东,使用AjaxPro.2.dll,从前台调用后台Ajax方法,希望对各位亲有帮助哦.. 1.首先将AjaxPro.2.dll从网上下载下来,打开VS项目,点击项目,右键- ...