Reputation: 3531
Currenly i am working on one example in which i am using multireading in C++.
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define NUM_THREADS 2
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
for (int i =0; i < 20000000000; i++)
{
int x;
x=x+x*x;
}
cout << "Hello World! Thread ID, " << tid << endl;
pthread_exit(NULL);
}
int main ()
{
pthread_t threads[NUM_THREADS];
int rc;
int i;
for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL,
PrintHello, (void *)i);
if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}
But as number of cores on my pc is 2 so i am supposed to get 2 threads while using top.
./multithreadprogram
main() : creating thread, 0
main() : creating thread, 1
But on top i see only one
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
13316 manish 20 0 0 0 0 Z 196.6 0.0 0:16.95 multithreadprog
3629 manish 20 0 528m 25m 12m S 0.7 0.3 0:32.57 gnome-terminal
As far as i know i should be able to get 2 threds running parallely on top. Pls help me out as i am newbie to multithreading.
Upvotes: 0
Views: 81
Reputation: 3858
Notice the 196% CPU in the process table, it means your program is running in more than one core ;)
Upvotes: 1