http://blog.csdn.net/youbang321/article/details/7815707

原型:int  pthread_create((pthread_t  *thread,  pthread_attr_t  *attr,  void  *(*start_routine)(void  *),  void  *arg)

用法:#include  <pthread.h>

功能:创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。

说明:thread:线程标识符;

attr:线程属性设置;

start_routine:线程函数的起始地址;

arg:传递给start_routine的参数;

返回值:成功,返回0;出错,返回-1。

举例:

  1. /*thread.c*/
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. /*线程一*/
  5. void thread_1(void)
  6. {
  7. int i=0;
  8. for(i=0;i<=6;i++)
  9. {
  10. printf("This is a pthread_1.\n");
  11. if(i==2)
  12. pthread_exit(0);
  13. sleep(1);
  14. }
  15. }
  16. /*线程二*/
  17. void thread_2(void)
  18. {
  19. int i;
  20. for(i=0;i<3;i++)
  21. printf("This is a pthread_2.\n");
  22. pthread_exit(0);
  23. }
  24. int main(void)
  25. {
  26. pthread_t id_1,id_2;
  27. int i,ret;
  28. /*创建线程一*/
  29. ret=pthread_create(&id_1,NULL,(void  *) thread_1,NULL);
  30. if(ret!=0)
  31. {
  32. printf("Create pthread error!\n");
  33. return -1;
  34. }
  35. /*创建线程二*/
  36. ret=pthread_create(&id_2,NULL,(void  *) thread_2,NULL);
  37. if(ret!=0)
  38. {
  39. printf("Create pthread error!\n");
  40. return -1;
  41. }
  42. /*等待线程结束*/
  43. pthread_join(id_1,NULL);
  44. pthread_join(id_2,NULL);
  45. return 0;
  46. }

以下是程序运行结果:

备注:pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在线程函数在编译时,需要连接库函数,如上图    gcc pthread_create.c -o pthread_create -lpthread

 
 

转:pthread_create()的更多相关文章

  1. 线程的创建pthread_create.c

    #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <errno.h&g ...

  2. pthread_create传递参数

    转自:http://blog.csdn.net/yeyuangen/article/details/6757525 #include <iostream> #include <pth ...

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

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

  4. pthread_create线程创建的过程剖析

    http://blog.csdn.net/wangyin159/article/details/47082125 在Linux环境下,pthread库提供的pthread_create()API函数, ...

  5. pthread_detach pthread_join pthread_create

    pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...

  6. 类成员函数作为pthread_create函数参数

    from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数 ...

  7. pthread_create如何传递两个参数以上的参数

    涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程 定义一个结构体 struct mypara { var para1;//参数1 var para2;//参数2 } 将这个结 ...

  8. linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake)

    问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a. 所以在使用pthread_create()创建线程时,需要链接该库. 1. 终端:问题解 ...

  9. undefined reference to `pthread_create'问题解决

    在编译pthread有关的程序时,出现undefined reference to `pthread_create'这样的错误. 问题原因: pthread 库不是 Linux 系统默认的库,连接时需 ...

  10. 在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static

    在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create ...

随机推荐

  1. mount 远程挂载Nfs

    服务器:192.168.20.204 客户端:192.168.20.203 1. 在服务器配置/etc/export 添加可以共享的文件夹和允许的客户端地址 /home/dir 192.168.20. ...

  2. Git详解之二 Git基础

    Git 基础 读完本章你就能上手使用 Git 了.本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令.读完本章,你就能初始化一个新的代码仓库,做一些适当配置: ...

  3. jQuery表单验证以及将表单序列化为json对象小练习

    jquery表单验证(非实时验证),同时,将表单序列化为json对象提交表单. <!DOCTYPE html> <html lang="en"> <h ...

  4. 1st day

    学习配置环境,听的一脸懵逼,不过还好装好了,哈哈哈... <?php /*写一个函数,该函数可以将给定的任意个数的参数以指定的字符串串接起来成为一个长的字符串.该函数带2个或2个以上参数,其中第 ...

  5. escape encodeURI encodeURIComponent区别

    escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串.使用unescape来解码. 有效的URI(统一资源标示符)是不能包含某些字符的,如空格,所以需要进行编码,编码方法有 ...

  6. motan源码分析一:服务发布及注册

    motan是新浪微博开源的服务治理框架,具体介绍请看:http://tech.sina.com.cn/i/2016-05-10/doc-ifxryhhh1869879.shtml. 本系列的文章将分析 ...

  7. 利用CSS边框合并属性打造table细边框

    CSS代码: <style> table{ border-collapse: collapse;/* 边框合并属性 */ width:200px; } th{ border: 1px so ...

  8. Android ProgressBar SeekBar

    1.ProgressBar 进度条 当水平进度条完成后,隐藏该进度条,并显示一张图片 常用属性: 1. android:max 设置进度条的最大值 2. android:progress 用于指定进度 ...

  9. xml--通过DOM解析XML

    此文章通过3个例子表示DOM方式解析XML的用法. 通过DOM解析XML必须要写的3行代码. step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器) step 2:获得具体的dom解 ...

  10. 01标题背包水章 HDU2955——Robberies

    原来是dp[i],它代表的不被抓的概率i这最大的钱抢(可能1-100) 客是dp[i]表示抢了i钱最大的不被抓概率,嗯~,弱菜水题都刷不动. 那么状态转移方程就是 dp[i]=max(dp[i],dp ...