Struggling with a loop

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Struggling with a loop

Mats Aubell
Hi all (again)

One problem solved and three spawn, that's usually how things work. I've
been messing around with pthread, but I'm stuck at the final problem which
hopefully will solve everything. My code is something like this:

Class SR

main
{
SR *top = new SR();
Method()
Method()
do {
        top->Ask();
        top->Reset();
} while(1);
}

I want the do-while loop to go in its own thread. I've tried several things,
like putting it into a method like this:

void * RunApplication(void * arg)
{
        SR *top = new SR();
        StartRecogniser();
        do {
                top->Ask();
                top->Reset();
        } while(1);
        return 0;
}

And calling it with this:

pthread_create(&thread, NULL, RunApplication, NULL)

but it wouldn't go past the SR *top = new SR();

I tried to pass it like this:

pthread_create(&thread, NULL, RunApplication, (void*)top)

with the following method:

void * RunApplication(void * arg)
{
        SR *top = SR*(arg);
        StartRecogniser();
        do {
                top->Ask();
                top->Reset();
        } while(1);
        return 0;
}

but the create thread method didn't approve of it.

The RunApplication() works fine when I'm not calling it through the
pthread_create, but I need it to run in it's own thread.

As you can tell, my C++ skills aren't exactly top notch and I've not done
threads on anything but Java so I'm struggling quite a bit. Any feedback
would be of tremendous help, I'm nearing desperation with this.

Cheers,
Mats Aubell