我正尝试使用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