Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post

    Parallel processes in c++

    Hi. I wanted to know what is the substitute for Threads in c++. Or is there any?
    We can use threads to run simulatinous parts of a program parallely in java... so how can we do that in c++. a sample program or a hint would help.
    Thanks in advance
    Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.

    Albert Einstein
    -----------------------------------------------------
    My Blog songs

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    They are called threads in C++ too. Threads isn't a language specific term.

    As for creating them, it depends on the platform, different platforms have different APIs.
    OracleGuy

  • #3
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    For Windows, I believe the function is createthread().

    A trivial example is the ubiquitous "searching through an array" problem, which can become faster with proper parallel programming. Say for instance you have an array of 10 elements and you want to search for a specific value in the array. Say that you want to create one thread per core (though, I think some cores can handle two threads).

    So, in a dual-core system, you could divide the array in two, so that one thread searches from elements 0 - 4 while the other searches from elements 5 - 9.

    When you are parallel programming there are a lot of other things to consider, for instance variable usage. Most processors implement a mechanism to lock a variable from write/read access (either that, or you have to implement it yourself) while another core is performing logic on it - so other cores will not receive a possibly outdated value to perform it's own logic on. If one core is performing logic on the same variable, the other core is going to have to wait until the first core finishes. This idle time can actually increase your program's execution time drastically rather than diminish it.

    On the flip side, it is possible that there is no locking mechanism, and cores work with outdated values.

    Nowadays processors are very smart. Even if you program sequentially, the processor will break distinct tasks amongst the cores to increase program performance, so there is a chance that you don't even have to parallel program to reap the benefits.

    I suggest picking up a Graduate Level book on parallel programming if you want to get really good at it.
    Last edited by BWiz; 08-13-2009 at 07:29 AM.
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #4
    Regular Coder adarshakb's Avatar
    Join Date
    Jun 2009
    Location
    Silicon valley of india
    Posts
    247
    Thanks
    11
    Thanked 1 Time in 1 Post
    i want this only to test my codes of OpenGl using parallel programing to see the % increase in performance v/s time. But dont want to go in detail... I searched the net and found that there was too big explanations.
    Can any one of u just write a sample code where in i need to just call it. If its quite easy to implement it. I am using VS2003 .net. And if not can some one point me to a place where Threads are explained clearly.
    Thanks in advance
    Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.

    Albert Einstein
    -----------------------------------------------------
    My Blog songs

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Its not that simple, once you try to multi-thread a process you need to place in appropriate protections into your code. Shared resources between the threads need to be protected with mutexes or semaphores. And you have to make sure that you don't create deadlocks.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •