Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++; 现在我正在研究一个例子,在这个例子中我用C++中的多读。p> #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); }_C++_Multithreading_Pthreads - Fatal编程技术网

C++; 现在我正在研究一个例子,在这个例子中我用C++中的多读。p> #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); }

C++; 现在我正在研究一个例子,在这个例子中我用C++中的多读。p> #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); },c++,multithreading,pthreads,C++,Multithreading,Pthreads,但在上面我只看到一个 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.9

但在上面我只看到一个

  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  

据我所知,我应该能够得到2次在顶部平行运行。请帮助我,因为我是多线程的新手

注意进程表中的196%CPU,这意味着您的程序运行在多个内核中;)

出于兴趣,您使用的是C++11编译器吗?196.6%的CPU,这是两个内核。有什么问题?取决于你想看什么。线程数与CPU使用量不必匹配。您可以让10个线程只是休眠,而不使用CPUUse
top-H-p
查看给定进程id的所有线程。默认情况下,它只显示进程。“4个线程,但每个线程的cpu使用率仅低于55%——如果您有2个内核,那么对于4个线程,每个线程的平均值必须感谢快速回复。
  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