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.
http://www.linuxquestions.org/questi...fusion-765421/
Thanks to rupertwh.
Code:
#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++) {
putchar('M');
fflush(stdout);
}
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) {
putchar('t');
fflush(stdout);
}
g_stop = 5;
printf("\nmythread thinks: g_stop=%d\n", g_stop);
return NULL;
}