转:pthread_create()
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。
举例:
- /*thread.c*/
- #include <stdio.h>
- #include <pthread.h>
- /*线程一*/
- void thread_1(void)
- {
- int i=0;
- for(i=0;i<=6;i++)
- {
- printf("This is a pthread_1.\n");
- if(i==2)
- pthread_exit(0);
- sleep(1);
- }
- }
- /*线程二*/
- void thread_2(void)
- {
- int i;
- for(i=0;i<3;i++)
- printf("This is a pthread_2.\n");
- pthread_exit(0);
- }
- int main(void)
- {
- pthread_t id_1,id_2;
- int i,ret;
- /*创建线程一*/
- ret=pthread_create(&id_1,NULL,(void *) thread_1,NULL);
- if(ret!=0)
- {
- printf("Create pthread error!\n");
- return -1;
- }
- /*创建线程二*/
- ret=pthread_create(&id_2,NULL,(void *) thread_2,NULL);
- if(ret!=0)
- {
- printf("Create pthread error!\n");
- return -1;
- }
- /*等待线程结束*/
- pthread_join(id_1,NULL);
- pthread_join(id_2,NULL);
- return 0;
- }
以下是程序运行结果:

备注:pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在线程函数在编译时,需要连接库函数,如上图 gcc pthread_create.c -o pthread_create -lpthread
转:pthread_create()的更多相关文章
- 线程的创建pthread_create.c
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <errno.h&g ...
- pthread_create传递参数
转自:http://blog.csdn.net/yeyuangen/article/details/6757525 #include <iostream> #include <pth ...
- Linux多线程实例练习 - pthread_create()
Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...
- pthread_create线程创建的过程剖析
http://blog.csdn.net/wangyin159/article/details/47082125 在Linux环境下,pthread库提供的pthread_create()API函数, ...
- pthread_detach pthread_join pthread_create
pthread_create:创建线程以后线程直接开始运行: pthread_detach pthread_join:线程资源的释放方式. 创建一个线程默认的状态是joinable, 如果一个线程结束 ...
- 类成员函数作为pthread_create函数参数
from:http://www.cnblogs.com/shijingxiang/articles/5389294.html 近日需要将线程池封装成C++类,类名为Threadpool.在类的成员函数 ...
- pthread_create如何传递两个参数以上的参数
涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程 定义一个结构体 struct mypara { var para1;//参数1 var para2;//参数2 } 将这个结 ...
- linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake)
问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a. 所以在使用pthread_create()创建线程时,需要链接该库. 1. 终端:问题解 ...
- undefined reference to `pthread_create'问题解决
在编译pthread有关的程序时,出现undefined reference to `pthread_create'这样的错误. 问题原因: pthread 库不是 Linux 系统默认的库,连接时需 ...
- 在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static
在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create ...
随机推荐
- has-a关系——包含对象成员的类
#ifndef _STUDENT_H_ #define _STUDENT_H_ #include <iostream> #include <string> #include & ...
- 在DataTable和DataView中查找指定记录
一.在DataTable中查找 1. 使用Select方法查找没有主键的表DataTable的Select方法返回一个DataRow数组,有四个重载的函数. DataRow[] drs = dt.Se ...
- VLC网页插件添加对火狐浏览器的支持
原文转自:http://blog.csdn.net/gsls200808/article/details/25536113 1.用<embed>标签 下面这段代码只支持火狐,不支持IE & ...
- Object-C 对象 (创建/销毁 对象)-- 笔记
创建/销毁 对象: Dog *dog = [Dog alloc]; // 通过alloc创建dog一个这样的对象, alloc相对于C语言中的new // *号既表示指针,也表示引用 初始化构造函数 ...
- Media层
媒体层包含图形技术.音频技术和视频技术,这些技术相互结合就可为移动设备带来最好的多媒体体验,更重要的是,它们让创建外观音效俱佳的应用程序变得更加容易.您可以使用iOS的高级框架更快速地创建高级的图形和 ...
- Android Studio中解决Gradle DSL method not found: 'android()'
近期导入as的项目出了这种问题 这个问题困扰了我非常长时间,好吧,搜了半天全都是runProguard的.最后在stackoverflow上搜到解决的方法了: http://stackoverflow ...
- [转] vim 正则表达式 很强大
毋庸多言,在vim中正则表达式得到了十分广泛的应用. 最常用的 / 和 :s 命令中,正则表达式都是不可或缺的. 下面对vim中的正则表达式的一些难点进行说明. 关于magic vim中有个magic ...
- 2015 UESTC Winter Training #7【2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest】
2015 UESTC Winter Training #7 2010-2011 Petrozavodsk Winter Training Camp, Saratov State U Contest 据 ...
- mui实现退出当前应用
var first = null; var showMenu = false; mui.back = function() { if(showMenu) { closeMenu();} else { ...
- 数据库 SQL 外键约束 多表查询
多表设计与多表查询 1.外键约束 表是用来保存现实生活中的数据的,而现实生活中数据和数据之间往往具有一定的关系,我们在使用表来存储数据时,可以明确的声明表和表之前的依赖关系,命令数据库来 ...