Nested "for" loop divided by threads

62 views Asked by At
 for (int y=pocz;y<kon;y++)
            {
                    for (int x = 0; x < obrazekKopia.Width; x++)
                    {
                        if (((wskWejsciowy[0] + wskWejsciowy[1] + wskWejsciowy[2])/3 < 128))
                        {
                            wskWyjsciowy[0] = wskWyjsciowy[1] = wskWyjsciowy[2] = 0;
                        }
                        else
                        {
                            wskWyjsciowy[0] = wskWyjsciowy[1] = wskWyjsciowy[2] = 255;
                        }
                        wskWejsciowy += 3;
                        wskWyjsciowy += 3;
                    }
                    wskWejsciowy += nOffset;
                    wskWyjsciowy += nOffset;
                }
            }

My task to school is to divide it on threads. Program will turn monochromatic picture to B&W. The number of threads must be "as the user wants". I tried many things and i cannot do this by myself. This is my first C# program, please help me!

You asked what i have done so:

     Thread1 []tab = new Thread1[liczbaWatkow];
            ThreadStart[] pts = new ThreadStart[liczbaWatkow];
            Thread[] t = new Thread[liczbaWatkow];
            for (int s = 0; s < liczbaWatkow; s++)
            {
                pts[s] = delegate
                             {
                                 tab[s] = new Thread1();
                                 tab[s].wykonaj(prog, obrazekKopia, wskWejsciowy, wskWyjsciowy, pocz, kon, nOffset);
                             };
                pocz += obrazekKopia.Height / liczbaWatkow;
                kon += obrazekKopia.Height / liczbaWatkow;
                t[s] = new Thread(pts[s]);
            }



                        for (int i = 0; i < liczbaWatkow; i++)
                        {
                            t[i].Start();
                        }

  public class Thread1
    {
        public Thread1() { }
        public unsafe void wykonaj(int prog, Bitmap obrazekKopia, byte* wskWejsciowy, byte* wskWyjsciowy, int pocz, int kon, int nOffset)
        {
            int prog2 = prog * 3;
            unsafe
            {
                for (int y = pocz; y < kon; y++)
                {
                    for (int x = 0; x < obrazekKopia.Width; x++)
                    {
                        if (((wskWejsciowy[0] + wskWejsciowy[1] + wskWejsciowy[2]) < prog2))
                        {
                            wskWyjsciowy[0] = wskWyjsciowy[1] = wskWyjsciowy[2] = 0;
                        }
                        else
                        {
                            wskWyjsciowy[0] = wskWyjsciowy[1] = wskWyjsciowy[2] = 255;
                        }
                        wskWejsciowy += 3;
                        wskWyjsciowy += 3;
                    }
                    wskWejsciowy += nOffset;
                    wskWyjsciowy += nOffset;
                }
            }
        }

I am not a lazy guy who wants you to solve the problem

2

There are 2 answers

0
Aleksandar Stojadinovic On

Sorry, it's a bit hard to read the code with the cryptic words in, I'm guessing, Polish or Czech.

One of the solution is to separate the image in parts and apply to algorithm you showed in your snippet to each part independently. This comes down to splitting your array into parts. So, if you have an array of 300 pixels and 3 threads, each thread will get his own 100 pixels which will be turned into black and white. After that you can join those three monochromated arrays into one again.

I can give you the code, but I don't think that will be useful for you. Instead, I'll just give you a reference to the corresponding document: http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx

Edit:

No you have edited you question I can help you more. You are passing the complete bitmap to your thread. Instead, you should pass only an N-th part of it, where N is the number of threads and than do what you are doing already.

2
Dialecticus On

You have a loop that calls Start for all threads. You must also have a loop that calls Join for all threads, so that you can wait for the threads to finish. Once all threads are finished you can use the data that threads have produced.