pthread_self和pthread_create函数

头文件

#include <pthread.h>

函数原型

pthread_t pthread_self(void);
int pthread_create(pthread_t *thread tidp, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

参数

第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的起始地址。
最后一个参数是运行函数的参数。

功能

线程可以通过调用pthread_self函数获得自身线程标识。创建线程调用pthread_create函数

说明

新创建的线程是从start_routine函数开始的,此函数只有一个无类型指针参数arg,如果需要向start_routine函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。新创建的线程并不能保证新创建线程和调用线程那个线程会先运行。

unix环境高级编程的例子

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>pthread_t ntid;void printids(const char *s)
{pid_t pid;pthread_t tid;pid = getpid();tid = pthread_self();printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)tid, (unsigned int)tid);
}void *thr_fn(void *arg)
{printids("new thread: ");return((void *)0);
}int main(void)
{int err;err = pthread_create(&ntid, NULL, thr_fn, NULL);if(err != 0){printf("can't create thread: %s\n",strerror(err));}printids("main thread: ");sleep(1);exit(0);}

ps: 因为pthread并非Linux系统的默认库,在编译时注意加上-lpthread参数,以调用静态链接库。如:gcc pthread.c -o pthread_create -lpthread

Linux pthread_self和pthread_create函数相关推荐

  1. 线程控制原语之pthread_self和pthread_create函数

    注意:使用线程库函数用gcc编译时,要加参数:-lpthread(libpthread.so),因为线程库函数属于第三方c库函数,不是标准库函数(/lib./usr/lib或者/usr/local/l ...

  2. linux编程之pthread_create函数

    linux编程之pthread_create函数UNIX环境创建线程函数, 具体格式: #include<pthread.h> int pthread_create(pthread_t * ...

  3. linux 线程创建 pthread_create函数 获取线程id

    函数原型: #include<pthread.h> int  pthread_create(pthread_t*thread,pthread_attr_t   *attr, void * ...

  4. linux线程随笔-pthread_create函数

    函数简介 pthread_create是UNIX环境创建线程函数 头文件 #include<pthread.h> 函数声明 int pthread_create(pthread_t  *r ...

  5. Linux中pthread_create函数的实现

    转:http://blog.sina.com.cn/s/blog_6abf2c040101fpca.html 原文地址:[原]Linux中pthread_create函数的实现作者:jiq408694 ...

  6. Linux中的线程创建函数pthread_create函数

    Linux系统中的多线程遵循POSIX线程接口,成为pthread.pthread_create函数用来创建一个用户线程,函数原型如下. #include <pthread.h>int p ...

  7. Linux / pthread_create() 函数所使用的线程函数为什么必须是静态函数?

    答案:因为 pthread_create() 函数要求的线程函数必须满足如下格式: void *ThreadFunc(void *args); 对于普通类成员函数.虚函数,他们实际上都是包含了调用他们 ...

  8. pthread_create()函数用法

    linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread. #include <pthread.h> int pthread_create(pth ...

  9. linux C语言 常用函数(系统调用等) 持续更新

    文章目录 系统调用是什么 Linux C语言 文件部分系统调用 1.open()打开文件 2.close()关闭文件 3.mkdir()创建目录 4.access()判断路径是否存在 5.fcntl( ...

最新文章

  1. C语言经典例40-逆置数组
  2. java中的md5加密_java中的MD5加密
  3. 为什么要学python语言、学完有什么好处_学编程为什么首选Python?学完Python的优势有哪些?...
  4. “管理压力,控制情绪”培训小结
  5. 尊贵的iPhone 11用户们,苹果喊你们换显示屏模块了……
  6. python计算文件大小的方法_使用Python计算目录的大小?
  7. 【Golang】关于从切片中删除某个元素时会覆盖底层数组的说明
  8. 便利店小程序需要服务器吗,便利店开发小程序的功能
  9. LINUX进程内存占用查看
  10. C++ 第四章 4.1
  11. 杀毒软件误删文件了怎么办?如何恢复被杀毒软件删除的文件
  12. linux ubuntu 加密狗,ubuntu – 将usb加密狗连接到KVM VM
  13. springboot获取到的MySQL数据少了8小时
  14. 冯绍峰晒与韩寒片场邋遢照 自称工地领薪水
  15. 今天,IT女神们是怎么度过的(文末送礼品)
  16. 迪士尼挖角波士顿动力,耗时3年打造漫威英雄机器人,1:1复刻效果堪比CG
  17. PS制作心跳二维码动画 学会后能增加粉丝关注率哦
  18. IDC数据中心机房气体灭火系统知识
  19. 2018北京小学生信息学科普竞赛试题点评
  20. JQuery的属性操作及事件

热门文章

  1. 祝计算机考试成功,计算机考试来啦,深信小编祝大家逢考必过!
  2. linux怎么查看系统版本
  3. java代码启动电脑相机_如何使用java启用电脑的摄像头摄相拍照
  4. 发现一个 很好的网站,可以画漫画。
  5. linux命令chgrp,Linux命令Chgrp 详解
  6. Violence detection-Hockey Fight-CNN+LSTM暴力检测CNN+LSTM实例
  7. 神机百炼1.5-二分
  8. 使用JMeter进行接口压测
  9. 预防I型糖尿病,从婴儿时期肠道菌群出发
  10. 分治算法——二分查找