linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Steve Graegert" <graegerts@gmail.com>
To: Sreevathsa <sreevathsa@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: PThread question
Date: Thu, 15 Mar 2007 10:37:34 +0100	[thread overview]
Message-ID: <6a00c8d50703150237g5ceef20ev87ea8bf9843e0cfe@mail.gmail.com> (raw)
In-Reply-To: <45f8f22b.0c41e030.3bf2.ffff8dda@mx.google.com>

On 3/15/07, Sreevathsa <sreevathsa@gmail.com> wrote:
> I have a question on syncing the threads. I have a scenario like this:
>
> 5 threads (T1, T2, T3, T4 & T5) are working on this piece of code:
>
> LINE1:  for(;;) {
> LINE2:          pthread_mutex_lock(&r1_mutex)
> LINE3:          .............
> LINE4:          .............
> LINE5:          pthread_mutex_unlock(&r1_mutex)
>
>
>                         /* some code that does not need protection */
> LINE6:          .............
> LINE7:          .............
>
>
> LINE8:          pthread_mutex_lock(&r2_mutex)
> LINE9:          .............
> LINE10:                 .............
> LINE11:                 pthread_mutex_unlock(&r2_mutex)
> LINE12:         }
>
>
> r1_mutex is to protect the access to a FIFO queue. I want the 5 threads to
> process the queue contents in the same order in which they arrived into the
> queue.
>
> Now, lets assume that a request comes into the queue and thread T1 picks up
> and starts processing it. T1 has released r1_mutex lock and is holding
> r2_mutex lock and is executing the code in lines 9 and 10.
>
> While T1 is busy executing code on lines 9 and 10, the queue gets 4 more
> requests and threads T2, T3, T4 and T5 (in that order) picks each one of
> them in the order in which they came into the queue and start processing
> them. They come till LINE 8 and wait to lock r2_mutex which thread T1 has
> currently locked.
>
> Now, given this scenario, here is my question:
> After T1 unlocks r2_mutex, which thread among T2, T3, T4 and T5 gets the
> r2_mutex lock? Does pthread scheduler schedule (give the lock to) thread T2
> which came to LINE8 first??

Sreevathsa,

I will not ask you why you want to enforce some sort of artificial
"scheduling policy" for your threads with regard to the problem you're
trying to solve.  I'd generally recommend decoupling the comsumer
pattern from the communication pattern.  While a FIFO guarantees items
to be processed in a particular order, the order of execution of
threads is beyond your control, unless you apply a scheduling policy
to each thread, which may require superuser privileges (see SCHED_FIFO
and SCHED_RR).

You may want to create a chain of threads (instead of a tree), each
one waiting for the other to complete (see pthread_join).  Every
controller thread could then create a single worker thread to process
the data, thus the controller thread can relinquish the CPU
afterwards.

Anyhow, I recommend not to rely on the order of items arriving in the
FIFO.  Instead, I'd introduce another layer of abstraction to map
items to particular threads.

	\Steve

--

Steve Grägert <steve@graegert.com>
Jabber    xmpp://graegerts@jabber.org
Internet  http://eth0.graegert.com, http://blog.graegert.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2007-03-15  9:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-15  7:13 PThread question Sreevathsa
2007-03-15  9:14 ` krishna.vamsi
2007-03-15  9:37 ` Steve Graegert [this message]
2007-03-19 20:22 ` Hendrik Visage
2007-03-20 11:29 ` Neil Horman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d50703150237g5ceef20ev87ea8bf9843e0cfe@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=sreevathsa@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).