pthread_create 所需要的时间

 

实际上pthread_create  应该是比较快的,但是从线程的创建到线程的调度的时间,取决于操作系统的调度算法以及硬件的性能。

但是我还是想有个大概的概念,到底是us级的还是ms级别的?

#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
void *test(void* n)
{
        //printf("thread%p:pid:%ld\n", pthread_self(), (long int)n);
        if(n == 0){
                return 0;
        }
        pthread_t id;
        if(pthread_create(&id, NULL, test, --n)) {
                fprintf(stderr, "Error creating thread\n");
                exit(0);
        }
        pthread_join(id, 0);

        return 0;
}
int main()
{
        struct timeval tv0, tv1;
        struct timezone tz0, tz1;
        gettimeofday(&tv0, &tz0);
        test((void*)100);
        gettimeofday(&tv1, &tz1);

        long int a = (tv1.tv_sec - tv0.tv_sec)*1000000 + tv1.tv_usec - tv0.tv_usec;
        printf("time =%lfms\n", (double)a/1000);

}

time =6.976000ms

从这里看,一个线程的生命周期差不多为70us

 

再看第二个测试代码:

void * kkk(void * n)
{
        printf("%ld\n", (long int)n);
        return 0;
}
void *test2()
{
        struct timeval tv0, tv1;
        struct timezone tz0, tz1;

        gettimeofday(&tv0, &tz0);

        pthread_t id[100];
        for(int i = 0; i < 100; i++){
                if(pthread_create(&id[i], NULL, kkk, (void*)i)){
                        fprintf(stderr, "Error creating thread\n");
                        exit(0);
                }
        }

        gettimeofday(&tv1, &tz1);
        long int a = (tv1.tv_sec - tv0.tv_sec)*1000000 + tv1.tv_usec - tv0.tv_usec;
        printf("time =%lfms\n", (double)a/1000);

        sleep(1);
        gettimeofday(&tv0, &tz0);
        for(int i = 0; i < 100; i++){
                pthread_join(id[i], 0);
        }
        gettimeofday(&tv1, &tz1);
        a = (tv1.tv_sec - tv0.tv_sec)*1000000 + tv1.tv_usec - tv0.tv_usec;
        printf("time =%lfms\n", (double)a/1000);

}
int main()
{
        test2();
        return 0;

}

执行结果:
0
3
2
1
4
7
8
6
9
14

....

.....
time =3.199000ms
99
time =0.533000ms

从上面结果来看,有些线程先创建到时后面才被调度,但从创建到被调度,大约为30us。

因为pthread 创建后立即执行,所以临时无法知道创建的时间,估计应该是10us数量级吧。

根据提供的引用内容,我们可以得知`pthread_create_wrapper`是一个函数的名称,该函数用来创建线程。在引用中,报错信息显示`undefined reference to pthread_create`,这意味着编译器无法找到`pthread_create`函数的定义。通常情况下,需要在代码中引入`pthread`库来解决这个问题,这也可以在引用中看到。在这个例子中,使用了`target_link_libraries(cpp_thread pthread)`来链接`pthread`库。 至于具体的`pthread_create_wrapper`函数的实现细节,由于引用内容中没有给出相关信息,我无法提供更多具体的解答。你可能需要查看你的代码,以确定`pthread_create_wrapper`函数是如何被定义和使用的。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [cmake undefined reference to `pthread_create](https://blog.csdn.net/sexyluna/article/details/123529290)[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: 50%"] - *2* [编写程序测量 pthread_create、fork 两个函数的运行时间,并进行实测比较](https://blog.csdn.net/enjoy_code_/article/details/105745101)[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: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值