可是卻連最簡單的多執行緒Sample都會踏到記憶體去當掉,這時候可以在pthread_create之前先呼叫pthread_mutex_init。另外,多執行緒在Compiler時請務必加入-lpthread的指令。
(此問題在arm-linux-gcc時不會遇到,在arm-linux-g++時才會出現,一樣的程式碼。)
#include <pthread.h&rt;
#include <iostream&rt;
using namespace std;
pthread_mutex_t mutex;
void *MyThread(void *dammy);
int main(){
   pthread_t thread;
   pthread_mutex_init(&mutex,NULL);
   pthread_create(&thread,NULL,MyThread,NULL);
   while(1){
       //printf("main\n");
       cout << "main" << endl;
       sleep(3);
   }
}
void *MyThread(void *dammy){
   while(1){
      //printf("Threah");
      cout << "Thread" << endl;
      sleep(1);     
   } 
}