我面對的C++並行線程一個奇怪的錯誤,我嘗試運行這段代碼:一些奇怪的事情,在C++ POSIX線程
typedef struct
{
struct sockaddr_in clienAddr;
int clientLength;
string message;
}param;
pthread_t clientThread;
param sentParam ;
sentParam.clienAddr = clientAddress;
sentParam.clientLength= client_info;
sentParam.message=buffString;
cout <<"sentParam: "<<sentParam.message<<endl;
// it prints well.
int i = pthread_create(&clientThread, NULL, handleClientRequestRead,&sentParam);
cout <<"i: "<<i<<endl;
the function which be called
void* handleClientRequestRead(void* params)
{
// cout<<"params: "<< ;
string msg = ((param *)(params))->message;
}
當我嘗試打印味精它是空的。任何幫助將不勝感激
您必須確保'sentParam'只要生活爲主線(這通常意味着然後結束程序的)。 –