转: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 ...
随机推荐
- C#获取字符串生成图片后的长度
1. 使用g.MeasureString()获得 使用MeasureString测量出来的字符宽度,总是比实际宽度大一些,而且随着字符的长度增大,貌似实际宽度和测量宽度的差距也越来越大了.查了一 ...
- Mysql 的字符编码机制、中文乱码问题及解决方案【转载】
本文转载自:http://hi.baidu.com/huabinyin/item/7f51e462df565c97c4d24929.感谢作者及相关博主. 相信很多朋友都会对字符编码敬而远 ...
- Back to Basics: Using KVO
One of the things I like most about Apple’s iOS SDK is the consistent and easy-to-use API they provi ...
- 【COM学习】之一、QueryInterface
开始先说一句,学习com之前要学好c++ 对象模型. QueryInterface的使用: QueryInterface是IUnknown的一个成员函数,客户可以通过此函数来查询某个组件是否支持某个特 ...
- mvc系统过滤器
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- windows 消息机制的学习
概述 链接1:http://www.cppblog.com/suiaiguo/archive/2009/07/18/90412.html 链接2:http://www.cnblogs.com/find ...
- Android ViewFlow的一个例子
完成这个例子的步骤: 1.下载ViewFlow的源码,然后将类ViewFlow放在自己的工程的src的某个包下. 2.下载的源码里有2个工程view flow,viewflow-example.将vi ...
- linux mysql 数据目录文件夹移动及所遇到的问题
一 .如果是fedora下用rpm包安装的mysql,修改方法如下: 如果这里说的不够清楚,可以到http://www.vipkj.net/post-839.html给我留言 MySQL默认的数据文件 ...
- RHEL7下PXE+FTP+Kickstart无人值守安装操作系统
1.配置yum源 vim /etc/yum.repos.d/development.repo [development] name=yum server baseurl=file:///mnt ena ...
- JVM 内存分为四大块
1.栈区 存放函数参数值和局部变量值 2.堆区 3.静态区 4.代码区