Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading in C, cross platform

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, there is a library for this, because, IMHO, the Win32 API is like poking yourself in the eye repeatedly. I know about Boost.Thread for C++, but, this must be C (and compilable on MinGW and gcc). Cygwin is not an option, sorry.

like image 875
Dhaivat Pandya Avatar asked Apr 10 '11 17:04

Dhaivat Pandya


People also ask

Does C allow multi threading?

C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.

Can threads be executed in parallel?

On a system with more than one processor or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel.

Is std :: thread cross platform?

Standard C++ 11's threading is based on boost::thread, now it's cross platform and no dependency needed. The code is simple, the thread function will run right after the std::thread is declared.

Is C single threaded?

C is a language that runs on one thread by default, which means that the code will only run one instruction at a time.


1 Answers

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.

like image 154
Cacho Santa Avatar answered Sep 28 '22 07:09

Cacho Santa