线程的创建,pthread_create,pthread_self,pthread_once

邶风 邶风     2022-08-23     269

关键词:

typedef unsigned long int pthread_t;
//come from /usr/include/bits/pthreadtypes.h

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);创建新的线程

 

pthread_t pthread_self(void);获取本线程的线程ID

 

int pthread_equal(pthread_t t1, pthread_t t2);判断两个线程ID是否指向同一线程

 

int pthread_once(pthread_once_t *once_control, void (*init_routine) (void));用来保证init_routine线程函数在进程中只执行一次。

 

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



int* thread_func(void* arg)
{
    pthread_t new_thid;
    new_thid = pthread_self();//打印线程自己的线程ID
    printf("the new thread, ID is %lu
", new_thid);

    return NULL;
}


int main()
{
    pthread_t thid;

    printf("main thread, ID is %lu
", pthread_self());//打印主线程自己的线程ID

    if (pthread_create(&thid, NULL, (void*)thread_func, NULL) != 0)
    {
        printf("create thread failed
");
        exit(0);
    }

    
    sleep(5);

    return 0;
}

 

某些情况下,函数执行次数要被限制为1次,这种情况下,要使用pthread_once,代码示例:

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


pthread_once_t once = PTHREAD_ONCE_INIT;

void run(void)
{
    printf("function run is running in thread:%lu
", pthread_self());
}


int* thread1(void* arg)
{
    pthread_t new_thid;
    new_thid = pthread_self();
    printf("current thread ID is %lu
", new_thid);
    pthread_once(&once, run);
    printf("thread1 end
");
    return NULL;
}

int* thread2(void* arg)
{
    pthread_t new_thid;
    new_thid = pthread_self();
    printf("current thread ID is %lu
", new_thid);
    pthread_once(&once, run);
    printf("thread2 end
");
    return NULL;
}

int main()
{
    pthread_t thid1, thid2;

    printf("main thread, ID is %lu
", pthread_self());

    pthread_create(&thid1, NULL, (void*)thread1, NULL);
    pthread_create(&thid2, NULL, (void*)thread2, NULL);
    
    sleep(5);    
    printf("main thread exit
");

    return 0;
}

运行结果:

main thread, ID is 3076200128
current thread ID is 3067804480
function run is running in thread:3067804480
thread2 end
current thread ID is 3076197184
thread1 end
main thread exit

虽然在thread1 跟thread2中都调用了run函数,但是run函数只执行了一次。

多线程之pthread_create()函数(代码片段)

总述:pthread_create是(Unix、Linux、MacOSX)等操作系统的创建线程的函数。它的功能是创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。pthread_create的... 查看详情

多线程之pthread_create()函数(代码片段)

总述:pthread_create是(Unix、Linux、MacOSX)等操作系统的创建线程的函数。它的功能是创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。pthread_create的... 查看详情

pthread_create()函数详解

pthread_create是类Unix操作系统(Unix、Linux、MacOSX等)的创建线程的函数。它的功能是创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。 头文件:#include<pthread.h> 函... 查看详情

linux创建线程之pthread_create(代码片段)

转自:https://www.cnblogs.com/amanlikethis/p/5537175.html  函数简介  pthread_create是UNIX环境创建线程函数头文件  #include<pthread.h>函数声明  intpthread_create(pthread_t *restricttidp,constpthread_attr_ 查看详情

pthread_create() 之后线程的好处是啥?

】pthread_create()之后线程的好处是啥?【英文标题】:Whatisthenicevalueofathreadafterpthread_create()?pthread_create()之后线程的好处是什么?【发布时间】:2021-11-2918:55:29【问题描述】:我正在使用libx264库将视频数据压缩到...x264。我使用默... 查看详情

C++:使用 pthread_create 创建新线程,以运行类成员函数

】C++:使用pthread_create创建新线程,以运行类成员函数【英文标题】:C++:Creatingnewthreadusingpthread_create,torunaclassmemberfunction【发布时间】:2012-08-2813:20:40【问题描述】:我有以下课程:classAprivate:intstarter()//TO_DO:pthread_create()void*thread... 查看详情

C++:使用 pthread_create 创建新线程,以运行类成员函数

】C++:使用pthread_create创建新线程,以运行类成员函数【英文标题】:C++:Creatingnewthreadusingpthread_create,torunaclassmemberfunction【发布时间】:2012-08-2813:20:40【问题描述】:我有以下课程:classAprivate:intstarter()//TO_DO:pthread_create()void*thread... 查看详情

线程的创建,pthread_create,pthread_self,pthread_once

typedefunsignedlongintpthread_t;//comefrom/usr/include/bits/pthreadtypes.hintpthread_create(pthread_t*thread,constpthread_attr_t*attr,void*(*start_routine)(void*),void*arg);创建新的线程 pthread_tpthrea 查看详情

C 强制线程在 pthread_create 之后立即启动。 pthread_create 之后的 pthread_yield?

】C强制线程在pthread_create之后立即启动。pthread_create之后的pthread_yield?【英文标题】:Cforcethethreadtostartimmediatelyafterpthread_create.pthread_yieldafterpthread_create?【发布时间】:2016-01-1816:32:55【问题描述】:我正在努力实现以下目标:强... 查看详情

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

因为接口仅仅定义了一个入參void*argintpthread_create(pthread_t*tidp,constpthread_attr_t*attr,(void*)(*start_rtn)(void*),void*arg);所以,假设想传參数,须要封装结构体。将多个參数通过一个结构体传入线程。 typedefstruct{FUNCPTRentry;/*函数入口*/void... 查看详情

linuxc语言pthread_create()创建线程失败的handle_error_en宏(取自man文档demo)errnoperror()

#definehandle_error_en(en,msg)\\doerrno=en;perror(msg);exit(EXIT_FAILURE);while(0)s=pthread_create(&thr,NULL,thread_start,NULL);if(s!=0)handle_error_en(s,"pthread_create");参考文章 查看详情

linuxc语言pthread_create()创建线程失败的handle_error_en宏(取自man文档demo)errnoperror()

#definehandle_error_en(en,msg)\\doerrno=en;perror(msg);exit(EXIT_FAILURE);while(0)s=pthread_create(&thr,NULL,thread_start,NULL);if(s!=0)handle_error_en(s,"pthread_create");参考文章 查看详情

当 std::lock_guard 仍在范围内时,使用 pthread_create 创建线程是不是安全?

】当std::lock_guard仍在范围内时,使用pthread_create创建线程是不是安全?【英文标题】:Isitsafetocreateathreadusingpthread_createwhenstd::lock_guardisstillinscope?当std::lock_guard仍在范围内时,使用pthread_create创建线程是否安全?【发布时间】:2019... 查看详情

c语言多线程教程(pthread)(线程创建pthread_t,指定线程run方法pthread_create,加mutex锁,解锁,伪共享falsesharing假共享)(代码片段)

[C语言]多线程程序入门教程文章目录查看pthread_create()函数文档·Demo1单线程(创建线程pthread_t、创建线程run方法pthread_create)·Demo2双线程(一个打印1,一个打印2)pthread_create()函数第四个参数,... 查看详情

使用 pthread_create 时出现“分段错误(核心转储)”

】使用pthread_create时出现“分段错误(核心转储)”【英文标题】:"Segmentationfault(coredumped)"whileusingpthread_create【发布时间】:2017-12-1722:41:44【问题描述】:所以我遇到了一个问题:当我尝试创建最后一个线程时,它总是... 查看详情

pthread_create的介绍

参考技术Apthread_create是类Unix操作系统(Unix、Linux、MacOSX等)的创建线程的函数。 查看详情

c语言怎么创建线程和使用

...程相关的头文件:#include<pthread.h>2、线程创建函数是pthread_create()函数,该函数的原型为:int pthread_create(pthread_t *thread,pthread_attr_t *attr,void* (*start_routine)(void*),void *arg);3、线程退出函数是pthread_exit()函数,该... 查看详情

linux线程操作问题undefinedreferenceto'pthread_create'的解决办法(cmake)

...默认的库,连接时需要使用静态库libpthread.a.所以在使用pthread_create()创建线程时,需要链接该库。 1.终端:问题解决:在编译中要加-pthread参数gccthread.c-othread-pthread   2.qt的cmake配置:可以修改CMakeLists.txt:Hereist 查看详情