Thread: Multi-threading in pure C

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    14

    Multi-threading in pure C

    Hi,

    Any idea how to go about implementing sometype of multi-Threading in pure ANSI C.

    I understand that in Linux, one would use pthreads, in windows one would create threads through the WinAPI.

    Is there a way to manually implement this logic in only ANSI C thus making it cross-platform/portable?

    In other words, what sort of logic would you use to split a CPU's time between 2 functions in the same program to allow them to run concurrently?

  2. #2
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Concurrency is as OS-specific as OS features come. You should use a multi-threading library that is cross-platform instead. Google for it.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Is there a way to manually implement this logic in only ANSI C thus making it cross-platform/portable?
    No.

    But there are pre-existing libraries to manage this for you. For example.
    Netscape Portable Runtime (NSPR)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    C11 actually has portable threads.
    (in threads.h and stdatomic.h)

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    14
    > Is there a way to manually implement this logic in only ANSI C thus making it cross-platform/portable?
    No.
    Considering that an OS like Linux was written in C, and it is capable of multi-threading, then how was the logic initially built in?

    How does one get that level of control over the CPU using C - considering that C is one of the lower level languages.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Considering that an OS like Linux was written in C, and it is capable of multi-threading, then how was the logic initially built in?
    Because it isn't written in purely ANSI-C.

    Threads in Linux are built on top of the clone(2) system call - which isn't part of ANSI-C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Repeat after me: "I'm not coding for CPU, I'm coding for the OS".

    Sure, you could implement some sort of concurrency middleware between the OS and your application. Whether can implies should is an open question.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    14
    ... Whether can implies should is an open question.
    Basically, why can't i control that aspect of the application and bypass the OS altogether? and how would I do that. maybe you could point me in the right direction if is possible....Maybe it sounds like a strange question buy I'm just curious.

    Must multithreading always be dependent on the OS.
    You're OS does not sit on top of another OS, it implements multithreading by controlling/managing the instructions sent to the hardware. Why can't an application do the same? and why can't it be done in C?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Why can't an application do the same?
    Because it probably involves running privileged instructions at some point - which can only be invoked from an OS.
    Userland programs run in a virtualised environment, they can't do just whatever they like to the hardware.


    > and why can't it be done in C?
    It can and is (mostly).
    Just rummage though the Linux source code to find out how.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    How does one get that level of control over the CPU using C - considering that C is one of the lower level languages.
    If you want to learn how to control a microprocessor (CPU) with C, buy a development board (like the PICdem board) and learn how to program a microprocessor in C-Language. You'll learn how to talk to registers, set up interrupts and all that fun stuff - You can even set up your own multi-threading paradigm.

    I'm sure that everyone will agree that Changing/bypassing anything set up by your OS on your computer is asking for trouble.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Threading
    By gametek101 in forum C Programming
    Replies: 19
    Last Post: 07-24-2009, 12:39 PM
  2. Using Multi-threading and/or Multi-process
    By lehe in forum C++ Programming
    Replies: 5
    Last Post: 07-14-2009, 11:43 AM
  3. Is Multi-threading necessary?
    By keira in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-14-2007, 05:13 AM
  4. Multi Threading
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-04-2006, 09:21 PM

Tags for this Thread