Linux多线程函数pthread_create()函数

8 篇文章 0 订阅

函数原型:

#include <pthread.h>

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

4个参数:

第一个参数:指向线程标示符pthread_t的指针;

第二个参数:设置线程的属性

第三个参数:线程运行函数的起始地址

第四个参数:运行函数的参数
pthreadTest.c

通过pthread_self()获取当前线程的ID

#include<stdio.h>
#include<pthread.h>

void *print_pthread_id(void *arg)
{
        printf("current thread id is %u\n",(unsigned)pthread_self());
}

int main(int argc, char * argv[])
{

        pthread_t thread;

        pthread_create(&thread,NULL,print_pthread_id,NULL);

        sleep(1);

        printf("main thread id is %u\n",(unsigned)pthread_self());

        return 0;
}
编译时必须加-lpthread,

运行结果:

current thread id is 2279393024
main thread id is 2287691520

pthreadTest1.c

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
pthread_t t_id;
void printf_tid_pid(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)pid,(unsigned)tid,(unsigned)tid);
}

void *thread_fuction(void *arg)
{
        printf_tid_pid("new thread: ");
        return ((void*)0);
}
int main(int argc,int argv)
{
        int err;
        err = pthread_create(&t_id,NULL,thread_fuction,NULL);
        if(err != 0 )
        {
                printf("create thread fail: %s\n",strerror(err));
        }
        printf_tid_pid("main thread");
        sleep(1);
        return 0;
}

 gcc -o pthreadTest1 pthreadTest1.c -lpthread

 ./pthreadTest1
main thread pid 10437 tid 3091662592 (0xb8470700)
new thread:  pid 10437 tid 3083364096 (0xb7c86700)







  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多线程中的pthread_create()函数是用来创建一个新的线程的。它的函数原型为int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)。这个函数通过传入四个参数来创建新的线程。第一个参数thread是一个指向pthread_t类型的指针,用来保存新线程的ID。第二个参数attr是一个指向pthread_attr_t类型的指针,用来指定新线程的属性,如果为NULL则使用默认属性。第三个参数start_routine是一个函数指针,指向新线程将要执行的函数。最后一个参数arg是一个指向任意类型的指针,用来传递给start_routine函数的参数。调用成功返回0,失败返回错误编号。 每个线程都有自己的独立栈来保存线程运行时形成的临时数据。而上下文中保存的是CPU调度时存放在寄存器中的临时数据。线程的标识符类型为pthread_t,可以使用pthread_self()函数获取当前线程的ID,类似于使用getpid()函数获取进程的ID。[2,3]<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Linux线程:创建(pthread_create),等待(pthread_join),退出(pthread_exit)](https://blog.csdn.net/m0_74985965/article/details/128815940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值