c++ - Memory issues with threads -
i'm working on multi-threaded server application. have struct try pass 2 threads:
struct params{ safequeue<int> *mq; database *db; }; class server{ server(database *db){ dword sthread, ethread; params *p; p = new params; p->db = db; safequeue<int> *msgq = new safequeue<int>; p->mq = msgq; cout << "address of params: " << p << endl; cout << "address of safequeue: " << msgq << endl; cout << "starting server...\n"; createthread(null, 0, smtpreceiver, &p, 0, &sthread); createthread(null, 0, mqueue, &p, 0, ðread); } } dword winapi mailqueue(lpvoid lpparam){ params *p = (params *) lpparam; safequeue<int> *msgq = p->mq; cout << "address of params: " << p << endl; cout << "address of safequeue: " << msgq << endl; cout << "queue thread started...\n"; }
now issue i'm having pointer safequeue in mailqueue thread has address of params struct... see output:
address of params: 0x23878 address of safequeue: 0x212c8 starting server... address of params: 0x28fe60 address of safequeue: 0x23878 queue thread started...
createthread(null, 0, mqueue, &p, 0, ðread); ^^
this should p
you pass params**
mailqueue
thread cast params*
, dereference it, that's undefined behaviour. happens in practice p->mq
address @ *p
(because offsetof(params, mq) == 0
) value of p
in server
constructor, you're seeing in cout
output.
to fix it, should passing params*
new thread, i.e. variable p
not address.
Comments
Post a Comment