/usr/share/gtk-doc/html/libvips/using-threads.html is in libvips-doc 8.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>VIPS and threads: VIPS Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="VIPS Reference Manual">
<link rel="up" href="ch01.html" title="VIPS Overview">
<link rel="prev" href="file-format.html" title="The VIPS file format">
<link rel="next" href="ch02.html" title="Core VIPS API">
<meta name="generator" content="GTK-Doc V1.24 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="ch01.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="file-format.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="ch02.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="using-threads"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle">VIPS and threads</span></h2>
<p>Using VIPS — VIPS and threading</p>
</td>
<td class="gallery_image" valign="top" align="right"></td>
</tr></table></div>
<div class="refsect3">
<a name="using-threads-intro"></a><h4>Introduction</h4>
<p>
This section tries to summarise the rules for threaded programs using
libvips. Generally, libvips is threaded and thread-safe, with a few
exceptions.
</p>
</div>
<div class="refsect3">
<a name="using-threads-images"></a><h4>Images</h4>
<p>
On startup, you need to call <a class="link" href="libvips-vips.html#VIPS-INIT:CAPS" title="VIPS_INIT()"><code class="function">VIPS_INIT()</code></a> single-threaded. After that,
you can freely create images in any thread and read them in any other
thread. See the example at the end of this chapter.
Note that results can also be shared between threads for you by the vips
operation cache.
</p>
<p>
The exception is the drawing operators, such as <a class="link" href="libvips-draw.html#vips-draw-circle" title="vips_draw_circle ()"><code class="function">vips_draw_circle()</code></a>.
These operations modify their image argument so you can't call them on
the same image from more than one thread. Reading from an image while
another thread is writing to it with one of the draw operations will
obviously also fail.
</p>
<p>
When libvips calculates an image, by default it will use as many
threads as you have CPU cores. Use <a class="link" href="libvips-VipsThreadState.html#vips-concurrency-set" title="vips_concurrency_set ()"><code class="function">vips_concurrency_set()</code></a> to change this.
</p>
</div>
<div class="refsect3">
<a name="using-threads-errors"></a><h4>Error handling</h4>
<p>
libvips has a single error code (-1 or <code class="literal">NULL</code>) returned by all functions
on error. Error messages are not returned, instead they are logged
in a single global error buffer shared by all threads, see
<a class="link" href="libvips-error.html#vips-error-buffer" title="vips_error_buffer ()"><code class="function">vips_error_buffer()</code></a>.
</p>
<p>
This makes error handling very simple but the obvious downside is that
because error returns and error messages are separate when you
detect an error return you can't be
sure that what's in the error buffer is the message that matches your
error.
</p>
<p>
The simplest way to handle this is to present the whole error log to
the user on the next interaction and leave it to them to decide what
action caused the failure.
</p>
</div>
<div class="refsect3">
<a name="using-threads-regions"></a><h4>Using <span class="type">VipsRegion</span> between threads</h4>
<p>
<span class="type">VipsImage</span> objects are immutable and can be shared between
threads very simply.
However the lower-level <span class="type">VipsRegion</span> object used to implement <span class="type">VipsImage</span>
(see <a class="link" href="extending.html" title="Extending VIPS">Extending VIPS</a>) is mutable and you
can only use a <span class="type">VipsRegion</span> from one thread at once.
</p>
<p>
In fact it's worse than that: to reduce locking, <span class="type">VipsRegion</span> keeps a
lot of state in per-thread storage. If you want to create a region in
one thread and use it in another, you have to first tag the region as
unowned from the creating thread with <code class="function">vips__region_no_ownership()</code>, then
in the recieving thread take ownership with
<code class="function">vips__region_take_ownership()</code>. See the source for operations like
<a class="link" href="libvips-conversion.html#vips-tilecache" title="vips_tilecache ()"><code class="function">vips_tilecache()</code></a> if you're curious how this works.
</p>
<p>
libvips includes a set of sanity checks for region ownership and will
fail if you don't pass ownership correctly.
</p>
</div>
<div class="refsect3">
<a name="using-threads-example"></a><h4>Example</h4>
<p>
This example runs many <a class="link" href="libvips-resample.html#vips-resize" title="vips_resize ()"><code class="function">vips_resize()</code></a> in parallel from many threads.
</p>
<div class="example">
<a name="id-1.2.11.7.3"></a><p class="title"><b>Example 2. VIPS and threads example</b></p>
<div class="example-contents"><pre class="programlisting">
/* Read from many threads.
*
* Compile with:
*
* gcc -g -Wall soak.c `pkg-config vips --cflags --libs`
*
* Run with:
*
* rm -rf x
* mkdir x
* for i in {0..10}; do ./a.out ~/pics/k2.jpg; done
*
*/
#include <stdio.h>
#include <glib.h>
#include <vips/vips.h>
/* How many pings we run at once.
*/
#define NUM_IN_PARALLEL (50)
/* Number of tests we do in total.
*/
#define TOTAL_TESTS (NUM_IN_PARALLEL * 20)
/* Workers queue up on this.
*/
GMutex allocation_lock;
/* Our set of threads.
*/
GThread *workers[NUM_IN_PARALLEL];
/* Number of calls so far.
*/
int n_calls = 0;
/* Our test function. This is called by NUM_IN_PARALLEL threads a total of
* TOTAL_TESTS times.
*/
static int
test (const char *filename)
{
VipsImage *im, *x;
char output_file[256];
snprintf (output_file, 256, "x/tmp-%p.jpg", g_thread_self ());
if (!(im = vips_image_new_from_file (filename,
"access",
VIPS_ACCESS_SEQUENTIAL_UNBUFFERED,
NULL)))
return (-1);
if (vips_resize (im, &x, 0.1, NULL))
{
g_object_unref (im);
return (-1);
}
g_object_unref (im);
im = x;
if (vips_image_write_to_file (im, output_file, NULL))
{
g_object_unref (im);
return (-1);
}
g_object_unref (im);
return (0);
}
/* What we run as a thread.
*/
static void *
worker (void *data)
{
const char *filename = (const char *) data;
for (;;)
{
gboolean done;
done = FALSE;
g_mutex_lock (&allocation_lock);
n_calls += 1;
if (n_calls > TOTAL_TESTS)
done = TRUE;
g_mutex_unlock (&allocation_lock);
if (done)
break;
if (test (filename))
vips_error_exit (NULL);
}
return (NULL);
}
int
main (int argc, char **argv)
{
int i;
if (VIPS_INIT (argv[0]))
vips_error_exit (NULL);
g_mutex_init (&allocation_lock);
for (i = 0; i < NUM_IN_PARALLEL; i++)
workers[i] = g_thread_new (NULL, (GThreadFunc) worker, argv[1]);
for (i = 0; i < NUM_IN_PARALLEL; i++)
g_thread_join (workers[i]);
return (0);
}
</pre></div>
</div>
<br class="example-break">
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.24</div>
</body>
</html>
|