多线程导致更多CPU使用

多线程导致更多CPU使用,c,linux,C,Linux,对于下面的代码,我的cpu使用率是97%。我在Ubuntu上运行c代码 #include <stdio.h> #include<pthread.h> void *s_thread(void *x) { printf("I am in first thread\n"); } void *f_thread(void *x) { printf("I am in second thread\n"); } int main() {

对于下面的代码,我的cpu使用率是97%。我在Ubuntu上运行c代码

#include <stdio.h>
#include<pthread.h>
void *s_thread(void *x)
{
        printf("I am in first thread\n");
} 
void *f_thread(void *x)    
{
        printf("I am in second thread\n");
}

int main()
{
        printf("I am in main\n");
        pthread_t fth;
        int ret;
        ret = pthread_create( &fth,NULL,f_thread,NULL);
        ret = pthread_create( &sth,NULL,s_thread,NULL); 
        while(1);
        return 0;
}        
int main()
{
    printf("I am in main\n");

    pthread_t fth,sth;
    int ret;

    ret = pthread_create( &fth,NULL,f_thread,NULL);
    ret = pthread_create( &sth,NULL,s_thread,NULL); 

    pthread_join(&fth,NULL);
    pthread_join(&sth,NULL);

    return 0;
}
#包括
#包括
void*s_螺纹(void*x)
{
printf(“我在第一个线程中\n”);
} 
空心*f_螺纹(空心*x)
{
printf(“我在第二个线程\n”);
}
int main()
{
printf(“我在主\n”);
pthread_t fth;
int ret;
ret=pthread_create(&fth,NULL,f_thread,NULL);
ret=pthread_create(&sth,NULL,s_thread,NULL);
而(1),;
返回0;
}        
这个简单的代码比只运行一个线程给了我更多的cpu使用率

int main()
{
    printf("I am in main\n");

    pthread_t fth,sth;
    int ret;

    ret = pthread_create( &fth,NULL,f_thread,NULL);
    ret = pthread_create( &sth,NULL,s_thread,NULL); 

    pthread_join(&fth,NULL);
    pthread_join(&sth,NULL);

    return 0;
}

while(1)
使用更多的CPU周期,因此使用
pthread\u join
并加入进程,以便
main
线程等待子线程完成。

在linux中有3个线程:

int main()
{
    printf("I am in main\n");

    pthread_t fth,sth;
    int ret;

    ret = pthread_create( &fth,NULL,f_thread,NULL);
    ret = pthread_create( &sth,NULL,s_thread,NULL); 

    pthread_join(&fth,NULL);
    pthread_join(&sth,NULL);

    return 0;
}
  • 1-执行main()的主线程
  • 2-f_螺纹
  • 3-s_螺纹
主线程等待while(1)循环,这会导致大量资源使用。 您不应该使用while(1),而应该使用
pthread\u join
(http://www.manpagez.com/man/3/pthread_join/) .

int main()
{
    printf("I am in main\n");

    pthread_t fth,sth;
    int ret;

    ret = pthread_create( &fth,NULL,f_thread,NULL);
    ret = pthread_create( &sth,NULL,s_thread,NULL); 

    pthread_join(&fth,NULL);
    pthread_join(&sth,NULL);

    return 0;
}

使用
pthread\u join
,主线程将休眠,直到其他两个线程完成。这样就不会有不必要的资源使用

将sleep命令放入循环中。例如,以下“sleep”命令休眠一秒钟:

int main()
{
    printf("I am in main\n");

    pthread_t fth,sth;
    int ret;

    ret = pthread_create( &fth,NULL,f_thread,NULL);
    ret = pthread_create( &sth,NULL,s_thread,NULL); 

    pthread_join(&fth,NULL);
    pthread_join(&sth,NULL);

    return 0;
}
while(1) {
  sleep(1);
}
还有其他一些睡眠时间较短的命令,例如unistd.h提供的“usleep”,它的睡眠时间为给定的微秒数:

int main()
{
    printf("I am in main\n");

    pthread_t fth,sth;
    int ret;

    ret = pthread_create( &fth,NULL,f_thread,NULL);
    ret = pthread_create( &sth,NULL,s_thread,NULL); 

    pthread_join(&fth,NULL);
    pthread_join(&sth,NULL);

    return 0;
}
while(1) {
  usleep(1000);
}

你能不能把你试过的单线程版本也发出来。我很困惑。您在(1)时编写了一个无限循环
while然后。。不知道为什么它只占用97%的CPU?当你到达无限循环时,你的线程已经死了,它们会在哪里。。帮忙?我不明白你想实现什么。这是我正在弄清楚的一个简单的例子,但假设我的线程也包含一个infinte循环…如果
while(1)
打算阻止主线程,你应该用一个更轻的东西来替换它,而不是占用cpu那么多。这将使您的测量更加准确。您的问题是什么,您希望实现什么?你的代码是如此的不合理以至于很难猜测,即使对于编译器也是如此:)谢谢你,我希望我的进程能继续运行在这种情况下我必须使用infinte而loop谢谢你,我希望我的进程能继续运行在这种情况下我必须使用infinte而loop在主线程中完成你的工作,在调用pthread_join之前。只要工作正在进行,主线程就不会到达调用pthread_join。不知何故,如果主线程中的工作已经完成,它应该等待pthread\u join(如果f\u thread和s\u thread这两个线程尚未完成)。这还取决于您正在做什么工作。如果您使用的是阻塞调用,而(1)是可以的。例如,while(1){read(..)}。在这里,主线程不会占用资源,因为当它在读取时被阻塞时,它将被调度出去。当有数据要读取时,它会被安排回读。简单地说,(1)会占用你的CPU。