Threading in C, cross platform

后端 未结 8 835
情话喂你
情话喂你 2021-02-05 14:40

I am dealing with an existing project (in C) that is currently running on a single thread, and we would like to run on multiple platforms AND have multiple threads. Hopefully, t

相关标签:
8条回答
  • 2021-02-05 15:06

    glib threads can be compiled cross-platforms.

    0 讨论(0)
  • 2021-02-05 15:06

    From my experience, multi threading in C for windows is heavily tied to Win32 APIs. Other languages like C# and JAVA supported by a framework also tie into these core libraries while offering their thread classes.

    However, I did find an openthreads API platform on sourceforge which might help you:

    http://openthreads.sourceforge.net/

    The API is modeled with respect to the Java and POSIX thread standard,

    I have not tried this myself as I currently do not have a need to support multiple platforms on my C/C++ projects.

    0 讨论(0)
  • 2021-02-05 15:08

    If someone needs a portable and lightweight solution for threading in C, take a look at the plibsys library. It provides you thread management and synchronization, as well as other useful features like portable socket implementation. All major operating systems (Windows, Linux, OS X) are supported, various other less popular operating systems are also supported (i.e. AIX, HP-UX, Solaris, QNX, IRIX, etc). On every platform only the native calls are used to minimize the overheads. The library is fully covered with Unit tests which are run on a regular basis.

    0 讨论(0)
  • 2021-02-05 15:20

    Try OpenMP API, it's multi-platform and you can compile it with GCC.

    Brief description from the wikipedia:

    OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared memory multiprocessing programming in C, C++, and Fortran,[3] on most platforms, processor architectures and operating systems, including Solaris, AIX, HP-UX, Linux, macOS, and Windows. It consists of a set of compiler directives, library routines, and environment variables that influence run-time behavior.

    0 讨论(0)
  • 2021-02-05 15:22

    I would use the POSIX thread API - pthread. This article has some hints for implementing it on Windows, and a header-file-only download (BSD license):

    http://locklessinc.com/articles/pthreads_on_windows/

    Edit: I used the sourceforge pthreads-win32 project in the past for multi-platform threading and it worked really nicely. Things have moved on since then and the above link seems more up-to-date, though I haven't tried it. This answer assumes of course that pthreads are available on your non-Windows targets (for Mac / Linux I should think they are, probably even embedded)

    0 讨论(0)
  • 2021-02-05 15:31

    The "best"/"simplest"/... answer here is definitely pthreads. It's the native threading architecture on Unix/POSIX systems and works almost as good on Windows. No need to look any further.

    0 讨论(0)
提交回复
热议问题