tags:

views:

201

answers:

5

Hi Guys,

Im trying to run a function that will never end (until the program is killed)

How would i start such a function and be able to continue on past that function, because at the moment the program will not run past the never ending function.

Regards

Paul

+9  A: 

You'd need to start a new thread. A given thread can only execute one function at a time. So, if you want to have two separate functions executing at the same time, you need multiple threads. You'll need to look into multi-threaded programming. If you're on linux or another unix-based system, then the library to look at would be pthreads.

Jonathan M Davis
+1  A: 

Fire it off in a thread. It would still, however, be really sensible to allow the function to exit.

Goz
A: 

An idea for a pseudo-thread based on a similar issue I needed to fix without threading (may or may not work for you):

  1. Start the application.
  2. when it gets to where the function is supposed to run, call a timer to start (very short time).
  3. timer calls back and starts the never-ending function then quits.
  4. Never-ending function includes a 10 ms sleep and a call to process all messages.
Tom
I've no idea why this is voted down! This is the most sensible answer especially the 10ms sleep to allow the cpu to process other stuff. Threads are overkill for such a simple problem.
Gary Willoughby
im trying to understand how this would work, surely the fact that the function never ends means that even when the timer calls back, it wont go any further because the never ending function will never complete?!
paultop6
@PaulThe timer starts running the code, meantime, the rest of the application is left to continue running and waiting for user input. Because of the sleep and message processing, the program can act as if it were a pseudo-thread. It's a major hack, I'll admit, but for my scenario, it works extremely well as my application spends all of its time waiting for input, then briefly displays a message to the user. Just remember to add in the never-ending function a check for a flag to tell it to quit, and other parts of the application can set that flag.
Tom
It's getting voted down because you're subverting the main message processing loop of your GUI toolkit. Much better to use the idle message or timeout defined by your GUI toolkit and then return back to the main message loop, letting it call you again when the timeout elapses. Having message processing deep in the call stack is bad design. Using sleep for this is really bad design.
Ben Voigt
@Ben - I understand the reason for the downvote (and don't have a problem with it), but I don't understand how I'm subverting the message processing loop. The function continues to run forever, its true, but there is a flag (as noted in my comment to Paul) that tells it to quit if needed. Also, with this running from a timer call, the rest of the message loop is left alone, and in my case, the run is appropriate (in my opinion) is it is a wait until condition satisfied, then return to previous function call. I can't wait and have it call a second time, as then my data is lost.
Tom
I don't understand how you're losing data... local variables disappear but you can keep state in an object that lives between function calls. Most GUI toolkits pass a user-defined parameter to timer callbacks for that purpose. What do you mean by "quits" in (#3) <quote>timer calls back and starts the never-ending function then quits</quote>. If your timer callback starts another thread, this isn't pseudo-threading. If your timer callback calls a function that doesn't return, the callback doesn't return either. Perhaps you're using a timer that uses the threadpool for callbacks?
Ben Voigt
My app is actually a Delphi GUI app, I was just passing on the principle I used. The timer doesn't create a thread, it just starts a loop running that continually checks if a condition is true, and if not, sleep for 10 ms, then let the rest of the application run. When it gets to true, then the loop exits. This works for me because the user interaction is limited to selecting one of a few buttons.
Tom
+2  A: 

While I'd agree with everyone else that a thread is probably the easiest way to go, this scenario doesn't sound like something that should ever happen (at least not deliberately) - it sounds like this originates from a design problem.

Why do you need a function that will run forever? What does this function do? There may be a more appropriate programming model, such as a Windows service or a daemon process. Also, it's usually better to send the thread a signal that it's time to end, and have the thread do it's thing until it receives such a message (rather than just force-killing it when the app exits).

I don't know enough about what you're trying to do here, but I'd suggest examining your design and see if there's a better way.

FrustratedWithFormsDesigner
Basically i have a GtkTreeView widget that has several columns. The data that is going to be in these columns is changing every few seconds. I was thinking i needed to flush the rows in those columns and then place the new data in, and do this on an infinate loop, hence needing a function to run in the background to constantly flush and place in this new data. Due to lack of programming experience, im not sure of an other way to get around this, perhaps someone could enlighten me.
paultop6
@paultop6: Ah, in this case your solution sounds like it's a good start. The only thing I'd change is that this auto-refresh thread should be able to be shutdown via a signal from the main process.
FrustratedWithFormsDesigner
@paultop6: Such a solution doesn't require a function in the background. What you need is an _event handler_. An event handler is a function which is called every time an event happens. For instance, when new data arrives. The event handler can return as soon as the event is processed.
MSalters
+1  A: 

Executing "past" a never-ending function isn't possible (if it doesn't end, you can't move past it). I am assuming that you want to have some code running in a loop in the background while your program does something else.

You want something like this:

#include <pthread.h>

int endLoop;

void* backgroundLoop(void* arg) {
    while (!endLoop) {
        ...
    }
    return NULL;
}

int main (int argc, char** argv) {
    pthread_t thread;
    int retval;
    ...
    // Launch background thread
    endLoop = 0;
    retval = pthread_create(&thread, NULL, backgroundLoop, NULL);

    // Continue with main function
    ...
    // Force background loop to end
    endLoop = 1;

}

If you are using pthreads, you can find more info here. If you are using some other thread library, the concept will be similar but the thread create/destroy functions may look different.

bta
I have tried this; pthread_t thread; pthread_create(But it will not work for me, stating; invalid conversion from ‘void*’ to ‘void* (*)(void*)’Am i using the pthread_create command correctly?
paultop6
Don't cast ``
bta