Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I'm using threads with dinamic queues to elaborate images, from a streaming video (using openCV funct).

I don't know how many images I have to elaborate, and so, I want to ask you if someone can tell me a method or idea to stop all threads in execution with an external variables (form main o similar) ...something like a "STOP" button, but I don't use GUI, I'm work on a console application.

How can I do this? It's possible? Exist a mode?

These threads works like an assembly chain...

Thread 1 pop from a queue1 and push on a queue2...
Thread 2 pop from a queue2 and push on a queue3...
Thread 3 pop from a queue3 and push on a queue4...and so on.

Enviroment is Visual C++ 2010, Windows OS, OpenCV for manage images and video streaming.

Someone can help me?
Posted
Comments
Sergey Alexandrovich Kryukov 10-Jun-13 13:52pm    
It depends on what those threads do. Do they execute some scenario based on the cycle? The universal approach does exist, but it is somewhat controversial...
—SA
Domus1919 10-Jun-13 13:57pm    
Every thread do an operation on image, that passing each other...greyscale, threashold, erode/dilate (I have asked about that situation with queue in this forum few days ago).

I need a mode to stop this elaboration when I want...but I don't know how...

1 solution

The solution is so easy that you don't see it:

Each of your threads executes in a loop - most likely. At some point of the loop that is being executed in every iteration place a test like the following:
C++
if (!gContinue)
    break;

where gContinue is the global variable of type bool, and which should be true as long as your threads should continue working. Once you set gContinue to false, all your threads will fall out of their loops. And beheind that main loop should be a statement that terminates the thread - usually a simple return instruction.
 
Share this answer
 
Comments
Domus1919 10-Jun-13 16:08pm    
mmm...it may be a solution...But, how set this variables when threads running?
nv3 10-Jun-13 16:24pm    
In your controlling thread (or any other thread) just do
gContinue = false;
Sooner or later all your threads will sense that the variable is on false and terminate. You can detect that all other threads have terminated by either checking in a loop with for example a sleep (10), or more elegantly by waiting on the thread objects to signal (google for it and you will find how to do that).
H.Brydon 17-Jun-13 0:07am    
[I'm not completely clear on the OP's question, but it looks like an implementation of a pipeline.]

If you use a global or thread common variable like "gContinue" in C++ (or C# or java), the compiler is free to optimize access to it and you might find yourself with several threads running infinite loops. At the very least, gContinue must be declared volatile and (depending on some other unstated features of your code) it might also need to be accessed through an API such as an accessor to work correctly.

Multithreading introduces a lot of subtle memory model issues that are not obvious in a single thread environment.
Domus1919 12-Feb-14 7:44am    
I've think about signal library, and write this code:

#include signal.h

bool forever = false;

void sighandler(int sig)
{
cout<<endl<<"Signal "<<sig<<" received. Terminate program."<<endl;
forever = true;
}

...here threads functions...

int main(int argc, char **argv)
{
signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler);
signal(SIGINT, &sighandler);

while (!forever)
{
...some operatios here
}


//join
WaitForSingleObject(handle0,INFINITE);
WaitForSingleObject(handle1,INFINITE);
WaitForSingleObject(handle2,INFINITE);
WaitForSingleObject(handle3,INFINITE);
WaitForSingleObject(handle4,INFINITE);

//end program
return 0;
}

the same while cycle are in the threads function...CTRL+C was relevated, but threads and main part related to that boolean don't end.

Can someone help me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900