Yes, but you shouldn't. I would say you're flirting with disaster. Sounds like you want to pass a pointer to a buffer in one task's stack to another task's stack. You better make sure you implement proper locking protection if you want to get valid reads and writes all the time.
Your first problem is with this line:
Code: Select all
BufferedTcpObject tcp = *(BufferedTcpObject*)pdata;
This is calling the assignment operator for a BufferedTcpObject. (I suspect it first calls a BufferedTcpObject constructor). That is it's the same as this
Code: Select all
BufferedTcpObject tcp; //constructor call - construct object on the stack
tcp = *(BufferedTcpObject*)pdata; //assignment call
If the assignment operator for BufferedTcpObject was written correctly it will NOT merely do a memberwise assignment. It will most likely do a full copy of the pointed to data structures (but it could do a reference counted copy - I just find it unlikely). (If you want to know why it should do this see Scott Meyer's Effective C++ Item 11). In effect you've ended up with a copy of the data contained in the main task at the time the spawned task gets to that line.
You could try
Code: Select all
BufferedTcpObject* tcp = (BufferedTcpObject*)pdata; //just copy the pointer
and then use the -> syntax
Safer (but still suspect) would be to do this:
Code: Select all
const BufferedTcpObject* tcp = (BufferedTcpObject*)pdata; //make what is pointed to const --personally I would also make the ptr const
Now at least you're promising to just read (not write) the other task's stack data. Even that's not a good idea without some sort of locking.
I would say the canonical way to exchange data between tasks is to use OS Calls (like mailbox and semaphores and often a combination of the two). Other may have differing opinions.