使用pthread_create()创建线程
可以通过 pthread_create()函数创建新线程。
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp,
const pthread_attr_t *restrict attr,
void *(*start_rtn)(void *),
void *restrict arg);
返回值:
若成功,返回0;否则,返回错误编码
参数说明:
- tidp:新创建的线程ID会被设置成tidp指向的内存单元。
- attr:用于定制各种不能的线程属性,默认为
NULL - start_rtn:新创建的线程从
start_rtn函数的地址开始运行,该函数只有一个void类型的指针参数即arg,如果start_rtn需要多个参数,可以将参数放入一个结构中,然后将结构的地址作为arg传入。
pthread函数在调用失败后通常会返回错误码,但不会设置errno
我们看下面一个例子,该示例中,程序创建了一个线程,打印了进程ID、新线程的线程ID以及初始线程的线程ID。
#include <pthread.h>
pthread_t ntid;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid, (unsigned long)tid, (unsigned long)tid);
}
void* thr_fn(void *arg)
{
printids("new thread:");
return((void*)0);
}
int main()
{
int err;
err = pthread_create(&ntid, NULL, thr_fn, NULL);
if(err!=0)
{
printf("can't create thread\n");
exit(1);
}
printids("main thread:");
sleep(2);
exit(0);
}
运行结果如下:
main thread: pid 13019 tid 139937898653440 (0x7f45d4bd6700)
new thread: pid 13019 tid 139937890158336 (0x7f45d43bc700)
正如我们的期望,进程ID相同10310,线程ID不同。
主线程如果不休眠,有可能在新线程执行之前就退出了。
如下是去掉后的再次执行结果,很明显,第一次执行时,新线程没有机会运行:
➜ tmp ./pt
main thread: pid 13113 tid 139742167656192 (0x7f1842436700)
➜ tmp ./pt
main thread: pid 13119 tid 139768977393408 (0x7f1e803f8700)
new thread: pid new thread: pid 13119 tid 139768968922880 (0x7f1e7fbe4700)
上面示例中,我们使用pthread_self()函数获得线程的ID
使用pthread_create()创建线程的更多相关文章
- clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结(转)
进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合,这些资源在Linux中被抽 象成各种数据对象:进程控制块.虚存空间.文件系统,文件I/O.信号处理函数.所以创建一个进程的 过程就是这 ...
- pthread_create()创建线程时传入多个參数
因为接口仅仅定义了一个入參void *arg int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start ...
- POSIX多线程之创建线程pthread_create && 线程清理pthread_cleanup
多线程之pthread_create创建线程 pthreads定义了一套C程序语言类型.函数.与常量.以pthread.h和一个线程库实现. 数据类型: pthread_t:线程句柄 pthread_ ...
- 在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static
在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create ...
- [笔记]linux下和windows下的 创建线程函数
linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...
- iOS开发多线程篇—创建线程
iOS开发多线程篇—创建线程 一.创建和启动线程简单说明 一个NSThread对象就代表一条线程 创建.启动线程 (1) NSThread *thread = [[NSThread alloc] in ...
- 创建线程方式-pthread
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Linux 进程与线程三(线程比较--创建线程参数)
int pthread_equal(pthread_t th1,pthread_t th2); pthread_equal函数比较th1与th2是否为同一线程,由于不可以讲pthread_t数据类型认 ...
- 【转】用Pthread创建线程的一个简单Demo
一.我们直接在COCOS2D-X自带的HelloWorld工程中添加代码.首先将Pthread的文件包含进来包括lib文件.在HelloWorld.cpp中引入头文件和库. #include &quo ...
随机推荐
- C_Learning(2)
/指针 /指针变量指向一个变量的地址 /给指针变量赋的值只能是地址 /指针变量的赋值 /{ int a; int *p; p=&a; } or { int a; int *p=&a; ...
- 20165310java_blog_week6
2165310 <Java程序设计>第6周学习总结 教材学习内容总结 String 构造 String str=new String() String (char a[]) String ...
- Duilib 实现右下角弹出像QQ新闻窗口,3秒后自动关闭(一)
转载:https://blog.twofei.com/667/ 自绘或子类化控件时,有时需要处理鼠标进入(MouseEnter)/鼠标离开(MouseLeave)/鼠标停留(MouseHover)消息 ...
- P1600 天天爱跑步
lca真心不太会,这里只介绍60分做法,100的太难辣简单了就不介绍了 n<=1000 zz回溯爆搜 S[i]全部相等 这dfs序都不用lca的,2333,差分,然后输出判断一下是否是0(1到i ...
- UVa 437 巴比伦塔
https://vjudge.net/problem/UVA-437 这道题和HDU的Monkey and Banana完全一样. #include<iostream> #include& ...
- ThreadPool开启多线程时支持最大连接200个(默认为2个),不加则会超时
//ThreadPool System.Net.ServicePointManager.DefaultConnectionLimit = 200;
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- python 阶乘
product= i= : product=i*product print('i=%d' %i,end='') print('\tproduct=%d' %product) i+= print('\n ...
- shell while 语句
普通循环格式: while condition do command done 例子一 #!/bin/bash )) do echo $int let "int++" done 结 ...
- STL_算法_06_遍历算法
◆ 常用的遍历算法: 1.1.用指定函数依次对指定范围内所有元素进行迭代访问.该函数不得修改序列中的元素 functor for_each(iteratorBegin, iteratorEnd, fu ...