tags:

views:

1054

answers:

3

hi,everyone.

Now i face a problem in my porting job, when i need to implement a thread class that will work in not only wince, symbian ,but also unix-like system, like iphone.

I own a suspend/resume interface to implement, anything is ok in wince/symbian except iphone, i use the posix pthread to finish my job, but i search the whole docsets for a resume/suspend-like interface. Things seem to be difficult, pthread in iphone own a *pthread_create_suspended_np* that can create a thread in a suspend mode. Now how can i resume or suspend a thread after the thread has run to its stuff in anytime.

BTW, i search Google for some help, it seems that someone else also have this problem . Some guys suggest use the SIGHUP signal, but this will suspend the whole process, that's absolutely not ok .

Many thanks if you guys can tell me some solutions for this problem.

A: 

To suspend and resume a running thread, I believe you need to use pthread_cond_wait. Basically, that suspends the calling thread until the condition variable becomes true. Of course, you need to also give each thread a way to figure out when to call the function.

Matthew Flaschen
+1  A: 

It's actually a bad idea to try and control threads externally. You never know what state they may be in when you suspend them. If they have a mutex lock on a resource that's needed elsewhere, you can easily end up with a deadlock situation.

We had to create a "safe" suspend functionality without resorting to any non-portable pthread extensions a while ago and I'll try to remember how we did it.

It consisted of a suspension mutex for each thread and a variable indicating that threads state. So the thread we wanted to suspend would have a loop (they mostly do) that went something like this:

while true:
    set mystate = suspended
    claim mymutex
    yield
    release mymutex
    set mystate = running
    do some work

and the code to suspend/resume the thread would be:

function suspend (state,mutex):
    claim mutex
    while state <> suspended:
        yield
function resume (state,mutex):
    release mutex
    while state <> running:
        yield

What the suspender would do is basically get a lock on the mutex and wait for the thread to enter suspended state (the writing to mystate was done only by the suspendee and did not have to be protected by another mutex). The suspend function did not return until it was guaranteed that the suspendee would be stopped.

Similarly, resuming the thread released the mutex so the suspendee could restart and then waited until it had restarted before returning.

This allowed suspension to take place but under the control of the thread being suspended. That was much safer since it could ensure it could only be suspended at safe points when it didn't have any locks that could deadlock the application.

paxdiablo
A: 

Hi, pax.

you give me a clue to solve this problem , yet this method or pthread_cond_wait just wait/singal when design some kind of situation. How can i suspend the special pthread without know more info. about the thread, we just own a thread id, we suspend it in another thread, when we wanna resume the suspended thread, we just probably run the resume function in anywhere. can we do something like this? or any other idea ?

regards.

Eric
If you have functions which will suspend threads (pthread_suspend or pthread_suspend_np in some implementations but not Linux, I believe), then yes, you can. You just need to be aware of the consequences. I don't know whether the iPhone has this capability, I just know I'd never use it even if it did.
paxdiablo
In addition, I'm not notified on responses to your questions (or this answer). You'd be better off leaving a comment on my answer so I get notified.
paxdiablo
Thanks so much for your reply, Pax.Finally I use a triggle insert into the thread run loop, this triggle just use the pthread_cond_wait .When other thread supendThread, it just modify the current state and init a new cond. In this situation, when thread run to the triggle point, it will wait untile someone resumeThread.Although this method do not work perfect, yet it is a solution in some case.Thanks anyway, Best Regreads.
Eric