Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-30-2009, 06:01 PM   #1
Registered: Feb 2007
Posts: 386

Rep: Reputation: 30
CPU usage of each threads

I understand that each thread will use a certain CPU time controlled by
scheduler. How do I find out how much each thread use the CPU time?
I was thinking to tick the time at the beginning of each thread, and again at the end, but it is just telling me the time the thread start and the time the thread end, but it does not tell me the CPU time.

Here is the code I modified from my previous post.

Thanks to rupertwh.

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

int g_stop = 0;

void* mythread(void *parg);

int main(void)
        pthread_t tid[2];
        int i;
        void *tret;

        if (pthread_create(&tid[0], NULL, mythread, NULL)) {
                perror("Couldn't create mythread");
                return 1;

        if (pthread_create(&tid[1], NULL, mythread, NULL)) {
                perror("Couldn't create mythread");
                return 1;

        for (i = 0; i < 100; i++) {
        g_stop = 1;

        pthread_join(tid[0], &tret);
        pthread_join(tid[1], &tret);
        printf("\nmain thinks: g_stop=%d\n", g_stop);

        return 0;

void* mythread(void *parg)
        while (!g_stop) {
        g_stop = 5;

        printf("\nmythread thinks: g_stop=%d\n", g_stop);

        return NULL;

Last edited by ufmale; 10-30-2009 at 06:03 PM.
Old 10-30-2009, 06:06 PM   #2
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i dont think you can, you can only get processor info as far down as the process.
BTW whats with your username?
and you can get the time the thread took by suptracting endd from start

Last edited by smeezekitty; 10-30-2009 at 06:08 PM.
Old 10-30-2009, 06:50 PM   #3
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,716

Rep: Reputation: 422Reputation: 422Reputation: 422Reputation: 422Reputation: 422
Have a look to the getrusage RUSAGE_THREAD kernel patch. It might be implemented in recent kernels.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
CPU usage 100% due to loops in multiple threads Manjunath1847 Linux - General 4 08-10-2009 03:45 AM
processes/CPU Usage/Mem Usage desktop wallpaper ceantuco Linux - Newbie 2 04-13-2009 02:14 PM
getting realtime info on memory usage-cpu and harddrive usage steering Linux - Newbie 5 03-03-2005 09:43 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 05:45 AM
Controlling CPU usage & memory usage Saravana babu Linux - Software 0 02-18-2004 06:55 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:36 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration