可以通过 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()创建线程的更多相关文章

  1. clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结(转)

    进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合,这些资源在Linux中被抽 象成各种数据对象:进程控制块.虚存空间.文件系统,文件I/O.信号处理函数.所以创建一个进程的 过程就是这 ...

  2. pthread_create()创建线程时传入多个參数

    因为接口仅仅定义了一个入參void *arg int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start ...

  3. POSIX多线程之创建线程pthread_create && 线程清理pthread_cleanup

    多线程之pthread_create创建线程 pthreads定义了一套C程序语言类型.函数.与常量.以pthread.h和一个线程库实现. 数据类型: pthread_t:线程句柄 pthread_ ...

  4. 在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static

    在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create ...

  5. [笔记]linux下和windows下的 创建线程函数

    linux下和windows下的 创建线程函数 #ifdef __GNUC__ //Linux #include <pthread.h> #define CreateThreadEx(ti ...

  6. iOS开发多线程篇—创建线程

    iOS开发多线程篇—创建线程 一.创建和启动线程简单说明 一个NSThread对象就代表一条线程 创建.启动线程 (1) NSThread *thread = [[NSThread alloc] in ...

  7. 创建线程方式-pthread

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. Linux 进程与线程三(线程比较--创建线程参数)

    int pthread_equal(pthread_t th1,pthread_t th2); pthread_equal函数比较th1与th2是否为同一线程,由于不可以讲pthread_t数据类型认 ...

  9. 【转】用Pthread创建线程的一个简单Demo

    一.我们直接在COCOS2D-X自带的HelloWorld工程中添加代码.首先将Pthread的文件包含进来包括lib文件.在HelloWorld.cpp中引入头文件和库. #include &quo ...

随机推荐

  1. Python3 爬取微信好友基本信息,并进行数据清洗

    Python3 爬取微信好友基本信息,并进行数据清洗 1,登录获取好友基础信息: 好友的获取方法为get_friends,将会返回完整的好友列表. 其中每个好友为一个字典 列表的第一项为本人的账号信息 ...

  2. map set iterator not incrementable 解决办法

    例子: #include <iostream> #include <map> using namespace std; int main() { map<int, int ...

  3. 使用fragment添加底部导航栏

    切记:fragment一定要放在framlayout中,不然不会被替换完全(就是切换之后原来的fagment可能还会存在) main.xml <LinearLayout xmlns:androi ...

  4. Codeforces Round #426 (Div. 2) C. The Meaningless Game

    C. The Meaningless Game 题意: 两个人刚刚开始游戏的时候的分数, 都是一分, 然后随机一个人的分数扩大k倍,另一个扩大k的平方倍, 问给你一组最后得分,问能不能通过游戏得到这样 ...

  5. 如何给域名配置https证书

    http和https的区别就是,后者在网络传输过程中会很安全,原因就是给http安装了SSL证书. SSL证书有免费的和收费的,收费的顾名思义就是保护的方面更多,更安全.收费的像单域名的一年有几百.几 ...

  6. UVa 11636 你好 世界!(贪心)

    https://vjudge.net/problem/UVA-11636 题意: 经过一次复制,一条语句会变成两条语句,再经过一次变成四条语句...求最少需要复制几次能使条数恰好为n? 思路: 贪心水 ...

  7. c++ 查找数组或者容器元素是否存在(find)

    #include <iostream> // cout #include <algorithm> // find #include <vector> // vect ...

  8. ubuntu 14.04(desktop amd 64) nginx 安装启动停止

    sudo apt-get install nginx 关闭: sudo service nginx stop 启动: sudo nginx

  9. java 基本数据类型及自动类型提升

    1.Java的8种基本数据类型及其所占空间大小: boolean  8bit/1byte       byte      8bit/1byte       char      16bit/2byte ...

  10. docker 出现 Error response from daemon

    第一步:通过dig @114.114.114.114 registry-1.docker.io找到可用IP navy@deepin:~/Desktop$ dig @.docker.io ; <& ...