|
Are you simply wanting to know how to search a vector ?
"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
|
|
|
|
|
it's a little bit more complicated
I'm looking for a way to search a vector into an other vector
std::set_intersection can do it, but in my case I have to respect the order of the sequence
|
|
|
|
|
kaminem wrote: ...search a vector into an other vector
What does this mean?
"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
|
|
|
|
|
I want to search vect_2 into Vect_1
for example
Vect_1 < "JAVA", "ADA", "C", "C++", "C#">
Vect_2 < "C++", "C#">
here vect_1 contain all vect_2 elemnts and in the some order
but if I don't find the entire sequence I should look for a subsequence too (and this is my big pb)
I'm trying to avoid the use of for loop
|
|
|
|
|
solution
use of std::search
|
|
|
|
|
Hello Everyone,
I have Demo MFC application, with 2 buttons Start and End Button.
Start button will start 2 threads
void CThreadDemoDlg::OnStart()
{
HANDLE hr1,hr2;
hr1 = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadOne,this,0,0);
hr2 = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadTwo,this,0,0);
}
UINT WorkerThreadOne(LPVOID Param)
{
fstream OutFile;
OutFile.open("FileOne.txt",ios::out);
for(int i=0;i<10000;i++)
OutFile << i << " ";
OutFile.close();
return true;
}
Can anyone please tell me how to kill the thread ? OnEnd click.
Thanking you,
Suresh.
|
|
|
|
|
First of all, you must use AfxBeginThread, if you're using MFC[^].
void CThreadDemoDlg::OnEnd(){
SetEvent(hEndAllEvent);
}
UINT WorkerThreadXYZ(LPVOID pParams){
fstream OutFile;
OutFile.open("FileOne.txt",ios::out);
for(int i=0;i<10000;i++)
{
if(WaitForSingleObject(hEndAllEvent, 0) == WAIT_OBJECT_0) break;
OutFile << i << " ";
}
OutFile.close();
return FALSE;
}
Have a look at WaitForSingleObject[^]
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Hello Everyone,,
Sorry saw all yr response now.Thanks a lot for the reply.
I am very much new to the Thread Concept. thought of learning the basics of threads.
can anyone suggest me some nice Article to start with.
Thanking you,
Suresh
|
|
|
|
|
There are plenty of articles around, but before all that, do you have a book on MFC programming? If not, please buy one. My book recommendations for MFC are here[^]
Essay recommendation:
Worker Threads[^]
UI Threads[^]
There are plenty of articles at CP as well, just do a search.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Message Closed
modified 14-Oct-15 22:14pm.
|
|
|
|
|
You can forget Terminate*(TerminateThread/TerminateProcess).
MSDN:
TerminateThread is a dangerous function. Use it only in the most extreme cases.
Why 1.00/5 ???
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
modified on Tuesday, April 14, 2009 5:54 AM
|
|
|
|
|
That's probably one of the worst way to do that. You should never call TerminateThread (unless in very specific conditions). In general, there are much better ways to achieve what you want.
|
|
|
|
|
Hi Parag Patel,
Thanks for the reply.
I tried with this below code but still the thread is running its not terminated,
can u please tell how to kill the thread.
void CThreadDemoDlg::OnEnd()
{
TerminateThread(hr1,1);
TerminateThread(hr2,1);
}
|
|
|
|
|
Having looked at your code in your first post, I can tell you that you MUST NOT use TerminateThread(). It's saddening to see you've picked the worst suggestion that was given to you.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Rajesh R Subramanian wrote: It's saddening to see you've picked the worst suggestion that was given to you.
That's usually the case because it looks like the easiest solution
|
|
|
|
|
Maybe you didn't see the replies to his post but it is strongly recommanded not to use TerminateThread.
A better approach would be to pass a pointer to your dialog class to the thread function, and within the thread cast it back to your dialog and call a public function on it. Inside that function you put the code you already provided except that in your loop, you also check if a flag is set or not (this flag is a simple bool which is a member of the dialog). If the flag is set, you simply stop the loop and the function terminates.
When you click on the end button, you can simply set this flag so that the loop stops.
|
|
|
|
|
Hello Cédric Moonen,,
Sorry saw yr response now.Thanks a lot for the reply.
I am very much new to the Thread Concept. thought of learning the basics of threads.
can anyone suggest me some nice basic Article to start with.
Thanking you,
Suresh
|
|
|
|
|
This[^] article is one of the best I know of.
|
|
|
|
|
It looks like we both gave him the same article to read, at the same time (20 mins back, as of now).
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
It seems that somebody is voting 1 to all of our messages. Can you guess who it is ?
|
|
|
|
|
I know, but I'm ignoring that.
I've been balancing the votes. Forget it, the votes will be balanced by the regulars.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Can you paste your code snap here?
You can also use flag mechanism, its good solution.
HANDLE hr1,hr2;
bool stopTh;
void CThreadDemoDlg::OnStart()
{
stopTh = false;
hr1 = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadOne,this,0,0);
hr2 = CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))WorkerThreadTwo,this,0,0);
}
void CThreadDemoDlg::OnEnd()
{
stopTh = true;
}
UINT WorkerThreadOne(LPVOID Param)
{
fstream OutFile;
OutFile.open("FileOne.txt",ios::out);
for(int i=0;i<10000;i++)
{
if(stopTh == true)
break;
OutFile << i << " ";
}
OutFile.close();
return true;
}
Why 1.0/5.0?
Parag Patel
modified on Thursday, April 16, 2009 9:40 AM
|
|
|
|
|
ParagPatel wrote: if(stopTh == true)
Unless stopTh is volatile , the compiler will optimize out this check.
"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
|
|
|
|
|
Thanks ,
Yes.. stopTH must be globle or thread function must use through passed class object.
Parag Patel
Sr. Software Eng, Varaha Systems
|
|
|
|
|
ParagPatel wrote: Thanks Simmons,
ParagPatel wrote: Yes.. stopTH must be globle
Being global has nothing to do with it. If the compiler detects that nothing in the loop is changing that variable, it will optimize out the check. So even if the secondary thread changes that variable, it will go unseen.
"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
|
|
|
|