Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #125

Re: pthread question

From Vaclav Haisman <v.haisman@sh.cvut.cz>
Newsgroups comp.programming.threads
Subject Re: pthread question
Date 2011-05-09 07:11 +0200
Organization Czech Technical University
Message-ID <4DC7778E.8090707@sh.cvut.cz> (permalink)
References <b3bf253c-0285-4eeb-8d64-e01fa16c5f0c@f15g2000pro.googlegroups.com>

Show all headers | View raw


p6 c6d6 wrote, On 9.5.2011 6:28:
> can pthread_func_1() eat up 100% cpu.
> i have the following scenario where pthread_fuc_1()
> is waiting on some global variable to be set.
> while pthread_func_2() does the setting of the global
> variable once it finsishes it's busy work.
> pthread_func_2() might wait on condition_variable while
> it is doing the work. In this scenario i've observed that
> my process is taking 100% cpu so i forced manual core
> on analyzing the core i've observed that pthread_func_1()
> thread is in Rnning state, pthread_func_2() is in cond_wait
> state. i was wondering, why pthread_func_1() is taking 100%
> cpu though there is pthread_yield(). i am guessing that
> it is reponsible for 100% cpu as it is the only thread in
> the running state, when forced it to coredump.
> 
> pthread_func_1() {
>     while (condition_not_met) {
>         pthread_yield();
Even with pthread_yeield() it is busy waiting. You are correct about why,
despite the yield, it runs all the time. Change it to use conditional
variable as the pthread_func_2() does to avoid that.

>     }
> }
> 
> pthread_func_2() {
>     while (there is busy work) {
>         doing some busy work;
>         wait on some condition var;
>     }
>     condition_not_met = 0;
> }

-- 
VH

Back to comp.programming.threads | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

pthread question p6 c6d6 <p6c6d6@gmail.com> - 2011-05-08 21:28 -0700
  Re: pthread question Vaclav Haisman <v.haisman@sh.cvut.cz> - 2011-05-09 07:11 +0200
  Re: pthread question Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-09 11:49 -0700
    Re: pthread question David Schwartz <davids@webmaster.com> - 2011-05-09 12:24 -0700
      Re: pthread question Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-09 14:22 -0700
        Re: pthread question "Chris M. Thomasson" <cristom@charter.net> - 2011-05-09 15:33 -0700
        Re: pthread question David Schwartz <davids@webmaster.com> - 2011-05-10 01:22 -0700
    Re: pthread question Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2011-05-10 13:47 +0100
      Re: pthread question Alexander Terekhov <terekhov@web.de> - 2011-05-10 17:53 +0200
        Re: pthread question Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2011-05-11 13:46 +0100
          Re: pthread question Alexander Terekhov <terekhov@web.de> - 2011-05-11 16:19 +0200
            Re: pthread question Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2011-05-12 13:26 +0100
              Re: pthread question Alexander Terekhov <terekhov@web.de> - 2011-05-12 15:28 +0200
                Re: pthread question Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2011-05-13 14:00 +0100
                Re: pthread question Alexander Terekhov <terekhov@web.de> - 2011-05-13 22:59 +0200
                Re: pthread question Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2011-05-16 13:28 +0100

csiph-web