[ACCEPTED]-Threading in C, cross platform-cross-platform
Try OpenMP API, it's multi-platform and you can 9 compile it with GCC.
Brief description from 8 the wikipedia:
OpenMP (Open Multi-Processing) is an 7 application programming interface (API) that 6 supports multi-platform shared memory multiprocessing programming 5 in C, C++, and Fortran, on most platforms, processor architectures 4 and operating systems, including Solaris, AIX, HP-UX, Linux, macOS, and 3 Windows. It consists of a set of compiler directives, library 2 routines, and environment variables that 1 influence run-time behavior.
I would use the POSIX thread API - pthread. This 11 article has some hints for implementing 10 it on Windows, and a header-file-only download 9 (BSD license):
Edit: I used the sourceforge 8 pthreads-win32 project in the past for multi-platform 7 threading and it worked really nicely. Things 6 have moved on since then and the above link 5 seems more up-to-date, though I haven't 4 tried it. This answer assumes of course 3 that pthreads are available on your non-Windows 2 targets (for Mac / Linux I should think 1 they are, probably even embedded)
Windows threading has sufficiently different 47 functionality when compared to that of Linux 46 such that perhaps you should consider two 45 different implementations, at least if application 44 performance could be an issue. On the other 43 hand, simply implementing multi-threading 42 may well make your app slower than it was 41 before. Lets assume that performance is 40 an issue and that multi-threading is the 39 best option.
With Windows threads I'm specifically 38 thinking of I/O Completion Ports (IOCPs) which 37 allow implementing I/O-event driven threads 36 that make the most efficient use of the 35 hardware.
Many "classic" applications are 34 constructed along one thread/one socket 33 (/one user or similar) concept where the 32 number of simultaneous sessions will be 31 limited by the scheduler's ability to handle 30 large numbers of threads (>1000). The IOCP 29 concept allows limiting the number of threads 28 to the number of cores in your system which 27 means that the scheduler will have very 26 little to do. The threads will only execute 25 when the IOCP releases them after an I/O 24 event has occurred. The thread services 23 the IOC, (typically) initiates a new I/O 22 and returns to wait at the IOCP for the 21 next completion. Before releasing a thread 20 the IOCP will also provide the context of 19 the completion such that the thread will 18 "know" what processing context the IOC belongs 17 to.
The IOCP concept completely does away 16 with polling which is a great resource waster 15 although "wait on multiple object" polling 14 is somewhat of an improvement. The last 13 time I looked Linux had nothing remotely 12 like IOCPs so a Linux multi-threaded application 11 would be constructed quite differently compared 10 to a Windows app with IOCPs.
In really efficient 9 IOCP apps there is a risk that so many IOs 8 (or rather Outputs) are queued to the IO 7 resource involved that the system runs out 6 of non-paged memory to store them. Conversely, in 5 really inefficient IOCP apps there is a 4 risk that so many Inputs are queued (waiting 3 to be serviced) that the non-paged memory 2 is exhausted when trying to temporarily 1 buffer them.
If someone needs a portable and lightweight 12 solution for threading in C, take a look 11 at the plibsys library. It provides you thread 10 management and synchronization, as well 9 as other useful features like portable socket 8 implementation. All major operating systems 7 (Windows, Linux, OS X) are supported, various 6 other less popular operating systems are 5 also supported (i.e. AIX, HP-UX, Solaris, QNX, IRIX, etc). On 4 every platform only the native calls are 3 used to minimize the overheads. The library 2 is fully covered with Unit tests which are 1 run on a regular basis.
glib threads can be compiled cross-platforms.
The "best"/"simplest"/... answer here is 4 definitely pthreads. It's the native threading 3 architecture on Unix/POSIX systems and works 2 almost as good on Windows. No need to look 1 any further.
Given that you are constrained with C. I 11 have two suggestions:
1) I have a seen a 10 project (similar to yours) that had to run 9 on Windows and Linux with threads. The way 8 it was written was that it (the same codebase) used 7 pthreads on Linux and win32 threads on Windows. This 6 was achieved by a conditional #ifdef statement 5 wherever threads needed to be created such 4 as
#ifdef WIN32 //use win32 threads #else //use pthreads #endif
2) The second suggestion might be to use 3 OpenMP. Have you considered OpenMP at all?
Please 2 let me know if I missed something or if 1 you want more details. I am happy to help.
From my experience, multi threading in C 10 for windows is heavily tied to Win32 APIs. Other 9 languages like C# and JAVA supported by 8 a framework also tie into these core libraries 7 while offering their thread classes.
However, I 6 did find an openthreads API platform on 5 sourceforge which might help you:
The API 4 is modeled with respect to the Java and 3 POSIX thread standard,
I have not tried this 2 myself as I currently do not have a need 1 to support multiple platforms on my C/C++ projects.
More Related questions