pthread_create函数

原型: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. Java 设计模式实现 不错的引用

    这段时间有兴趣重新温习一下设计模式在Java中的实现,碰巧看到一个不错的设计模式总结,这里引用一下作为参考. 创建型模式: JAVA设计模式-Singleton JAVA设计模式-Factory JA ...

  2. 创建一个Android工程

    Creating an Android Project 原文演示了怎么通过Android Studio和命令行两种方式来创建一个Android工程. 原文链接:http://developer.and ...

  3. K - The Unique MST - poj 1679

    题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...

  4. foreach as 和 in_array

    1.$arr = array("one", "two", "three"); foreach ($arr as $value) { echo ...

  5. iOS内存管理(一)

    最近有时间,正好把iOS相关的基础知识好好的梳理了一下,记录一下内存相关方面的知识. 在理解内存管理之前我觉得先对堆区和栈区有一定的了解是非常有必要的. 栈区:就是由编译器自动管理内存分配,释放过程的 ...

  6. IOS开发之Post 方式获取服务器数据

    //1.创建post方式的 参数字符串url +(NSString *)createPostURL:(NSMutableDictionary *)params { NSString *postStri ...

  7. ios开发应用内实现多语言自由切换

    需求描述:应用内部有一按钮,点击切换语言(如中英文切换).说起来这个是好久以前做的一个功能点了,刚开始也是没有头绪,后来解决了发现很简单,把方法分享一下.1.原理.查看NSLocalizedStrin ...

  8. Linux磁盘设备文件(sda,sdb,sdc…)变化问题

    在Linux下往往会碰到这样的问题,磁盘的设备文件,比如/dev/sda, sdb, sdc等等在某些情况下会混乱掉,比如sda变成了sdb或者sdc变成了sdb等等,这样无形中会导致磁盘设备管理的混 ...

  9. java 类加载过程

    1. 使用命令行查看类加载过程,在eclipse测试类的run configuration中配置-verbose:class或者-verbose,如下图所示: 运行结果如下所示: [Opened D: ...

  10. [转] 用source命令执行脚本和用sh执行脚本之间的区别

    from: http://blog.csdn.net/david_xtd/article/details/8012627 问题: 有很多方式可以执行脚本, 1).source test.bsh 2). ...