blob: 305fc29337ca17b806e92e2a938be3421ea7562e [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>C Thread Pool / Apache Celix</title>
<link rel="icon" href="/assets/img/favicon.ico">
<link href="/assets/css/bootstrap.min.css" rel="stylesheet">
<link href="/assets/css/style.css" rel="stylesheet">
<style>
.card-body img {
max-width: 100%;
width: 100%;
height: auto;
}
.card-body img + em {
text-decoration: underline;
}
</style>
<script>
var _paq = window._paq = window._paq || [];
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://analytics.apache.org/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '9']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
</head>
<body class="light-grey">
<a href="https://github.com/apache/celix" class="github-ribbon">
<img src="/assets/img/forkme_right_red_aa0000.png" alt="Fork me on GitHub">
</a>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary fixed-top">
<div class="container">
<a class="navbar-brand" href="/">
<img src="/assets/img/celix-white.svg" height="40" class="d-inline-block align-top" alt="Celix Logo">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/download.cgi">Download</a>
</li>
<li class="nav-item dropdown active">
<a class="nav-link dropdown-toggle" href="#" id="ddDocs" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Docs
</a>
<div class="dropdown-menu" aria-labelledby="ddDocs">
<a class="dropdown-item" href="/docs/2.4.0/docs.html">2.4.0 (latest)</a>
<a class="dropdown-item" href="/docs/2.3.0/docs.html">2.3.0</a>
<a class="dropdown-item" href="/docs/2.2.1/docs.html">2.2.1</a>
<a class="dropdown-item" href="/docs/2.1.0/docs.html">2.1.0</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="ddContributing" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Contributing
</a>
<div class="dropdown-menu" aria-labelledby="ddContributing">
<a class="dropdown-item" href="/contributing/youatcelix.html">You at Celix</a>
<a class="dropdown-item" href="/contributing/submitting-patches.html">Submitting patches</a>
<a class="dropdown-item" href="/contributing/source-and-builds.html">Source code and builds</a>
<hr>
<a class="dropdown-item" href="/contributing/releasing.html">Releasing</a>
<a class="dropdown-item" href="/contributing/volunteers.html">Volunteers</a>
<a class="dropdown-item" href="https://whimsy.apache.org/board/minutes/Celix.html">Board Reports</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="ddSupport" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Support
</a>
<div class="dropdown-menu" aria-labelledby="ddSupport">
<a class="dropdown-item" href="/support/mailing-list.html">Mailing Lists</a>
<a class="dropdown-item" href="/support/issue-tracking.html">Issue Tracking</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="ddFoundation" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
ASF
</a>
<div class="dropdown-menu" aria-labelledby="ddFoundation">
<a class="dropdown-item" href="https://www.apache.org/">ASF Home</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/how-it-works.html">How it works</a>
<a class="dropdown-item" href="https://www.apache.org/licenses/">License</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/thanks.html">Thanks</a>
<a class="dropdown-item" href="https://www.apache.org/security/">Security</a>
<a class="dropdown-item" href="https://www.apache.org/foundation/policies/conduct">Code of Conduct</a>
</div>
</li>
</ul>
</div>
</div>
</nav>
<div class="section">
<div class="container">
<div class="row py-4">
<div class="col-sm-12 card">
<div class="card-body pt-5">
<a href="/docs/2.2.1/docs.html" title="back to documentation">&lt;&lt; back to documentation</a>
<p><img src="http://178.62.170.124:3000/pithikos/c-thread-pool/badge/?branch=master" alt="Build status"></p>
<h1 id="c-thread-pool">C Thread Pool</h1>
<p>This is a minimal but fully functional threadpool implementation.</p>
<ul>
<li>ANCI C and POSIX compliant</li>
<li>Number of threads can be chosen on initialization</li>
<li>Minimal but powerful interface</li>
<li>Full documentation</li>
</ul>
<p>The threadpool is under MIT license. Notice that this project took a considerable amount of work and sacrifice of my free time and the reason I give it for free (even for commercial use) is so when you become rich and wealthy you don&rsquo;t forget about us open-source creatures of the night. Cheers!</p>
<h2 id="v2-changes">v2 Changes</h2>
<p>This is an updated and heavily refactored version of my original threadpool. The main things taken into consideration in this new version are:</p>
<ul>
<li>Synchronisation control from the user (pause/resume/wait)</li>
<li>Thorough testing for memory leaks and race conditions</li>
<li>Cleaner and more opaque API</li>
<li>Smart polling - polling interval changes on-the-fly</li>
</ul>
<h2 id="run-an-example">Run an example</h2>
<p>The library is not precompiled so you have to compile it with your project. The thread pool
uses POSIX threads so if you compile with gcc on Linux you have to use the flag <code>-pthread</code> like this:</p>
<pre><code>gcc example.c thpool.c -D THPOOL_DEBUG -pthread -o example
</code></pre>
<p>Then run the executable like this:</p>
<pre><code>./example
</code></pre>
<h2 id="basic-usage">Basic usage</h2>
<ol>
<li>Include the header in your source file: <code>#include &quot;thpool.h&quot;</code></li>
<li>Create a thread pool with number of threads you want: <code>threadpool thpool = thpool_init(4);</code></li>
<li>Add work to the pool: <code>thpool_add_work(thpool, (void*)function_p, (void*)arg_p);</code></li>
</ol>
<p>The workers(threads) will start their work automatically as fast as there is new work
in the pool. If you want to wait for all added work to be finished before continuing
you can use <code>thpool_wait(thpool);</code>. If you want to destroy the pool you can use
<code>thpool_destroy(thpool);</code>.</p>
<h2 id="api">API</h2>
<p>For a deeper look into the documentation check in the <a href="https://github.com/Pithikos/C-Thread-Pool/blob/master/thpool.h">thpool.h</a> file. Below is a fast practical overview.</p>
<table>
<thead>
<tr>
<th>Function example</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><em><strong>thpool_init(4)</strong></em></td>
<td>Will return a new threadpool with <code>4</code> threads.</td>
</tr>
<tr>
<td><em><strong>thpool_add_work(thpool, (void*)function_p, (void*)arg_p)</strong></em></td>
<td>Will add new work to the pool. Work is simply a function. You can pass a single argument to the function if you wish. If not, <code>NULL</code> should be passed.</td>
</tr>
<tr>
<td><em><strong>thpool_wait(thpool)</strong></em></td>
<td>Will wait for all jobs (both in queue and currently running) to finish.</td>
</tr>
<tr>
<td><em><strong>thpool_destroy(thpool)</strong></em></td>
<td>This will destroy the threadpool. If jobs are currently being executed, then it will wait for them to finish.</td>
</tr>
<tr>
<td><em><strong>thpool_pause(thpool)</strong></em></td>
<td>All threads in the threadpool will pause no matter if they are idle or executing work.</td>
</tr>
<tr>
<td><em><strong>thpool_resume(thpool)</strong></em></td>
<td>If the threadpool is paused, then all threads will resume from where they were.</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<footer class="py-3 bg-secondary">
<div class="container">
<div class="row">
<div class="col-md-8 text-center">
<p class="m-0 text-white">
Copyright &copy; 2023 The Apache Software Foundation, Licensed under
the <a href="https://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.
<br>
Apache Celix, Celix, Apache, the Apache feather logo and the Apache Celix logo are trademarks of The Apache Software Foundation.
</p>
</div>
<div class="col-md-4 text-center">
<a href="https://www.apache.org/events/current-event.html" target="_blank">
<img src="https://www.apache.org/events/current-event-234x60.png" title="Apache Event" width="234" height="60" border="0">
</a>
</div>
</div>
</div>
</footer>
<script src="/assets/js/jquery.min.js"></script>
<script src="/assets/js/bootstrap.bundle.min.js"></script>
</body>
</html>