boost smart_ptr and WIN32 PostMessage

F

figsys

I am looking for a simple wrapper or mechanism to send a boost
smart_ptr using WIN32 PostMessage (or PosthreadMessage). The smart_ptr
should be incremented when sent and decremented when received (or
derefenced).

WIN32 is not "C++ enabled" and thus does not allow to cast a smart_ptr
into an LPARAM or WPARAM.

Something like

DWORD nID;
HANDLE m_hBxTxThread = CreateThread (NULL, 0, MyThread, NULL, 0, &nID);

boost::smart_ptr<MyObject> pSmart( new MyObject);
pSmart->Set(100);

PostThreadMessage( nID, 0, 0, (LPARAM) pSmart );

does not work - can convert smart_ptr to LPARAM. This works but defeats
the point since reference is not incremented:

PostThreadMessage( nID, 0, 0, (LPARAM) &pSmart )

Thanks!
 
P

Phlip

figsys said:
HANDLE m_hBxTxThread = CreateThread (NULL, 0, MyThread, NULL, 0, &nID);

Firstly, you should find any means necessary to avoid threads. Your
application should be event driven, so its working thread can time-slice
events.

Almost none of the tutorials on threads adequately describe why to
use them. They typically say, "If you have a function that runs too long,
you can just use threads!" A function could run too long for many reasons,
including processing a long list, or blocking on a semaphore. The former
should be broken up into a time-sliced routine, so the GUI thread can
strobe it with a timer. The latter should be multiplexed, so GUI events
dispatch synchronously with event threads.

All those techniques lead to better designs. These, in turn, are trivially
easy to multi-thread if you then determine you have a real need.
boost::smart_ptr<MyObject> pSmart( new MyObject); pSmart->Set(100);

PostThreadMessage( nID, 0, 0, (LPARAM) &pSmart );

Use SendMessage(...pSmart.get()), and fully use the target object before
the event returns.

Or pass a semaphore and block until the receiver clears it. The receiver
will reconstitute the smart pointer and then copy it into another smart
pointer.

And that technique depends on thread-ready smart pointers. You might want
to back off and invent a new smart pointer (they are very easy) that is
Win32-thread aware. A boost mailing list might be able to instruct you to
re-use their smart pointers with policy objects that enable Win32.

From here, your next question should go to a newsgroup with win32 and
programming in its name. This newsgroup is only qualified to discuss the
raw C++ language itself. Boost smart pointers barely qualify, and threads
do not.
 
Y

Your Uncle

Phlip said:
Firstly, you should find any means necessary to avoid threads. Your
application should be event driven, so its working thread can time-slice
events.

Almost none of the tutorials on threads adequately describe why to
use them. They typically say, "If you have a function that runs too long,
you can just use threads!" A function could run too long for many reasons,
including processing a long list, or blocking on a semaphore. The former
should be broken up into a time-sliced routine, so the GUI thread can
strobe it with a timer. The latter should be multiplexed, so GUI events
dispatch synchronously with event threads.

All those techniques lead to better designs. These, in turn, are trivially
easy to multi-thread if you then determine you have a real need.
That's as good a hundred word appaisal on threads as I've ever laid eyes on.

Use SendMessage(...pSmart.get()), and fully use the target object before
the event returns.

Or pass a semaphore and block until the receiver clears it. The receiver
will reconstitute the smart pointer and then copy it into another smart
pointer.

And that technique depends on thread-ready smart pointers. You might want
to back off and invent a new smart pointer (they are very easy) that is
Win32-thread aware. A boost mailing list might be able to instruct you to
re-use their smart pointers with policy objects that enable Win32.

From here, your next question should go to a newsgroup with win32 and
programming in its name. This newsgroup is only qualified to discuss the
raw C++ language itself. Boost smart pointers barely qualify, and threads
do not.
That certainly sounds like the right answer for the topical part. Boost has
progressed farther than I had anticpated. fu
 
P

Peter

I am looking for a simple wrapper or mechanism to send a boost
smart_ptr using WIN32 PostMessage (or PosthreadMessage). The smart_ptr
should be incremented when sent and decremented when received (or
derefenced).

WIN32 is not "C++ enabled" and thus does not allow to cast a smart_ptr
into an LPARAM or WPARAM.

Something like

DWORD nID;
HANDLE m_hBxTxThread = CreateThread (NULL, 0, MyThread, NULL, 0, &nID);


you may want to use _beginthreadex() instead -- since you're very
likely using the C++ library and CreateThread() does not initialize the
new thread for using the C++ library.

Since _beginthreadex() may fail you may want to wrap it into a C++
wrapper,
throwing an exception in case of it fails.

boost::smart_ptr<MyObject> pSmart( new MyObject);


You MyObject should have a private constructor and a static method
pSmart->Set(100);


I take operator overloading for the -> operator as code obfuscation.
This confuses the hell out of somebody reading your code.
Also pSmart is not a pointer but an object and your naming should
reflect this
(this is just my personal opinion).
What is the problem with writing:

sSmart.get()->Set(100);
PostThreadMessage( nID, 0, 0, (LPARAM) pSmart );


I guess the point is to pass the object pointed to by the smart pointer
to the thread.
I would pass the contained pointer to MyObject to PostThreadMessage()
after incrementing the reference count by one.
The thread can then decrement the reference count by one after
receiving the message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top