Existing threadpool C implementation [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
What open-source implementation(s) in C for a pthreads thread pool would you recommend ?
Additional points if this implementation is :
Light-weight: glib, APR, NSPR and others come with a big buy-in, I'd rather have just 2 files (header and implementation).
Tested on several platforms (Linux, BSD, Mac OS X, etc.).
Still maintained.

I worked on making something I'd be able to use and I've published it on github: it's unimaginably called threadpool.

If your goal is light-weight, then the last thing you want is a prewritten, super-general-purpose, high-level-abstraction-based implementation. Implementing a thread pool yourself, suited to your particular task, is fairly trivial, but you might also question whether you actually need a thread pool or whether you'd be fine just creating and destroying threads as needed.
Without knowing more details about your application, I can't give much more specific advice. But the tools you might find useful are:
Condition variables
Semaphores
A job queue protected by a mutex
POSIX message queues

Here is an implementation with these features:
ANSI C and POSIX compliant
Minimal but powerful API
Synchronisation from the user
Full documentation

I once used this, which isn't actually an official implementation per se. It does use pthreads as you requested, and should give you some ideas of what you need to do. (See threadpool.h, threadpool.c, threadpool_test.c, and the Makefile for instructions on how to compile.) You'll obviously have to do some refactoring as it's original intention is probably different than yours. It's commented rather well actually.
Even though this deviates from the original question, I'd also like to mention that the newest C standard, unofficially C1X (see wikipedia, hyperlink limit), has planned support for threads N1570 (google it, hyperlink limit again!) (7.31.15).
Some personal advice from my experience would be to make sure that your application can actually be run in parallel, and if the overhead of creating a new thread is so high that you can't live without a thread pool. Personally I've blundered on both these parts and I've actually ended up with implementations slower than my single threaded application. Also, you might want to be aware of different problems, including cache-lockouts and misses, which would actually degrade the performance of your application.
I'm probably blabbering on by now, but best of luck.

Related

Windows programmer moving to linux - Coding conventions [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been developing for Windows for a long time, mainly WinApi (and .Net).
I'v started learning basic Linux, and I have some questions regarding the differences:
In Windows I have barely used the C Standard library.
If I needed an API, I would search MSDN and find the appropriate library\function.
From what it seems like, in Linux the C Standard library is EVERYTHING.
All the code samples I have seen used the standard library (Instead of using some Linux internal functions, like a Linux "CreateFile").
Is this really how writing "proper" linux code is done ? Using the C standard library ?
If I wish to read a file, or allocate memory are fopen\malloc the way to go ?
If the answer to my first question is yes (And I guess it will be)
The C standard library is POWERLESS compared to the powerful WinApi.
Lets say I wish to get a list of running process (CreateToolhelp32Snapshot) or create a thread or a process (CreateThread\CreateProcess), How should I do that in Linux ?
Documentation.
In Windows, all I need can be found in MSDN.
If I have a "how do I do" question (Like the questions above) where should I go ?
Where is my main source of documentation.
Thanks a lot,
Michael.
Perhaps you've forgotten that the Standard C Library isn't environment-specific, it specifies least-common-denominator functionality among all systems that can run C programs, and C runs on systems that don't even have processes.
If you want an API that provides consistent common GUI/multithread/etc. APIs, pick a likely-looking GUI/multithread/etc. API. You might start with Qt, it's quite comprehensive and produces good-looking, near-native UIs on a host of systems.
It's not generally considered polite to point this out, but most questions that get asked publicly are asked by people who lack the discipline to do even simple research. Once people can do that, they don't need to ask very many, and that's why what you see is so ... trivial. You're past that. For more options, you could start here.
For more general-purpose tools, the top hit on a search for important linux tools might be helpful.

Quantifiable differences between RTOS kernels for small ARM microcontrollers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
There are many different RTOS available for microcontrollers. I am specifically looking for RTOS that support the ARM Cortex M processors. Also, I am not interested in closed source solutions.
Attempting to compare the relative merits of each RTOS from websites and mailing lists seems pretty difficult as they mostly seem to have equivalent features and do the same thing. The real differences become apparently only after trying to use each RTOS for some tasks.
I know this is somewhat subjective question and probably hard to answer - but there must be many people out there who have actually tried several different RTOS and formed an opinion of the relative merits of each one.
I am specifically interested in FreeRTOS, ChibiOS and Coocox CoOS, but other choices are also very welcome.
For example: it would seem that in ChibiOS, ISRs can call any system functions, but those calls must be wrapped in chSysLockFromIsr()/chSysUnlockFromIsr() and the code is not preemptable during those sections. In CoOS, the only functions callable are the ones starting with isr_ such as isr_PostSem(), isr_PostMail(), isr_PostQueueMail() and
isr_SetFlag(), but those functions internally use a service request queue which means most of the request is preemptable.
Some of the features that one could take into account while choosing the RTOS:
context-switch time
interrupt latency
synchronization mechanisms (flags, semaphores, mutexes, mailboxes, queues, ...)
priority inversion handling
memory management support (i.e. memory pools)
scheduling policies support
MMU support
process support
memory footprint
efficiency of the kernel itself
POSIX vs. non-POSIX API's
software eco-system available (a.k.a middleware)
...
Which point(s) to put more focus, depends on the very application you're going to run. But generally, these are the things I can remember of which make difference between various RTOS's.

Modern Ada to C/C++ translator [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Is there any source-to-source converter (translator) from Ada (95, 2005) to C?
How full they are (can they convert every feature of Ada into gnu c99 + pthreads + POSIX)?
Is it possible to use such ada-to-c translator in critical applications?
PS: Translators to C++ (up to 2003 with gnu extensions) are welcome too.
PPS: when said "gnu c99", it means only that C99 + most gnu extensions are supported, but don't mean the GCC.
I don't know of any open source Ada-to-C translator. The only one I knew of at all was SofCheck's, which was reportedly pretty good.
SofCheck has since been bought by AdaCore, and I did a very brief search of the AdaCore website for the translator, and nothing jumped out. You could ask them at sales#adacore.com, if pursuing a commercial solution is a viable option for you. (At least get a price.)
Unless there is an incredibly strong reason to use Ada for this application (e.g., customer demands it, or you already have a big application coded in Ada that you want to use), it will likely be a lot less painful if you just bite the bullet and code your solution in well-crafted C99 or C++ as you see fit.
If you insist, Sofcheck's translator might be best; they've been working on it a long time.
Failing that, you might(?) build a translator starting with the ASIS output of an Ada compiler. That's likely rather a lot of persnickety work since Ada has pretty precise semantics that you'd better preserve if you want to just carelessly code in Ada, translate and run. It will be even more work if you want the output to be "pretty" for the final customer. (Long term maintenance should be a consideration). I suspect implementing code to simulate Ada's rendezvous might be rather tricky, being both semantically complicated and asynchronous at the same time. The real flaw with this approach is that it is a lot of work; maybe just getting on with your life and coding the application itself in something non-Ada would be less effort.
See my caveats on language translation done poorly and alternative methods.

Simple/canonical implementations of synchronization primitives? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm looking for a good online reference on typical implementation of synchronization primitives (spinlocks, mutexes, semaphores, read-write locks, conditional variables, ...) either in abstract c+atomics or pseudo-asm (i.e. any reasonable notation of the sequence of atomic operations performed) or x86 asm. Something that starts with the most naive implementations and then addresses their shortcomings and some of the approaches to solving the shortcomings would be great.
Try Tanenbaum's Operating Systems: Design and Implementation.
edit: or Modern Operating Systems. I think the 1st one includes Minix, the 2nd one doesn't. Not sure, sorry =(
It's academically oriented, so it'll get you started on the right path.
Not a complete reference by any means, but the following paper is a classic and essential for understanding the implementation of synchronisation primitives in Linux:
"Fuss, Futexes and Furwocks: Fast Userlevel locking in Linux", Franke Russell & Kirkwod, Proceedings of the Ottawa Linux Symposium 2002 - available (among others) at: http://www.cis.temple.edu/~ingargio/cis307/readings/futex0.pdf
From that and the glibc sources it's possible to learn a lot, but it's not what I'd call easy-going :-)
As a side note, if you just want atomic stuff and you'r using gcc you have some built in functions that you can use instead of asm.
http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html
But for specific locks you always have Wikipedia
http://en.wikipedia.org/wiki/Spinlock
http://en.wikipedia.org/wiki/Semaphore_(programming)
Also worth looking at is
http://en.wikipedia.org/wiki/Lock-free_and_wait-free_algorithms

Boost like libraries in C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Can you recommend peer reviewed libraries that I can use in C environment (something like Boost for C++) ? Something that provides hash, thread, interprocess communications, lists, smart memory management...
The environment is embedded system, not a very minimal system, but also not a PC!
+1 for GLib from me, too. Plus, it has its own threading API too, so you don't have to learn pthreads if you don't want to.
Not sure if there exists such a thing as "smart memory management" in C, it's not very easy when you don't have the safety nets of destructors and control over operators. But, again, GLib has plenty of memory-oriented data structures and stuff that really makes life easier.
And no, I'm not on the GLib team, but I really do like it. :)
Check out the Apache Portable Runtime (APR) project.
Some of it's features:
memory management API
threads, mutexes
file I/O
atomic operations
hash tables, arrays
network sockets and protocol
shared memory, mmap
Not to mention it's portable.
I'm not sure if you'll find a single library that covers all of that... but you can check out glib and pthreads to cover a good bit of that.
Look at Boehm GC a widely used conservative garbage collector for C (or C++) that might serve your needs as far as smart memory management is concerned.
I'll jump on the GLib bandwagon too. Remember that C doesn't provide any syntactic sugar for complex data structures, so there are lots of casts and long function names in GLib, but it really does a great and efficient job with a little added verbosity!!
About the Glib use.
You probably can take what you need and cross-compile it. So if you just need the thread package - just compile that and don't take everything.
I'm doing the same thing with the Python VM. PyMite fits on a microcontroller and doesn't use all the functionality.

Resources