Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15745
  • 博文数量: 6
  • 博客积分: 130
  • 博客等级: 入伍新兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-08 00:17
文章分类

全部博文(6)

文章存档

2008年(6)

我的朋友
最近访客

分类: LINUX

2008-08-08 00:29:34

写了sleep_in_thread.c测试sleep和select在线程中的效用:

sleep和select都会阻塞线程一段时间,sleep是以秒为单位的, select是以微秒为单位的(实际应该支持不到微秒,可以支持到毫秒)

在线程中调用sleep或者select的作用:

  • 可以平衡各线程之间运行的时间,在没有阻塞时, 单一线程会占用CPU时间很长时间才放开,有阻塞后,各线程会比较均衡的被分配到CPU时间。
  • 可以降低对CPU的占用。
code:

/** sleep_in_thread.c */
/** debian下编译通过 */

#include
#include
#include
#include
#include
#include

#define SLEEP 0
#define SELECT 1


void* thr_fn1(void* arg)
{
    int i, flag;
    struct timeval timeout;
    printf("thread 1 running\n");
    flag = *((int*)arg);

    for(i = 0; i < 1000000; i++) {
        printf( "thread 1: %u\n", i );
        if (flag == SLEEP) {
            printf("thread 1 sleep 1 second\n");
            sleep(1);
        }
        if (flag == SELECT){
            timeout.tv_sec = 0;
            timeout.tv_usec = 1000;
            //printf("thread 1 sleep 1000 usec\n");
            select(0, NULL, NULL, NULL, &timeout);
        }
    }

    return (void *)0;
}

void* thr_fn2(void* arg)
{
    int i, flag;
    struct timeval timeout;
    printf("thread 2 running\n");
    flag = *((int*)arg);

    for(i = 0; i < 1000000; i++) {
        printf( "thread 2: %u\n", i );
        if (flag == SLEEP) {
            //printf("thread 2 sleep 1 second\n");
            sleep(1);
        }
        if (flag == SELECT){
            timeout.tv_sec = 0;
            timeout.tv_usec = 1000;
            //printf("thread 2 sleep 1000 usec\n");
            select(0, NULL, NULL, NULL, &timeout);
        }
    }
   
    return (void *)0;
}

void* thr_fn3(void* arg)
{
    int i, flag;
    struct timeval timeout;
    printf("thread 3 running\n");
    flag = *((int*)arg);

    for(i = 0; i < 1000000; i++) {
        printf( "thread 3: %u\n", i );
        if (flag == SLEEP) {
            //printf("thread 3 sleep 1 second\n");
            sleep(1);
        }
        if (flag == SELECT){
            timeout.tv_sec = 0;
            timeout.tv_usec = 100;
            //printf("thread 3 sleep 1000 usec\n");
            select(0, NULL, NULL, NULL, &timeout);
        }
    }
   
    return (void *)0;
}

int main(int argc, char** argv)
{
    pthread_t tid1, tid2, tid3;
    int err, sleep_type;

    if (argc != 2) {
        printf("useage sleep_in_thread sleep_type\n");
        return 1;
    }

    sleep_type = atoi(argv[1]);

    err = pthread_create(&tid1, NULL, thr_fn1, &sleep_type);
    if (err != 0) {
        printf("create thread 1 error: %s\n", strerror(err));
        return 1;
    }

    err = pthread_create(&tid2, NULL, thr_fn2, &sleep_type);
    if (err != 0) {
        printf("create thread 2 error: %s\n", strerror(err));
        return 1;
    }

    err = pthread_create(&tid3, NULL, thr_fn3, &sleep_type);
    if (err != 0) {
        printf("create thread 3 error: %s\n", strerror(err));
        return 1;
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);

    return 0;
}


阅读(1265) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:线程:sleep和select测试

给主人留下些什么吧!~~