|
Iterators are a more generic and flexible concept than indices. They allow you to access the item they reference without having to refer to the parent container. Indices can't do that - they need to be explicitly used with the container they index.
Things like polymorphic algorithms (like in the STL header algorithm) would be tricky to implement with indices...
The Wikipedia page on iterators[^] has quite a good explanation.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I searched over and found that gotoxy() is defined in conio.h!!!!!!
Is it write.
I want to use gotoxy() to print the text at some other location than the origin in the following program. This program is basically a informal animation of the text entered. The code is given below.
#include<stdio.h>
#include<windows.h>
#include<conio.h>
main()
{ static char ch[100], rev[100];
int n=0,c=0,d=0,i=0,h=0,k=0,r=0;
for(n=0;n<100;n++)
{ch[n]=getche();
if(ch[n]=='\r')
break;}
system("cls");
gotoxy(20,18);
while(1)
{for(h=1,k=n;h<100,k>n/2;h++,k--)
{if(h>n)
break;
for(c=0;c<h;c++)
printf("%c",ch[c]);
for(d=n,r=0;d>k-1,r<100;d--,r++)
{
rev[r]=ch[d-1];}
for(i=h-1;i>=0;i--)
printf("%c",rev[i]);
Sleep(1000);
system("cls");}}}
|
|
|
|
|
|
|
Hi ya,
I am a noob to multi threading on C++ (did a bit on Java) and was hoping you could explain to me the following (from http://www.ddj.com/cpp/184401518?pgno=3).
<br />
<br />
#include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <iostream><br />
<br />
boost::mutex io_mutex;<br />
<br />
struct count {<br />
<br />
count(int id) : id(id) { }<br />
<br />
void operator()()<br />
<br />
{<br />
<br />
for (int i = 0; i < 10; ++i)<br />
<br />
{<br />
<br />
boost::mutex::scoped_lock lock(io_mutex);<br />
<br />
std::cout << id << ": " << i << std::endl;<br />
<br />
}<br />
<br />
}<br />
<br />
int id;<br />
<br />
};<br />
<br />
int main(int argc, char* argv[])<br />
<br />
{<br />
<br />
boost::thread thrd1(count(1));<br />
<br />
boost::thread thrd2(count(2));<br />
<br />
thrd1.join();<br />
<br />
thrd2.join();<br />
<br />
return 0;<br />
<br />
}<br />
what is this for? boost::mutex io_mutex; To say you wish to define a boost::mutex lock and call it io_mutex?
In the for loop a scoped lock is used. Does that mean any object used within the scope of the for loop is locked? Does the lock only lock "this"? How do I lock other objects or do I have to use
a scoped lock in the member function?
Thanks for any information.
|
|
|
|
|
minkowski wrote: what is this for? boost::mutex io_mutex; To say you wish to define a boost::mutex lock and call it io_mutex?
Yes
minkowski wrote: In the for loop a scoped lock is used. Does that mean any object used within the scope of the for loop is locked?
No - it means that the thread containing the for loop will own the mutex within the for loop
minkowski wrote: Does the lock only lock "this"? How do I lock other objects or do I have to use a scoped lock in the member function?
The lock isn't explicitly attached to the thing it locks - it's more of a conceptual relationship. What it means in this case is that only one thread at once can execute the line std::cout << id << ": " << i << std::endl; .
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hey thanks for that. So basically, anything that is in the for loop is only executable by 1 thread?
If so, what if I have some member functions for a class, say a get() and a set() and I only want one thread at a time to access either of them (preventing a read occuring the same time as a write). How would I lock them?
|
|
|
|
|
minkowski wrote: So basically, anything that is in the for loop is only executable by 1 thread
Yup.
minkowski wrote: If so, what if I have some member functions for a class, say a get() and a set() and I only want one thread at a time to access either of them
Same sort of thing - using the same threading classes as before:
class MyClass
{
void SetMyObject(SomeClass const& value)
{
boost::mutex::scoped_lock lock(objectMutex);
myObject = value;
}
SomeClass SetMyObject() const
{
boost::mutex::scoped_lock lock(objectMutex);
return myObject;
}
private:
SomeClass myObject;
boost::mutex objectMutex;
};
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
So from your example, say one thread accesses the SetMyObject() , given that there is a lock in the function, it will also lock the GetMyObject() blocking any other thread from calling?
This kind of implies that wherever in the object there is a lock, any function owned by the object will be locked to any other thread trying to access. Is this correct?
Thanks again for your help.
|
|
|
|
|
minkowski wrote: So from your example, say one thread accesses the SetMyObject() , given that there is a lock in the function, it will also lock the GetMyObject() blocking any other thread from calling?
Yes
minkowski wrote: This kind of implies that wherever in the object there is a lock, any function owned by the object will be locked to any other thread trying to access. Is this correct?
Sort of - any function that attempts to acquire (lock) the mutex will be serialized, as only one thread can acquire the mutex at a time. So, that means if you have a group of functions that acquire the lock, only one of that group can be executed concurrently.
There are other sorts of locks that may better suit different situations (semaphores, read-write locks) - part of learning how to design multi-threaded systems is knowing which sort of synchronisation mechanism to use.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hey thanks for that, it tidied up some of my uncertainties.
I was wondering how C++ knows to lock the other member function if one is being accessed by another thread. In the thread constructor itself you pass in either a function pointer or overloaded operator() in your class. So for the case of an object, there must be some reference to the this pointer for the object for it to know to lock the other member functions if a lock is in place from another thread. Was wondering if this is correct?
Thanks for any information.
|
|
|
|
|
minkowski wrote: I was wondering how C++ knows to lock the other member function
It doesn't.
The locking is contained within the mutex implementation. When you acquire a mutex, you either get retun immediately with ownership of the mutex, or your thread waits inside the OS until you do get ownership.
minkowski wrote: Was wondering if this is correct?
As explained above, no
Can I suggest you read up on how mutexes[^] work[^]? That should maybe make things a bit clearer...
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Oh I see, so since its the same lock object ( objectMutex from your example in the get() and set() functions) of course it will be locked?
|
|
|
|
|
That's it. So, as each instance of MyClass has a separate instance of objectMutex, you can access different instances of MyClass concurrently on separate threads, but not the same instance (because the mutex will stop you).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Ah excellent. Makes sense! Thanks for your time (and patience!)
|
|
|
|
|
My pleasure
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hello,
Does anyone know of a way to disable the "What's This?" menu (appears after right-clicking on certain items)? I would like to disable it on my "Save As" dialog. I've created a class for the dialog and it is inherited from the CFileDialog class. I have looked at many places on the internet, but I'm unable to find what I need. Any ideas on this would be appreciated.
Regards,
Mike
|
|
|
|
|
Sounds like you need to remove the WS_EX_CONTEXTHELP style.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
In the OnInitDialog function of my class derived from CFileDialog, I have the following:
GetParent ()->ModifyStyleEx(WS_EX_CONTEXTHELP, 0);
The only difference I noticed was that the question mark button, which is normally located to the left of the close button (the X), disappeared. The What's This? is still showing.
Regards,
Mike
|
|
|
|
|
I guess I misunderstood what you were after. I do not know of a way of removing that little context menu. I guess you could try hooking the Save As dialog and then intercepting the right-click message.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
// ----------------------------------------------------------------------------------------------
// myfiledialog.h
// ----------------------------------------------------------------------------------------------
#pragma once
#include "afxdlgs.h"
class CMyFileDialog :
public CFileDialog
{
public:
CMyFileDialog(void);
~CMyFileDialog(void);
virtual BOOL OnInitDialog();
};
// ----------------------------------------------------------------------------------------------
// end myfiledialog.h
// ----------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------
// myfiledialog.cpp
// ----------------------------------------------------------------------------------------------
#include "stdafx.h"
#include "myfiledialog.h"
#include "resource.h"
CMyFileDialog::CMyFileDialog() : CFileDialog(FALSE,0,0,OFN_EXPLORER|OFN_PATHMUSTEXIST|
OFN_HIDEREADONLY|OFN_LONGNAMES|OFN_OVERWRITEPROMPT| OFN_CREATEPROMPT)
{
}
CMyFileDialog::~CMyFileDialog()
{
}
BOOL CMyFileDialog::OnInitDialog()
{
CFileDialog::OnInitDialog();
GetParent ()->ModifyStyleEx(WS_EX_CONTEXTHELP, 0);
return TRUE; // return TRUE unless you set the focus to a control
}
// ----------------------------------------------------------------------------------------------
// end myfiledialog.cpp
// ----------------------------------------------------------------------------------------------
I hope this helps.
Regards,
Mike
|
|
|
|
|
Just out of curiosity, why derive a class from CFileDialog rather than just create an instance of it?
UINT_PTR CALLBACK OFNHookProc( HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam )
{
return 0 or 1;
}
CFileDialog fd(FALSE, 0, 0,
OFN_ENABLEHOOK | OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_OVERWRITEPROMPT | OFN_CREATEPROMPT);
fd.m_ofn.lpfnHook = OFNHookProc;
fd.DoModal();
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
The application I'm working with uses a CFileDialog format, except for 2 buttons that have been added to the dialog. This is the reason for deriving from CFileDialog.
Anyway, after trying your code, I'm still seeing the "What's This?" menu. When I debug your code, and set a breakpoint in OFNHookProc, the breakpoint is reached several times, but is never reached when I click on a button using the secondary mouse button. When I click on a button using the secondary mouse button, the "What's This?" menu appears. I tried both return values (0 and 1 from function OFNHookProc) with no success.
Regards,
Mike
|
|
|
|
|
Michael Adamus wrote: Anyway, after trying your code, I'm still seeing the "What's This?" menu.
I simply provided you a starting point. You'll need to do some research in order to fill in the blanks. There are no guarantees that this will work, but at least you'll have found one more way that doesn't. You can use Spy++ to see the relationships of that dialog. The hook procedure is actually for the child dialog of the main dialog. You can even customize the child dialog by providing your own template.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
OK, thank you for your time and effort. Hopefully, I'll be able to figure out how Spy++ works by doing my own research.
Regards,
Mike
|
|
|
|