473,383 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

boost smart_ptr and WIN32 PostMessage

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!

Jun 20 '06 #1
3 6038
figsys wrote:
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.

--
Phlip
Jun 20 '06 #2

"Phlip" wrote:
figsys wrote:
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.

That's as good a hundred word appaisal on threads as I've ever laid eyes on.

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.

That certainly sounds like the right answer for the topical part. Boost has
progressed farther than I had anticpated. fu

Jun 20 '06 #3

fi****@yahoo.com wrote:
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
returning a smart_Ptr<MyObject> to create a new object.

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.


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!


Jun 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
5
by: Lars Schouw | last post by:
I have downloaded the newest boos release. I am havng problems building boost using the intel C++ 8.0 compiler. It looks as if bjam can't fine the icl.exe compiler executable itself. This file is...
4
by: Steven T. Hatton | last post by:
I'm trying to create an baseclass that will serve as a parent for reference counted objects handled by boost::intrusive_ptr<>. The documentation didn't provide much in the way of describing what...
3
by: ~dr-sci-fi | last post by:
looking for a comprehensive version of most common API functions used in ..Net, sample below: =============================================================== using System; using...
2
by: RafaƂ Maj Raf256 | last post by:
How can I manually (other then http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html) create a shared, and a weak pointer to "this"? Of course I dont want the shared / weak pointer to...
6
by: toton | last post by:
Hi, One more question with smart_ptr ( I am using the one from Axter). I want to assign the object to the pointer later, not at the construction time. like, smart_ptr<BasepBase; ///what will be...
85
by: g | last post by:
Hello, is there any library for C as Boost is for C++? thanks in advance,
2
by: Danny | last post by:
Hi I want to be able from my csharp button to start an application ( which I can do) and then somehow send a message to the win32 application that says "press the button" The win32...
9
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.