我正嘗試使用pthread從命令行讀取輸入。 pthread會調用一個閱讀功能。遇到一些麻煩,我已經閱讀了POSIX文檔。感謝幫助!使用pthreads讀取輸入
int main(int argc , char *argv[])
{
pthread_t client_thread; // client thread
int rc;
string msg;
cout<<"Please enter a message to send to the server: "<<endl;
pthread_create(&client_thread, NULL, readerT, &msg);
cout<<"Msg is: "<<msg<<endl;
return 0;
}
void * readerT(string * temp)
{
cout<<"GOT IN HERE:\n"<<endl;
getline(cin,*temp);
}
當前錯誤消息:
Client.cpp: In function ‘int main(int, char**)’:
Client.cpp:33: error: invalid conversion from ‘void* (*)(std::string*)’ to ‘void* (*)(void*)’
Client.cpp:33: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’
加入創建的線程,以便主線程不會結束。 – Whoami
我加了pthread_join(client_thread,NULL);在cout <<「之後msg是...但是我得到了一堆關於無效轉換的錯誤:( – Masterminder
1)檢查線程創建的返回值,確保你編輯了具有適當錯誤信息的問題,以便工程師可以 – Whoami