「pthread」ライブラリを使用してC++でマルチスレッドプログラムを作成していますが、Ubuntu仮想マシンで実行すると、マルチコアプロセッサ(i7-2630QM)を使用しているにもかかわらず、スレッドが並行して実行されていないようです。 。コードが長すぎるので、この単純なコードの問題を説明します。
#include <iostream>
#include <pthread.h>
using namespace std;
void* myfunction(void* arg); //function that the thread will execute
int main()
{
pthread_t thread;
pthread_create(&thread, NULL, myfunction, NULL); //thread created
for (int i=0; i<10; i++) //show "1" 10 times
cout << "1";
pthread_join(thread, NULL); //wait for the thread to finish executing
return 0;
}
void* myfunction(void* arg)
{
for (int j=0; j<10; j++) //show "2" 10 times
cout << "2";
return NULL;
}
このコードをホストOS(Windows 7 with VC ++ 2010)で12212121211121212...
実行すると、マルチスレッドアプリが実行するはずの結果が得られますが、ゲストOS(Ubuntu on Vmware with Vmware)で同じコードを実行するとCode :: Blocks)私はいつも得ます11111111112222222222
!!! AFAIKスレッドは、順次ではなく、main()関数と並行して実行する必要があります。VMのコア番号は4に設定されていますが、プログラムは1つのコアしか使用していないようです。何が問題なのかわかりませんか?それはコードですか...?私はここで何かが欠けていますか?私は助けに感謝します、事前に感謝し、私の英語を許してください:/