1 <chapter id="chapter-threads">
4 &GStreamer; is inherently multi-threaded, and is fully thread-safe.
5 Most threading internals are hidden from the application, which should
6 make application development easier. However, in some cases, applications
7 may want to have influence on some parts of those. &GStreamer; allows
8 applications to force the use of multiple threads over some parts of
12 <sect1 id="section-threads-uses">
13 <title>When would you want to force a thread?</title>
15 There are several reasons to force the use of threads. However,
16 for performance reasons, you never want to use one thread for every
17 element out there, since that will create some overhead.
18 Let's now list some situations where threads can be particularly
24 Data buffering, for example when dealing with network streams or
25 when recording data from a live stream such as a video or audio
26 card. Short hickups elsewhere in the pipeline will not cause data
29 <figure float="1" id="section-thread-buffering-img">
30 <title>Data buffering, from a networked source</title>
33 <imagedata scale="75" fileref="images/thread-buffering.ℑ" format="&IMAGE;"/>
41 Synchronizing output devices, e.g. when playing a stream containing
42 both video and audio data. By using threads for both outputs, they
43 will run independently and their synchronization will be better.
45 <figure float="1" id="section-thread-synchronizing-img">
46 <title>Synchronizing audio and video sinks</title>
49 <imagedata scale="75" fileref="images/thread-synchronizing.ℑ" format="&IMAGE;"/>
58 Above, we've mentioned the <quote>queue</quote> element several times
59 now. A queue is the thread boundary element through which you can
60 force the use of threads. It does so by using a classic
61 provider/receiver model as learned in threading classes at
62 universities all around the world. By doing this, it acts both as a
63 means to make data throughput between threads threadsafe, and it can
64 also act as a buffer. Queues have several <classname>GObject</classname>
65 properties to be configured for specific uses. For example, you can set
66 lower and upper thresholds for the element. If there's less data than
67 the lower threshold (default: disabled), it will block output. If
68 there's more data than the upper threshold, it will block input or
69 (if configured to do so) drop data.
72 To use a queue (and therefore force the use of two distinct threads
73 in the pipeline), one can simply create a <quote>queue</quote> element
74 and put this in as part of the pipeline. &GStreamer; will take care of
75 all threading details internally.
79 <sect1 id="section-threads-scheduling">
80 <title>Scheduling in &GStreamer;</title>
83 Each element in the &GStreamer; pipeline decides how it is going to
84 be scheduled. Elements can choose to be scheduled push-based or
86 If elements support random access to data, such as file sources,
87 then elements downstream in the pipeline can ask to schedule the random
88 access elements in pull-based mode. Data is pulled from upstream
89 and pushed downstream. If pull-mode is not supported, the element can
90 decide to operate in push-mode.
93 In practice, most elements in &GStreamer;, such as decoders, encoders,
94 etc. only support push-based scheduling, which means that in practice,
95 &GStreamer; uses a push-based scheduling model.