Thread: Multithreading with pthreads on Windows

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137

    Question Multithreading with pthreads on Windows

    So I've already tested some pthreads on this Windows 10 computer and they do function. Upon a little further research, I discovered that apparently MinGW which is what I am using, has some sort of wrapper that maps the pthreads API to whatever it is Windows uses on a Windows system. However, information on this seems pretty scarce and I'm not sure what's really going on under the hood, plus POSIX is supposed to be a *NIX standard rather than Windows. Are pthreads really safe to use on Windows?

    Context: Please note that I am not doing Win32 programming in Visual Studio. I am using MinGW & GCC and writing code with Emacs and compiling GCC at the command-line. I'm trying to stay in this environment and avoid Visual Studio if at all possible and I am aware that there is a "Windows API" way of doing this with all of the MS stuff using Visual C++ and all that which I am trying to avoid.

    So, is this the best I'm going to get outside of using the Win API stuff? Also, does anyone happen to know how to call the Win API into a program without using Visual Studio? Can I just #include <windows.h> or whatever in a plain GCC program? Or do I have to use Windows' cl.exe compiler or whatever? Thanks so much.

    BTW: A similar topic is here . However, this user seems to be programming in C++ and the solution is not the same.
    Last edited by Asymptotic; 12-09-2016 at 10:04 PM.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    For Windows API, you would need to link with some of the Windows based libraries, some of which can be statically linked or dynamically linked. I don't know if GCC can do this. As for pthreads, eventually it will end up as some Windows thread.

    One thing I would miss in *NIX systems is Windows WaitForMultipleObjects(), a feature that most of the multi-threading / multi-processing systems have had going back to the 1980's (back in the days of mini-computers) and prior to that on mainframes.

    WaitForMultipleObjects function (Windows)

  3. #3
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Interesting function.

    Oh I see what you're saying, so basically the Win API is just some DLL files that I would have to feed into the linker and then #include in my sources, possibly... Hm... Yeah win32, kernel32, and user32 ring a bell from my Visual Studio days..

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Asymptotic View Post
    Oh I see what you're saying, so basically the Win API is just some DLL files that I would have to feed into the linker and then #include in my sources, possibly... Hm... Yeah win32, kernel32, and user32 ring a bell from my Visual Studio days..
    I'm thinking also one of the libc...libs ... .

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Try compiling the following in the usual way. It will probably work without adding any extra libraries since mingw knows all about windows. Less common libraries may need to be added. If you're ever missing a library, just look up the WinAPI functions you're using on MSDN and see what library is required.
    Code:
    #include <windows.h>
    int main() {
        MessageBox(NULL, "It works!", "Test", MB_OK | MB_ICONEXCLAMATION);
        return 0;
    }
    As for pthreads eventually calling the Windows API, there's simply no other way to get a thread from an operating system than to ask it nicely. The OS owns the resources of the computer. Your process is just borrowing them.

  6. #6
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    algorism!! You're right! Genius lol! Sometimes I overthink things. Sweet, now I do native Windows programming without C#. :P

    I just tried a CreateThread handle as well which worked!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading Server Loop in Linux and Windows
    By gemera in forum C Programming
    Replies: 8
    Last Post: 10-16-2012, 10:02 AM
  2. Multithreading in plain C for Windows
    By ulillillia in forum C Programming
    Replies: 4
    Last Post: 09-09-2012, 02:21 AM
  3. Multithreading with pthreads -- no performance increase
    By lilrayray in forum C Programming
    Replies: 11
    Last Post: 02-07-2010, 06:27 AM
  4. pthreads
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 10-05-2005, 11:41 PM
  5. Multithreading with the Windows API
    By Xzyx987X in forum Windows Programming
    Replies: 7
    Last Post: 11-07-2003, 12:22 AM

Tags for this Thread