<p></p>
<p dir="auto"><a class="user-mention" data-hovercard-type="user" data-hovercard-url="/users/avafinger/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/avafinger">@avafinger</a> also just to comment on a couple of things you said above which may indicate a misunderstanding of threads.</p>
<blockquote>
<p dir="auto">Threads are not predictable, but in the sample, Thread[0] will always finish first, because it is fired first, in theory.</p>
</blockquote>
<p dir="auto">No, theory says exactly the opposite, nothing guarantees that, it can be scheduled to run or not at the OS convenience, that depends on what the heaps of other threads running in applications like the desktop and other apps want to do at the same time, no current CPU has enough cores to cope, even hyperthreaded.  So it totally depends on scheduling if a newly created thread gets to run right away, or not.</p>
<blockquote>
<p dir="auto">Maybe printf is not the best thing to use in the example.</p>
</blockquote>
<p dir="auto">IIUC in practice<sup><a href="#user-content-fn-1-c92ea0c8389a27e05bde0359825a0200" id="user-content-fnref-1-c92ea0c8389a27e05bde0359825a0200" data-footnote-ref="" aria-describedby="footnote-label">1</a></sup> printf is fine for threads, each printf will be atomic, so individual characters won't mix, and therefore the order of output shows the order of execution, useful for debugging.</p>
<p dir="auto">Therefore your two runs output are both perfectly reasonable, remember the main is also a thread and subject to scheduling, so looking at the first few lines, in run 1 main returned from pthread_create and ran its "successful" printf before thread[0] got to its first printf, but in the second case thread[0] got to run two printfs before the main thread got to run its "successful" printf. See comment above about scheduling.</p>
<section data-footnotes="" class="footnotes"><h2 id="footnote-label" class="sr-only">Footnotes</h2>
<ol dir="auto">
<li id="user-content-fn-1-c92ea0c8389a27e05bde0359825a0200">
<p dir="auto">POSIX requires printf to take a lock on the file, but some argue it doesn't explicitly require the lock to be held while all characters are output, but in practice most implementations do. <a href="#user-content-fnref-1-c92ea0c8389a27e05bde0359825a0200" data-footnote-backref="" class="data-footnote-backref" aria-label="Back to content"><g-emoji class="g-emoji" alias="leftwards_arrow_with_hook" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/21a9.png">↩</g-emoji></a></p>
</li>
</ol>
</section>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/geany/geany-plugins/issues/1069#issuecomment-1088145277">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAIOWJ4NRZWJNCATKQ6KLW3VDOBH7ANCNFSM4YQPZN7A">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAIOWJ7QDXVLCQCYUF3HF5TVDOBH7A5CNFSM4YQPZN7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOIDN4O7I.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><geany/geany-plugins/issues/1069/1088145277</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/geany/geany-plugins/issues/1069#issuecomment-1088145277",
"url": "https://github.com/geany/geany-plugins/issues/1069#issuecomment-1088145277",
"name": "View Issue"
},
"description": "View this Issue on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>