我有一個class A
函數void runThread()
來調用新線程。這是我A.cpp
與struct SendInfo
和功能void thread(...)
不包含在頭文件A.h
:在C++頭文件的cpp文件中啓動線程<process.h>
//A.cpp
struct SendInfo{
int a;
std::string mess;
SendInfo(int _a, std::string _mess){
a = _a;
mess = _mess;
}
};
void thread(SendInfo* args){
std::cout << args->mess << std::endl; // Result here is nothing :-?
}
void A::runThread(){
SendInfo info(10,"dump_string");
std::cout << info.mess << std::endl; // Result here is "dump_string"
_beginthread((void(*)(void*))thread, 0, &info);
}
當主函數,我調用A object
runThread()
的info.mess
結果是好的,但args->mess
沒有字符串。那麼我的問題是什麼?以及如何解決它?
SendInfo分配在堆棧上,但是你發送一個指向_beginthread的指針,緊接着堆棧被展開一級。錯字或致命錯誤? – Max
這是一個錯誤:D!謝謝! –