Ubuntu,Kylin环境使用clock()函数设置延迟

 一、Ubuntu操作系统中,直接在main中测试clock()设置延迟功能

代码描述:直接在main中使用clock()函数设置200ms延迟。

代码输出: 实现了200ms的延迟。

#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    unsigned long tick_start, tick_current, tick_start_i;
    tick_start = clock();
    tick_current = clock();

    struct timeval tv;
    gettimeofday(&tv, NULL);
    long long milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
    printf("current time in milliseconds: %lld, %lld\n", milliseconds, (long long int)CLOCKS_PER_SEC);

    while(tick_current - tick_start < (200 * CLOCKS_PER_SEC / 1000))
    {
        tick_current = clock();
    }

    gettimeofday(&tv, NULL);
    long long milliseconds2 = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
    printf("current time in milliseconds: %lld\n", milliseconds2);
    printf("eclipsed time is : %lld\n", milliseconds2 - milliseconds);

    return 0;
}

打印输出如下:  

二、Ubuntu操作系统中,在子线程中使用clock()函数设置延迟

需求:在main函数中,创建线程,在线程中通过clock()函数设置200ms延迟,并在延迟前后通过gettimeofday()函数打印时间。

输出:经过打印测试,程序输出却是100ms的延迟。

原因:clock()函数在C/C++中是用于测量程序执行的时间,它返回的是从程序启动到调用clock()函数时CPU时间(以时钟周期为单位),该时间并不特指哪个线程,而是整个进程所消耗的CPU时间。因此在子线程中虽然设置了200ms的时钟周期延迟,但是却是包含主线程和子线程一共消耗的CPU时钟,也就是主线程消耗了100ms的时钟周期,子线程消耗了100ms的时钟周期,一共200ms的时钟周期,但是实际上只过去了100ms的时间。

#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <pthread.h>


void* thread1(void * a)
{
    unsigned long tick_start, tick_current, tick_start_i;
    tick_start = clock();
    tick_current = clock();

    struct timeval tv;

    gettimeofday(&tv, NULL);
    long long milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
    printf("current time in milliseconds: %lld, %lld\n", milliseconds, (long long int)CLOCKS_PER_SEC);

    while(tick_current - tick_start < (200 * CLOCKS_PER_SEC / 1000))
    {
        tick_current = clock();
    }


    gettimeofday(&tv, NULL);
    long long milliseconds2 = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
    printf("current time in milliseconds: %lld\n", milliseconds2);
    printf("eclipsed time is : %lld\n", milliseconds2 - milliseconds);
}


int main()
{
    pthread_t id1;
    int ret = pthread_create(&id1,NULL, thread1, (void *)NULL);
    if(ret!=0){
        printf ("Create pthread error!\n");
    }

    int i = 0;
    while(1)
    {
        i += 1;
    }

    return 0;
}

打印输出如下: 

注:如果将main中while部分代码替换为pthread_join和sleep方式,则子线程中延迟仍为200ms,这是因为此时主线程并未消耗CPU资源。

源代码: 

int i = 0;
while(1)
{
    i += 1;
}

替换1: 

pthread_join(id1,NULL);

替换2:

sleep(10);

相关推荐

  1. 【C语言】clock_gettime函数使用

    2024-04-14 07:20:02       70 阅读
  2. vue中@click.prevent函数使用

    2024-04-14 07:20:02       31 阅读
  3. Unity之延迟函数

    2024-04-14 07:20:02       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-14 07:20:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-14 07:20:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-14 07:20:02       87 阅读
  4. Python语言-面向对象

    2024-04-14 07:20:02       96 阅读

热门阅读

  1. 算力服务器包含哪些业务?

    2024-04-14 07:20:02       57 阅读
  2. 网站如何一定程度上防止ddos和压力测试

    2024-04-14 07:20:02       40 阅读
  3. Linux nfs挂载失败处理

    2024-04-14 07:20:02       56 阅读
  4. 【C语言】命令行

    2024-04-14 07:20:02       39 阅读
  5. janus搭建

    2024-04-14 07:20:02       44 阅读
  6. libtorch中API介绍

    2024-04-14 07:20:02       151 阅读
  7. FFmpeg: 自实现ijkplayer播放器--07解复用线程设计

    2024-04-14 07:20:02       40 阅读
  8. 解决在 Ubuntu18.04 上安装 ffmpeg 失败的方法

    2024-04-14 07:20:02       168 阅读
  9. FFmpeg: 自实现ijkplayer播放器--08视频解码线程设计

    2024-04-14 07:20:02       38 阅读