drivel
drivel

Reputation: 53

Use threading and sockets in C in a platform independent way

I need to write a programm which uses threads and sockets. What is the best way to do it for different platforms (Linux, Windows, Mac).

I know about POSIX, but there is no POSIX on Win.

Are there any libraries handling that in a platform independent way?

Upvotes: 5

Views: 2348

Answers (6)

Jens Gustedt
Jens Gustedt

Reputation: 78903

The new C standard, C11, comes with thread support. AFAIK there are no C11 out there, yet, but there are implementations (wrappers) of the threading part. On POSIX systems you'd could use my wrapper in P99 on top on pthread, this should give you Linux and Mac. On Windows systems there is at least the reference implementation from Dinkumware that gave rise to API as defined in the standard.

Using C11 would give you a "future proof" unified thread API.

For sockets, my impression is that the BSD socket interfaces are provided relatively directly in windows, too, but I don't have experience in programming on that platform.

Upvotes: 1

vond
vond

Reputation: 1948

If you really want C (not C++), I suggest to use the standard POSIX threads on non-Windows platforms, and use pthreads-win32 on Windows. It supports both 32- and 64-bit, both MSVC and MinGW. It's current version (2.9.1) was released just one month ago, so the project is actively maintained. There's also a fork on github with some fixes in MSVC2010 project.

If C++ is also an option, I'd choose boost, because it's where the standard c++ evolves (the design of std::thread in c++11 is an evolution from boost::thread, etc.)

For the network part of your question, boost::asio is the best choice if C++ is OK for you, otherwise didn't see anything comparable in C. In particular, boost::asio supports I/O Completion Ports (IOCP) on Windows, which is critical for performance. boost::asio requires some time to learn, but in my personal opinion it worth every minute spent reading the documentation (which is great) and working with examples.

Upvotes: 5

Specksynder
Specksynder

Reputation: 843

For Windows, you can use Win32's CreateThread API: http://msdn.microsoft.com/en-us/library/kdzttdcb(v=vs.71).aspx

Upvotes: -1

Stefan Steiger
Stefan Steiger

Reputation: 82136

There is a C++ class called XP threads on codeproject. http://www.codeproject.com/Articles/15983/Cross-Platform-Threads
You need to change the ifdef from mach to ifndef win32/64

I used it for my OpenArena aimbot, the tutorial includes the modified XP-Threads class: http://aimbots.net/tutorials/19992-audio-killspam.html

All you need to do is to de-"classify" it for usage in C.

You can see the end-result here, btw: :)
http://www.youtube.com/watch?v=nyJ8aM52ykE

Upvotes: 0

Eugene
Eugene

Reputation: 2908

You can use boost (C++): asio - for asinchronous input output, threads - for platform independent threading

Upvotes: 1

Shahin Dohan
Shahin Dohan

Reputation: 6907

You can try using Qt (C++) as a wrapper, it has cross-platform libraries for threading and networking; unless your project has to be 100% C.

Upvotes: 1

Related Questions