Simple Multithreading design

Discussion in 'Mac Programming' started by Kaliemon, Nov 16, 2009.

  1. Kaliemon macrumors member

    Joined:
    Apr 30, 2006
    #1
    I need help with a simple multithreaded design.

    I have an app that loops through each pixel of an image and calculates the data at that point. Each pixel is completely independent of the others, so there shouldn't be any synchronization issues.

    Mainly, I need to find out how to spawn about 5 threads that each handle a 32x32 square, and do this for the entire image. Or possibly 5 threads each doing a pixel in the 32x32 square, I don't know which is better.

    I already have a subclass of a Qthread and a method that can handle the square, I just don't know how to create the next thread when one finishes. I thought about a loop, but it waits for all 5 threads to finish before creating the next set.

    I hope this all makes sense.

    Note: this is done in C++ on Linux
     
  2. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #2
    I'm not sure this is a Mac-specific question.

    I wouldn't make the threads operate on square sub-images, but rather on "strips" of the image. This is better for cache coherancy. Also, it would not make much sense to spawn more threads than you have cores in your system.
     
  3. Menge macrumors 6502a

    Menge

    Joined:
    Dec 22, 2008
    Location:
    Amsterdam
    #3
    If you want the benefits of multi-threading, then you don't create a thread when the last one finishes. You create all threads at the same time, assign them an area (re-recommending the same as Sander: a strip of pixels) and wait for all of them to finish. Multithreading doesn't go like this:
    (1)--, (2)--, (3)--, *

    it goes like this:
    (1) -\,
    (2) --,*
    (3) -/,
     
  4. Kaliemon thread starter macrumors member

    Joined:
    Apr 30, 2006
    #4
    So if I have a 4 core system, and say a 500px high image, would I create 500 threads and let the OS handle the queueing?

    I was thinking it would be best to have 4 threads processing, a 5th waiting to run and every time the 5th thread starts create another thread to be waiting to run. I am trying to break up the threads to process the smallest amount of data since 3 could finish instantly and I don't won't the last running thread to have to do a large amount of work when there are available threads.

    Also with the square vs line choices, the data in our case is much more similar in a 32x32 patch vs across a line, so wouldn't it be better for cache?
     
  5. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #5
    Definitely DO NOT create 500 threads. That's too much for any current computer.

    The Snow Leopard way to do this is to create 500 blocks, and let GCD decide how many threads to create and when to schedule them.
     
  6. Kaliemon thread starter macrumors member

    Joined:
    Apr 30, 2006
    #6
    That's how I am used to doing things, but since this has to run on linux I don't have GCD.
     
  7. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
    Threadsafe queue implementations shouldn't be *too* hard to find. Once you have one, you can simply spawn N threads where N is the number of cpus available, then have them sit in while (pixelstrip = workqueue.pop()) { process(pixelstrip); } loops. You may have to fiddle with the granularity of the pixel strips for optimum efficiency.
     

Share This Page