Path: csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jens Gustedt Newsgroups: comp.lang.c,comp.programming.threads Subject: Re: Trivial C11 threads.h wrapper (public domain) Date: Fri, 28 Sep 2012 20:22:53 +0200 Organization: A noiseless patient Spider Lines: 60 Message-ID: <5065EAFD.1050102@loria.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit Injection-Info: mx04.eternal-september.org; posting-host="2590e4fa64e7931a8f955de6703c052f"; logging-data="15097"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jnF/FF/P7b4jTox0qH33hSmr6eApS4qE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120827 Thunderbird/15.0 In-Reply-To: Cancel-Lock: sha1:u9MDrql/9YLlpTmhyh5mVCaTkS4= Xref: csiph.com comp.lang.c:26770 comp.programming.threads:1113 Hello, Am 27.09.2012 05:38, schrieb John Tsiombikas: > Hello everyone! It's been a long time since I was last on usenet :) > > I was anxious to use the new standard C11 threading features, instead of > platform-specific APIs, but unfortunately my system libc (GNU libc) does > not implement that bit that yet. > So, I wrote a trivial C11 thread wrapper over POSIX threads, and > released it in the public domain just in case anyone is itching to use > standard C threading in a new project just like me: > https://github.com/jtsiomb/c11threads There is a major flaw in your implementation that concerns the type of the thread functions. You are simply casting C11 function type int f(void*) to the POSIX type void* f(void*) That is not only undefined behavior what is concerned C, but also dangerous. The calling conventions for these type of functions may be different on a given architecture, e.g returning the int value in a reserved register and the void* on the stack. Taking care of that incompatibility between C11 an POSIX threads needs a bit more care than that, I think, if you want to be portable. > The wrapper is so thin, I didn't think it made sense to make a proper > library out of it, so it's just a header file with "static inline" > functions. Drop it in your project, link with pthread and you're good to > go. > > Disclaimer: I used a draft of the C11 standard while writting this code, > since I don't actually have the final document. Feel free to notify me > of any glaring discrepancies or omissions. The xtime things are gone in the final version and all is now done with the time structures as they existed before. There is already an implementation of C11 threads as a wrapper around POSIX threads that is publicly available. It is integrated in P99: http://p99.gforge.inria.fr/ The C11 compatibility wrapper of P99 should be completely interface compatible to C11 threads. (the include files are named differently, though.) Basically it also is a shallow wrapper using inline functions around POSIX, but in addition P99 also offers - an implementation/wrapper for the atomics - of thread local variables - _Generic (emulated through a macro) if they are available (generally through gcc and friends) Jens