Linux多线程基础学习(二)创建新的线程

多线程基础学习(二)创建新的线程

这次博客主要记录以下多线程知识内容(代码运行环境为Linux):

  • 获取Linux中进程与线程的ID
  • 创建一个新的线程
  • 理解一个进程中包含多个线程的实际案例

进程与线程的ID

收到

项目进程线程
标识符类型pthread_tpid_t
获取idpthread_self()getpid()
创建pthread_create()fork()

在Linux中pthread_t是uint类型。
以下是Linux帮助手册对pthread_self的解释:

NAME
       pthread_self - obtain ID of the calling thread
SYNOPSIS
       #include <pthread.h>
       pthread_t pthread_self(void);
       Compile and link with -pthread.
DESCRIPTION
       The pthread_self() function returns the ID of the 
in *thread in the  pthread_create(3) call that created 
this thread.

以下是Linux帮助手册threads 中对Thread IDs的解释:

Thread IDs
Each of the threads in a process has a unique thread identifier (stored in the type pthread_t). This identifier is returned to the caller of pthread_create(3), and a thread can obtain its own thread identifier using pthread_self(3). Thread IDs are only guaranteed to be unique within a process. A thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated. In all pthreads functions that accept a thread ID as an argument, that ID by definition refers to a thread in the same process as the caller.

创建新线程

函数原型:

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

函数原型中各个参数的含义:

第一个参数:新线程的id,如果成功则新线程的id回填充到tidp指向的内存
第二个参数:线程属性(调度策略,继承性,分离性…)
第三个参数:回调函数(新线程要执行的函数)
第四个参数:回调函数的参数
返回值:成功返回0,失败则返回错误码

以下是Linux帮助手册pthread_create中对pthread_create函数的解释:

NAME
       pthread_create - create a new thread
SYNOPSIS
       #include <pthread.h>
       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
       Compile and link with -pthread.

DESCRIPTION
       The  pthread_create()  function  starts  a  new  thread 
by invoking  start_routine();
       arg is passed as the sole argument of start_routine().

       The new thread terminates in one of the following ways:
       * It  calls pthread_exit(3), specifying an exit status value that is available to another  thread in the same process that calls pthread_join(3).

       * It  returns fro  start_routine(). This is equivalent to calling pthread_exit(3) with the value supplied in the return statement.
       * It is canceled (see pthread_cancel(3)).
       * Any of the threads in the process calls exit(3), or the main thread performs  a  return from main(). This causes the termination of all threads in the process.
       The attr argument points to a pthread_attr_t structure  whose  contents are  used  at  thread creation time to determine attributes for the new thread; this structure is initialized using pthread_attr_init(3) and
 related  functions.  If attr is NULL, then the thread is created with default attributes.

       Before returning, a successful call to pthread_create() stores  the  ID of  the  new thread in the buffer pointed to by thread; this identifier is used to refer to the thread in subsequent calls  to  other  pthreads functions.

代码案例

运行环境:Ubuntu、eclipse集成开发环境
实现功能:在main函数中创建一个新的线程,新的线程中以及main函数调用了打印线程以及进程ID的函数。

#include <pthread.h>
#include <iostream>
#include <stdio.h>
//#include <stdlib.h>
//#include <string.h>
//#include <unistd.h>
//#include <sys/types.h>

using namespace std;

#include <pthread.h>
#include <iostream>
using namespace std;

void printThreadID(const char * ThreadName)
{

    cout<<ThreadName<<"'s threadid is:"<<pthread_self();
    cout<<"   "<<"PID is:"<<getpid()<<endl;
}

void* newthread(void * arg)
{
    printThreadID((char*)arg);
    cout<<"newthread argument is "<<(char *)arg<<endl;
    return NULL;
}

int main(int argc, char const *argv[])
{
    pthread_t ptid;

    if(pthread_create(& ptid,
                        NULL,
                        newthread,
                        (void*)"newthread"))
    {
        cout<<"create thread fialed!"<<endl;
    }
    printThreadID("main");
    sleep(1);

    return 0;
}

运行结果:

main’s threadid is:3074275072 PID is:14981
newthread’s threadid is:3074267968 PID is:14981
newthread argument is newthread

结果分析:main函数与新的线程是两个不同的线程,但是同属于一个进程。因此他们的进程ID相同,线程ID不同。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值