Slashdot Mirror


Pthreads vs Win32 threads

An anonymous reader writes "It's interesting when different people have different opinions. While I was searching for something on Intel's website, I came across an article on why Windows threads are better than Posix threads. Curiously, I also came across this article on why Posix Pthreads are better that Win32 threads. The thing is, both of these articles are written by the same author!

So who is right (metaphorically speaking?), or what has changed since the first article was written?"

33 of 385 comments (clear)

  1. quothe the poster by TinBromide · · Score: 5, Insightful

    "or what has changed since the first article was written?"

    Vista's release and a massive advertising campaign/increase in revinue for microsoft partners?

    --
    Is it sad that I am more likely to recognize you and your posts by your sig than your name or UID?
    1. Re:quothe the poster by should_be_linear · · Score: 5, Funny

      I have different theory: Dude has 2 separate threads in his brain. Comparing pthreads to Win32 threads only exposed dangerous race condition producing funny effects in his blog.

      --
      839*929
    2. Re:quothe the poster by Hal_Porter · · Score: 5, Funny

      Q)Why did the multithreaded chicken cross the road?
      A)to To other side. get the

      Q)Why did the multithreaded chicken cross the road?
      A)other to side. To the get

      --
      echo -e 'global _start\n _start:\n mov eax, 2\n int 80h\n jmp _start' > a.asm; nasm a.asm -f elf; ld a.o -o a;
    3. Re:quothe the poster by Korin43 · · Score: 4, Funny

      What are you talking about? Plenty of people code for Windows, we call them "virus writers". ;)

    4. Re:quothe the poster by Courageous · · Score: 4, Interesting

      Nobody program for Windows because he likes to program for Windows...

      To the contrary. I work on Linux systems at work, and play on Windows, using .NET (C#) at home, for fun. .NET is a genuine pleasure to work with: a better java than java, I say. It may be an asset of the evil empire, and it may not be multiplatform, but I find those to be its only weaknesses.

      C//

    5. Re:quothe the poster by PitaBred · · Score: 5, Informative
      Did you notice the other similarities?

      Initial (2003) article:

      I've used both POSIX threads (Pthreads) and Win32 threads APIs and I believe that Pthreads has the better programming model of the two. While each threading method can create threads, destroy threads, and coordinate interactions between threads, the reason I make this claim is the simplicity of use and elegance of design of Pthreads. Let me illustrate with a few examples.
      And in the later article:

      I've used both POSIX threads (Pthreads) and Windows threads APIs, and I believe that Windows has the better programming model of the two. While each threading method can create threads, destroy threads, and coordinate interactions between threads, the reason I make this claim is the simplicity of use and elegance of design of the Windows threads API. This is all from the perspective of multithreaded code developers or maintainers. Let me illustrate with a few examples.
      It's not plagiarism because he copied from himself and just added a few edits, but c'mon... how lazy of a shill can you be?
    6. Re:quothe the poster by mustafap · · Score: 4, Funny

      >Get it right, assmuch. It's Linsucks or Linsux.

      It's GNU/Linsux you bastards! When are you going to get it right! Arghh!

      --
      Open Source Drum Kit, LPLC deve board - mjhdesigns.com
    7. Re:quothe the poster by 0xABADC0DA · · Score: 5, Funny

      Q) Which came first, the multithreaded chicken or the multithreaded egg?
      A) They came at the same time, but the multithreaded chicken terminated first.

      Q) Which came first, the multithreaded chicken or the multithreaded egg?
      A) Neither; mt egg could not acquire chicken-lock from mt chicken. mt chicken could not acquire egg-lock from mt egg.

      Q) Which came first, the multithreaded chicken or the multithreaded egg?
      A) Multithreaded egg, but it overwrote its DNA while still in use and became mt turkey.

  2. $Money$ by ArcherB · · Score: 4, Insightful

    So who is right (metaphorically speaking?), or what has changed since the first article was written?"

    Who was paying for it.

    --
    There is no "I disagree" mod for a reason. Flamebait, Troll, and Overrated are not substitutes.
  3. Pthreads = Win32 threads? by arlo5724 · · Score: 5, Funny

    ( Pthreads >= Win32 threads ) and ( Win32 threads >= Pthreads ) => Pthreads = Win32 threads

    1. Re:Pthreads = Win32 threads? by anci_pants · · Score: 5, Funny

      I think you mean Pthreads == Win32 threads.

      Although, it may be true that Win32 threads = Pthreads (which may account for the improvement)

  4. From the obvious dept by hey · · Score: 5, Funny

    If you are programming on Widows I would recommend Windows threads, while on *nix Pthreads are a better choice.

    1. Re:From the obvious dept by Hal_Porter · · Score: 5, Funny

      Windows threads work on BOTH platforms, Windows XP/Vista and Windows CE.

      --
      echo -e 'global _start\n _start:\n mov eax, 2\n int 80h\n jmp _start' > a.asm; nasm a.asm -f elf; ld a.o -o a;
  5. Re:Look at the dates, Dude. by Anonymous Coward · · Score: 5, Informative

    Except that the 2006 entry is essentially a structural copy of the 2003 entry, just with some wording changes. He even uses the same arguments against PThreads in the second entry that he used to support it in the first. Weird...

  6. Re:Look at the dates, Dude. by AKAImBatman · · Score: 5, Interesting

    I have a feeling that as the author worked more and more with the different threading models, he seems to have a more matured opinion.

    Normally I'd agree with you. But if you read both articles, you realize that his intentions may be a bit less honorable. The Win32 article is the exact same article, but with the conclusions changed. Maturing opinions usually result in some discussion of why one has changed their mind (even if it's only mentioned in passing) and/or a deep explanation of what caused their mind to change. This smells more like an attempt to play both sides.

    Of course, it could be that the author is simply not comfortable with writing. He could be looking to make a quick buck by taking a simple forum post of his and modifying it to reflect his current opinion. It's worth giving him the benefit of the doubt on, but I am certainly suspicious.
  7. PThreads is better by swm · · Score: 4, Informative

    The articles read like one of those English assignments where you have to pick an issue and then write two essays, one supporting each side. Probably the author wrote them to generate traffic to his websites, or maybe for freelance fees.

    Anyway, PThreads is better. The reason is that Win32 gives you a fixed set of synchronization primitives. If you can solve your problem with those primitives. they work great. If you can't, you are completely stuck.

    For example, it used to be that a socket handle was not a synchronization object, so you couldn't integrate select() calls with other synchronization primitives. Maybe that's been fixed, but if it isn't sockets, it will be something else.

    PThreads gives you condition variables. They are harder to program, but once you understand them, you can use them to synchronize on absolutely anything. You aren't dependent on the OS to have foreseen your special needs and provided special synchronization primitives to meet them.

    If you really want the Win32 model, it is easy enough to build it on top of PThreads, but there is no way to build PThreads on top of Win32.

    The complaint about lost signals in PThreads means that the author is using them incorrectly.

    1. Re:PThreads is better by leuk_he · · Score: 4, Informative

      there is a pthreads implementation build on top if win32. I am not saying it is efficient, but it does the job.

    2. Re:PThreads is better by PhrostyMcByte · · Score: 5, Informative

      You're in luck-

      Vista comes with APIs for condition variables and reader-writer locks so you don't have to spend 15 minutes writing your own.

    3. Re:PThreads is better by dmayle · · Score: 4, Interesting

      It's not so cut and dry. I use a library I wrote to abstract away PThreads and Win32 Threads to give me a multi-platform interface, but I would say that each side has their upsides and downsides.

      With pthreads, there's no way to combine socket i/o and thread notification. In Win32, I can have a thread waiting on both a socket and an event at the same time, allowing me to cleanly signal I/O blocked threads. This isn't the case with pthreads.

      However, Win32 REALLY needs a way to handle negative mutexes (ie. taking more than one resource from a mutex at a time). I had to write my own, and it was a serious pain to make sure I didn't have any race conditions. All the articles I've seen on Win32 equivalents had race conditions, and that means that there means that there are probably developers trusting multi-threading primitives that they really shouldn't be./p>

    4. Re:PThreads is better by MythoBeast · · Score: 4, Insightful

      Vista comes with APIs for condition variables and reader-writer locks so you don't have to spend 15 minutes writing your own.

      Well, fifteen minutes writing, plus ten years of programming experience to be sure that you aren't going to create a deadlock in some obscure circumstance.

      --
      Wake up - the future is arriving faster than you think.
    5. Re:PThreads is better by Twylite · · Score: 4, Interesting

      You are talking mostly bollocks, probably because you don't program using the Windows API

      Anyway, PThreads is better. The reason is that Win32 gives you a fixed set of synchronization primitives. If you can solve your problem with those primitives. they work great. If you can't, you are completely stuck.

      And PThreads also gives you a fixed set of primitives. In some respects they are more powerful, in others they are weaker. This is a bullshit strawman.

      For example, it used to be that a socket handle was not a synchronization object, so you couldn't integrate select() calls with other synchronization primitives.

      WTF?

      1. select() has nothing to do with synchronization. Although it does have something to do with scheduling.
      2. select() can't wait on "synchronization primitives" under *nix. Really. Try man select . It works with FDs, only.
      3. select() is not a core function of the Windows API. It is part of Winsock, which uses lower level scheduling primitives.
      4. Finally, if you want to do asynchronous development with Windows you use Async IO, not select. Async IO is built on top of Events, which are synchronization primitives.

      That brings me to the next point: in Unix there is no wait to wait on multiple synchronization primitives (as opposed to FDs) at once, and only a few primitives support a timed wait. In Windows you have WaitForMultipleObjectsEx. Look it up.

      Let me be clear on the difference: in Windows the scheduler API for waiting on IO and synchronization primitives is fully integrated - you can wait on any combination of IO and sync objects in the same Wait call. You must of course be using Windows Async IO which uses Events rather than FDs as the waitable object. Under *nix you can wait for multiple condition variables, or multiple file descriptors, or a single mutex / thread / process. There is no way to link an FD to a condition variable.

      PThreads gives you condition variables. They are harder to program, but once you understand them, you can use them to synchronize on absolutely anything. You aren't dependent on the OS to have foreseen your special needs and provided special synchronization primitives to meet them.

      In Windows you have Mutexes, Events and Semaphores. Events alone are sufficient to provide almost identical functionality to condition variables. You may have missed that memo.

      I say "almost identical" because the underlying scheduler behaviour is slightly different, which makes it very difficult to perfectly emulate Pthreads on Win32. Read Strategies for Implementing POSIX Condition Variables on Win32.

      If you really want the Win32 model, it is easy enough to build it on top of PThreads, but there is no way to build PThreads on top of Win32.

      Cough. Bullshit. Cough. Read Porting of Win32 API WaitFor to Solaris Platform to get a clue. It is possible to build Pthreads on top of Win32, and vice versa. Neither emulation is particularly efficient though.

      The complaint about lost signals in PThreads means that the author is using them incorrectly.

      The complaint about weakness in the Win32 scheduling API means that the author is using it incorrectly.

      --
      i-name =twylite [http://public.xdi.org/=twylite], see idcommons.net
  8. Re:Yes, the real world is a complicated place. by Ctrl-Z · · Score: 5, Insightful

    It is perfectly feasable for there to be ways in which Win32 threads are better than Pthreads (as hard as that is to believe), and also vice versa. Hence the two articles. You didn't read the two articles, did you?

    From the first one:

    I've used both POSIX threads (Pthreads) and Win32 threads APIs and I believe that Pthreads has the better programming model of the two. While each threading method can create threads, destroy threads, and coordinate interactions between threads, the reason I make this claim is the simplicity of use and elegance of design of Pthreads. Let me illustrate with a few examples. And, from the second one:

    I've used both POSIX threads (Pthreads) and Windows threads APIs, and I believe that Windows has the better programming model of the two. While each threading method can create threads, destroy threads, and coordinate interactions between threads, the reason I make this claim is the simplicity of use and elegance of design of the Windows threads API. This is all from the perspective of multithreaded code developers or maintainers. Let me illustrate with a few examples.
    --
    www.timcoleman.com is a total waste of your time. Never go there.
  9. there are differences, but not _that_ many by radarsat1 · · Score: 4, Interesting

    The thing is, they are just APIs. They both do just about the same thing. Asking which one is better is a pretty pointless question. I have always thought that the WaitFor* functions in Windows are quite nice, but frankly not that much of an advantage. It's quite rare that you actually need to wait for multiple objects of different types at the same time. Combine that with the fact that its semantics are slightly different for different objects (it destroys a thread, but only unlocks a mutex), and your program is that much more difficult to read. Of course, this is just comparing two APIs, a mostly pointless exercise, and says _nothing_ about implementation, which is quite a bit more important in terms of comparison. For example, Linux has completely changed its pthreads implementation since the switch from 2.4 to 2.6 (from LinuxThreads to NPTL), and programmers get the advantages without needing to change anything. In Windows, of course, we have no (or very little) idea of the implementation, except for what we can infer from the API, and performance tests. A third argument in this little debate could be to argue that one should just stay away from threads, period. I haven't successfully done it myself, since I find the threading paradigm useful, but using processes and non-blocking IO properly, one can avoid threads completely. Of course that's a bit easier to do with some of the Posix functions (eg. socketpair). But doing so will probably result in a more robust piece of software, and which scales better to multiple cores/processors. (Because processes do not share memory, so inter-thread cache misses will be minimized.)

    That said, I find it quite creepy that this guy wrote these two articles with extremely similar wording for his introductions, making the exact opposite points. It is very strange. I wonder what his motivation was.

  10. Re:win32 has 1 thing i want pthreads to have by tjw · · Score: 4, Informative


    I'm not an expert on pthreads by any means, but I think what you're looking for is pthread_cond_timedwait().

    --

    XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UB E-TEST-EMAIL*C.34X
  11. Re:Look at the dates, Dude. by AKAImBatman · · Score: 4, Insightful

    I think you misunderstand. My suspicion stems from the author's motivations, not whether Microsoft is involved or not. His first post was made to a software forum, where it's unlikely he was compensated for it. His second post was made to a semi-official corporate "blog", which raises questions about if he's being paid or not. Being an author myself, I know that's it's incredibly easy to step over the lines of journalistic integrity in exchange for that few hundred dollars of pay.

    The question is, did the author really change his mind, or is he writing the conclusions that he knows will net him an income? If it's the former, he really should have expanded his article to prevent this sort of issue. If it's the latter, then the author is untrustworthy and should no longer be paid to provide his opinion in any professionally written form.

    Microsoft never enters into the equation here, save for the fact that some entities may have a monetary interest in promoting Microsoft Windows -OR- Linux. (There's money in both directions.) That being said, it is sad that this issue may never have come up if the order of the articles was reversed. Slashdot is very pro-Linux (something which I am ambivalent about), meaning that it would have likely been seen as a win rather than the questionable journalism it is.

  12. A better Win32/Linux thread article by NullProg · · Score: 4, Informative

    It sounds like the developer from Intel needs to ask IBM:

    http://www-128.ibm.com/developerworks/linux/librar y/l-ipc2lin1.html

    Enjoy,

    --
    It's just the normal noises in here.
  13. Re:Eeew, threads. by daVinci1980 · · Score: 4, Insightful

    These solutions are not equivalent. And the reason that fork/exec doesn't have the same problems as threading is because it can only (realistically) solve a subset of the problems that multithreading can solve.

    You have to consider the task you're working on before you decide whether you want to go with fork/exec or multiple threads.

    A sibling post mentioned the cost of creating new processes on windows, and that's definitely something to consider: it's quite expensive to do so on windows.

    However, the more important question is the problem you're working on solving.

    If you're working on a task that allows each drone to work without communicating with any of the other drones, then fork/exec is a possible candidate. If you're working on an application where you require even a minimal amount of synchronization between different drones, fork/exec is a huge, huge pain in the ass.

    An example of a good fork/exec app: webserver. One process deals with hearing the incoming connection, spawns off a new process to actually handle an individual connection. As a bonus, a single bad client connection will most likely NOT kill the whole webserver. (A malicious client will kill the process they've connected to, but probably none of the other processes, unless they manage to hang a database, etc).

    An example of a good multithreaded app: anything that plays lots of sounds (for a specific example, a game). There's lots of synchronization that has to go on here: threads have to be started (or more likely pulled from a pool) to play a sound, the threads playing the sound have to check back periodically to see if they should stop playing (or need to adjust their volume or other processing effects), they need to notify the originating thread when they have completed, etc. No one in their right mind would use fork/exec for this. Besides the high overhead of the process spawn on windows, you would need a process for each of the sounds playing, and you would need to use the OS interprocess communication apis to synchronize between the different processes (shared memory, global mutexes, or file pipes). Note that file pipes aren't sufficient for synchronization, so you'd still have to use OS mutexes to sync on.

    Yup.

    --
    I currently have no clever signature witicism to add here.
  14. Re:quothe the poster-What Has Changed by Nom+du+Keyboard · · Score: 5, Insightful
    "or what has changed since the first article was written?"

    What has likely changed is were the paycheck came from this week.

    --
    "It's the height of ridiculousness to say for those 9 lines you get hundreds of millions."
  15. Re:And so the point becomes... by skoaldipper · · Score: 5, Funny

    > Who gives a shit. They're just threads, man. Both work.

    You know, I said this very same thing back in the 70s. However, my buddy with the bell bottoms and KISS shirt got more play than I ever did with my adidas shorts and Vader cape.

    --
    I hope, when they die, cartoon characters have to answer for their sins.
  16. Re:win32 has 1 thing i want pthreads to have by dominator · · Score: 4, Informative

    pthread_cond_timedwait() waits on a condition variable, which basically signals that a mutex has been released. Win32 calls these things "events". It is not the same thing as joining on a thread. Joining a thread means "I'm waiting for this thread to exit, so that I can capture its return value."

    Sure, you could implement something like pthread_join_with_timeout() using a conditional inside the thread. But you'd need to do that manually, as pthreads doesn't provide a primitive for that particular use-case AFAIK.

  17. Re:Eeew, threads. by Mysticalfruit · · Score: 5, Interesting

    A while back I had to write a server that would recieve concurrent network connections from different clients and then get some data from those clients and do some processing and then interact with a database, then when it's done the fork exits.

    I ended up writing the whole thing using forks and no pthreads. My code was then subjected to a code review and one of the questions that came up in the review meeting was why I used fork and didn't do the implementation in pthreads. My arguement was one of complexity. I was challenged with the "fork is old technology, you should have used pthreads" and my response was "My implementation is easy to understand, oh and by the way, it works!"

    Needless to say, my code has been in production for about a year and a half with no issues. I'm sure someone smarter than me could have wrote the whole using pthreads, but I'm just not sure what it would have gained them other than a slightly smaller memory footprint but at the price of increased complexity.

    --
    Yes Francis, the world has gone crazy.
  18. Re:Eeew, threads. by pthisis · · Score: 4, Informative

    I don't know why the Windows equivalent of fork() is slower than the Unix fork(). Perhaps it is a historical thing. Unix programs often use fork() - shell scripts use it all the time (this is one reason why a Python or Perl script is often faster). I'm just guessing now, but perhaps Unix fork() is efficient because it is frequently used and has therefore been optimised in various ways (e.g. memory is only copied if there is a write on Linux).


    I already got modded down for mentioning it elsethread (not sure why), but Windows does have ZwCreateProcess and NtCreateProcess. Both of those will do a copy-on-write fork() style process creation if you pass them a NULL SectionHandle--it is much more efficient than the normal CreateProcessEx and is the way to go for doing heavy multiprocess stuff on Win32.

    see, e.g., http://www.osronline.com/showThread.cfm?link=35916
    --
    rage, rage against the dying of the light
  19. Nothing has changed by einhverfr · · Score: 4, Informative

    Read the two articles closely, and side by side. What you will see is that both articles have an identical structure and make simply the opposite cases. The opening is almost verbatim between the two articles, for example.

    Although there are three year between the articles and people change, this looks to me like someone trying to write different articles from different viewpoints rather than proving that one is best. If he really had changed his mind, maybe he would have said so and referenced his previous article rather than copying it?

    --

    LedgerSMB: Open source Accounting/ERP