Thread: Can I easily write multi-core programs in C?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    15

    Can I easily write multi-core programs in C?

    Hi

    I was wondering whether it was easy in C to write a program to take advantage of two cpu cores.

    For example, if I wanted to make a program that found out what every number from 0-100 dived by 3 was, I could have one core work out 0-50 and the next 51-100 (so they would finish around the same time).

    Is there an easy way to do this? Being able to do this would be useful in programs where it is easy to split up a set of calculations where the two cores won't mess with the eachother's data.

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes and no. C in itself does not know anything about multiple threads - and you need threads to make the two cores run parts of your application in parallel. But making a multithreaded application isn't very difficult.

    Since C doesn't know about threads, you will need to understand how to make sure that thread 1 doesn't step on thread 2's toes when they are dancing together. This is a more complex subject than I can easily describe in a single post, so you probably should google from "introduction to threads" or something like that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Since C does now know about threads (yet) you will have to rely on your platform for thread synchronization. Windows offers several mechanism for thread safety and synchronization that are easy to use and efficient.

    Multi-threaded programming is not for the faint of heart. Your program may die in certain areas that have little or nothing to do with the core problem which can and will lead you off into the weeds for hours.

    Essentially the idea is that if a write to the data (IE: altering it or changing it) is being done it needs to be protected. If you are using singletons then they too will need some type of protection to ensure that somehow you don't end up with multiple singletons....which is very possible.

    Code:
    if (!m_pInstance)
    {
        m_pInstance = new Foo();
    }
    
    return m_pInstance;
    Sorry for the C++ example on the C board but I rarely do pure C and this was just for illustration.

    Here if thread 1 gets into the if but then gets switched before the new you have a problem. Thread 2 sees the instance as NULL and therefore creates the new instance. Now Thread 2 gets switched and thread 1 resumes after the if. Since it already passed the instance check it too will go ahead and create an instance. Now you have two instances of a singleton which is a very bad thing. Because of situations like this patterns like double check locking have been used. Even double check locking is not completely guaranteed to succeed.

    So before you run off to multi-thread land you might want to do some reading and research. Then you can weigh the advantages vs the disadvantages of multi-threading and decide if your app really needs it.
    Last edited by VirtualAce; 08-13-2008 at 05:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-10-2005, 08:55 AM
  2. Possible to write adaptive programs?
    By crag2804 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-16-2003, 04:35 PM
  3. Programming Puns
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 03-23-2002, 04:38 PM
  4. This should be the Cprogramming.com anthem!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-21-2002, 12:01 AM
  5. programs
    By xen in forum C Programming
    Replies: 5
    Last Post: 12-14-2001, 11:32 PM