|
I think it is implementation specific - but I would normally put here some locking mechanism - it's shared resource between two threads - it means, say on two processor machine you can have half pushed object in a dequeue and in the very same moment second thread running in another processor receives that half baked object and consider that it's complete - where it can lead noone knows...
How to do it without waiting in A thread - that depends on your requirements - e.g. I can imagine a mechanism where thread A makes some batches (queue of commands say) and when there's some free time or the queue reaches some maximum size it's marked as complete (event signalled e.g.) and pushed to the thread B for processing, while A thread creates new (or uses previously created) queue for pushing...
just an idea, maybe there can be dozens of other solutions, it really depend on the requirements you have for the processing.
hope this helps
|
|
|
|
|
One solution is a critical section for single process and a mutex for multiple processes. Check the size of the container.
Kuphryn
|
|
|
|
|
I don't think that you can do this without using a critical section. You have to synchronize access in some way. I'd be interested to hear more on your plan for this delimiter object-- how do you think that it will solve the problem of A and B both accessing the same memory at the same time? I'm really curious.
One tactic that you can take is to buffer things a little before even putting them in the queue. A can stack up a few things destined for the queue, and just copy them into the queue on a scheduled basis or when the buffer gets full. In my experience, it will still have to lock, but this can greatly decrease the per-element synchronization overhead. Of course, this can mean some tradeoffs like extra memory overhead, and another drawback is that the elements aren't immediately made available to thread B.
Thank you.
Jeff Varszegi
|
|
|
|
|
Well my thought was because a deque is basically made up out memory ellements with two pointers one to the next element and one to the pervious. So if you make sure one or two elements always stay in the deqeu the two threads wil never access the same memory space (because each thread only operates at one end of the queue). At least that is my theory please correct me if I'm wrong.
But apperantly it's safest to find some locking meganism, the problem is just that thread A is reading from a serial port, so I never really know whether I have time to wait for a lock on the deque, or I'll mis incomming bytes. Secondly it's rather important to get all bytes in de deque as soon as possible (I need to register when they got in), so buffering them is not really an option.
Thanks for all reactions,
Erik
|
|
|
|
|
There are many approaches. Depending on what you are doing, have a searh on google for Producer/Consumer, Bounded Buffer and Multiple Reader/Writer as thread syncronisation methods. If you have just two threaads, then all you should need is a Mutex or a Critical Section (which is a faster method for all in process - Mutex can do cross process).
If you want to limit the size of the buffer i.e. the writer can outrun the reader, then have a look at the bounded buffer.
Personally for lots of different patterns with good descriptions and full working implementations is a book called "Multithreaded Programming with Win32" written by T.Q. Pham and P.K. Garg.
Its even demonstrates a multithreaded quicksort.
"Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table.
Shameless Plug - Distributed Database Transactions in .NET using COM+
|
|
|
|
|
Hello,
I am using eVC 3.0 and trying to build a database application. I am using an #import of arm\adoce31.dll. It is giving me a linker error [CODE]unresolved external symbol "class _variant_t vtMissing"[/CODE].
I have read in one section of MSDN that vtMissing is suppiled by #import. Do I have to include some more libraries ?
Many thanks in advance.
Regards,
Amit
|
|
|
|
|
If this is the only value you are missing
just define your own:
vtMissing.vt = VT_ERROR;
vtMissing.scode = DISP_E_PARAMNOTFOUND;
|
|
|
|
|
I am brand new to STL so please bear with me. I am creating an item Label based on an abstract class button. In code in the first segment works for me as expected. But In the second part I was not expected to have to cast (delete (Label*)pB; ) as noted below. When I don't cast the destuctor does not get called.
So, finally my question. As I am going thru the vector should I have to know each type of thing being deleted so I can cast it accordingly or am I approaching this all wrong?
In my application I was only using ( delete pB; ) and this is causing a memory leak. The only solution I see during clean up I query each object being destroyed for its type then call the appropriate code.
for example
if( objecttype == Label )
delete (Label*)pButton;
else if ( objecttype == Image )
delete (Image*)pButton;
This seems wrong to me. Please enlighten me.
// First part of code example
std::vector<Button*> vButtons;
pButton = new Label( ... );
vButtons.push_back( pButton );
// Second part of code example
int count = vButtons.size();
if( count > 0 )
{
std::vector<Button*>::iterator it;
Button *pB;
for (it = vButtons.begin(); it != vButtons.end(); ++it)
{
pB = *it;
if(pB != NULL)
{
delete (Label*)pB;
pB = NULL;
}
}
vButtons.clear();
}
|
|
|
|
|
Did you declare your destructor (in the button class) as virtual?
John
|
|
|
|
|
Thanks, that worked great. I guess you noticed that I am new to more than just STL.
|
|
|
|
|
smesser wrote:
I guess you noticed that I am new to more than just STL.
No problem, thats what the forums are for. I remember having the same problem a few years back...
John
|
|
|
|
|
John M. Drescher wrote:
No problem, thats what the forums are for. I remember having the same problem a few years back...
Well, thanks again for your wisdom.
Steve
|
|
|
|
|
hi,
How can i convert the window handle in to object pointer
Regards
Abhay
eeeshwar
|
|
|
|
|
Hi there,
suppose u have HANDLE , HWND of a Window class CWnd
Create a Object of that class
HWND m_hwnd
CWnd wndObj;
or CWnd *wndObj=new CWnd()
wndObj.Attach(m_hwnd)
or wndObj->Attach(m_hwnd)
in this way object is created.
is that u wnat to know
Abhishek Srivastava
Software Engg (VC++)
India ,Noida
Mobile no 9891492921
|
|
|
|
|
hi,
i tried the same i wrote win32 dll with following code:
long __declspec(dllexport)__stdcall GetControlReference(long handle)
{
long reference;
CWnd* controlwindow=new CWnd();
controlwindow->Attach((HWND)handle);
reference=(long)controlwindow;
return reference;
}
//here is my vb client
function GetObject (hwnd1) as object
dim obj as object
dim ret as long
ret = GetControlReference(hwnd1) //calling win32 dll
If Not IsBadCodePtr(ret) Then
CopyMemory obj, ret&, 4
If Not obj Is Nothing Then
Set GetObject = obj //crashing here
CopyMemory obj, 0&, 4
End If
End If
|
|
|
|
|
CWnd has a static member function FromHandle which will give you a (possibly) temporary object. If an object is already attached to a window handle, you'll get a base class pointer to that object, and you can safely upcast it. If not, then you'll get a pointer to a temporary object, which may well only exist while the current message is being processed.
Steve S
|
|
|
|
|
Abhay-
I'm trying to do much the same thing myself, if I understand your message correctly. I need to access the methods/properties of COM objects that already exist within another application.
The approach I am taking (though I've not yet succeeded) is to create a CAxWindow instance using the HWND of the target window, then call the method CAxWindow::QueryControl(). (An alternative is use of the AtlAxGetControl() function.) In theory, this would provide a pointer to the object's IUnknown interface, or any other interface if the appropriate interface ID were passed in. From that, it should be possible to invoke methods of the interface that is returned.
As I said, I have not yet succeeded in this task, which is why I was scanning this message board. I'll keep trying the above approach and searching other resources, but if anyone has any insight on this topic, I would appreciate it- and I'm sure Abhay would as well!
Bob
|
|
|
|
|
Hi
I´m trying to create an instance of:
CComQIPtr<IInputObjectSite>pInputObjectSite;
but i´m getting error 2787 (No guid has been associated with this object).
The guid is declared in shlguid.h, and this file is included in the project. Why it doesn´t recognize it?
Thanks
Gabriel
Old C programmers never die. They just cast into void
|
|
|
|
|
try IID_IInputObjectSite as the template parameter
|
|
|
|
|
Thanks for your suggestion. It worked. I formerly posted that it didn´t work but i was really tired and i was forgetting the & before the GUID.
Gabriel
Old C programmers never die. They just cast into void
|
|
|
|
|
Is there a way I could get to see the value of an iterator?
IOW, if I am doing something at the 525th place in a vector, is there a way I could get the iterator to show me its position?
Thanks for any insight.
William
Fortes in fide et opere!
|
|
|
|
|
You can subtract iterators just like you can subtract pointers:
vector<int> v(600);
vector<int>::iterator it = v.begin() + 525;
cout << it - v.begin() << endl; Prints 525.
--Mike--
Ericahist [updated Oct 26] | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber
"Linux is good. It can do no wrong. It is open source so must be right. It has penguins. I want to eat your brain."
-- Paul Watson, Linux Zombie
|
|
|
|
|
Clever!!
But you still didn't answer the question, because (let's suppose) 'v.begin' has the internal value of 306759. By assigning its internal value plus 525 (as follows)
vector<int>::iterator it = v.begin() + 525; you are essentially adding 306795 to 525 giving 'it' the value of (525 + 306795) = 307320. Then you turned right around and did (307320 - 306795) which very logically gives you the difference of 525 (the same number you just added to 'v.begin()', the subtrahend).
IOW, you didn't give me the value of either the minuend nor the subtrahend. You simply gave me the numerical difference between them, which is the same number you added to one of them in the first place.
What I want to know is whether I am able to see the value of 'v.begin()' or 'it' after (let's say) 525 has been added to it.
William
Fortes in fide et opere!
|
|
|
|
|
No - an iterator may not have a simple value - i.e. an iterator is NOT neccesarily a pointer. For example, in VC++ 7.0, std::string::iterator is a struct , not a char* .
Now, it sounds to me like you're looking for a pointer to the element the iterator references, which you can get hold of using &*it , where it is an iterator.
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
Michael Dunn has the right idea. The key concept of iterators is that they are an abstraction for accessing containers and don't necessarily know their own position. For example, take the iterator for the std::list template. This iterator implements the bidirectional iterator model meaning that it merely needs to know the previous node and the next node in a container. For std::list , that means that it just needs a reference to a single node in the doubly-linked list -- it has no concept of where that node is in the list.
Here's an example as to why this is important: Let's say that you have a list and you start iterating through it. Somewhere in the middle, you insert a node somewhere in the list. This does not invalidate the iterator, so you can still happily keep moving around the list. If the iterators kept track of the position, the list would have to keep track of every single iterator and notify them that their position has been changed. Or they could just get invalidated, which is worse.
Even a std::vector iterator (which implements the random access iterator model) probably doesn't know what position in the vector it points to -- most implementations just store a pointer to the item, not an index, and some older implementations typedef the iterator type to a pointer directly! However, it means that it can find out the relative distance to another iterator, so subtracting iterators produces a useful result.
As far as I can tell, only a random access iterator needs to implement subtraction. Other iterator types probably won't compile when you try to subtract them.
- Mike
|
|
|
|