Find the answer to your Linux question:
Results 1 to 4 of 4
Hello ppl! How do you implement suspend/resume for pthreads? I read somewhere that you have to use a combination of mutex, condition variables and boolean predicates to implement this feature. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2004
    Posts
    3

    resume and suspend pthreads


    Hello ppl!

    How do you implement suspend/resume for pthreads? I read somewhere that you have to use a combination of mutex, condition variables and boolean predicates to implement this feature. This is required because i have to port some Windows code to linux and there is this class CPrcThread which is an abstraction for threads. So there are thread.suspend and thread.resume functions in the windows API so thats ok, but how do i do that with pthreads? I have come up with a solution of my own but it has hit a snag. This is my solution

    Code:
    void CPrcThread<Worker>&#58;&#58;suspend&#40;&#41;
    &#123;
        ifdef WIN32
        //do windows specific things here... 
        #endif
    
        #ifdef __linux__
         pthread_mutex_lock&#40;&mutex&#41;;
         flag--;
         pthread_mutex_unlock&#40;&mutex&#41;;
        #endif
    &#125;
    
    void CPrcThread<Worker>&#58;&#58;resume&#40;&#41;
    &#123; 
            #ifdef WIN32
            //do windows specific things here... 
            #endif
    
            #ifdef __linux__
            pthread_mutex_lock&#40;&mutex&#41;;
            flag++;
            pthread_cond_signal&#40;&cond&#41;;
            pthread_mutex_unlock&#40;&mutex&#41;;
            #endif
    &#125;
    
    void* CPrcThread<Worker>&#58;&#58;threadFunc&#40;void* pParameter&#41;
    &#123;
    
    while&#40;1&#41;
    &#123;
    
            #ifdef WIN32
            //do windows specific things here... 
            //no member variables accessed here so its ok...
            #endif
    
    
             #ifdef __linux__
               pthread_mutex_lock&#40;&mutex&#41;;
               while&#40;flag <= 0&#41;
               &#123;
                       pthread_cond_wait&#40;&cond, &mutex&#41;;
               &#125;
               pthread_mutex_unlock&#40;&mutex&#41;; 
              #endif
    
    
              //actual thread work here
     
    &#125;
    
    &#125;
    mutex, cond, and flag are member variables of CPrcThread.
    First of all, i dont know if this is the correct way to implement suspend/resume? Secondly, the bigger problem is that the threadFunc function is static for reasons i cant control. How do i then access the member variables??

    Thanks in advance!

  2. #2
    Just Joined!
    Join Date
    May 2004
    Posts
    3
    ahem..20+ views and no replies!...is this such a difficult question that nobody can answer??

  3. #3
    Just Joined!
    Join Date
    May 2004
    Location
    Oxford, UK
    Posts
    15
    The question iteself is not very difficult, but you need experience with pthread programming to be able to give a correct answer. I'm reading the books on pthread programming at the moment, maybe I could answer your question in a few weeks time

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    May 2004
    Posts
    3

    looking forward...

    ..to hearing from you then...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •