0

I'm attempting to write some multi-threaded code in C# XNA. What I want to do is move a for-loop calculation into a new thread. I'm using the ThreadStart() method in XNA to do this. However, while I can move the for-loop into a new thread, it's part of a method that takes in variables and while in the thread, the for loop in unable to use variables outside of the thread.

public static string EndianFlip32BitChunks(string input)
    {
        //32 bits = 4*4 bytes = 4*4*2 chars
        string result = "";


        ThreadStart threadStarter = delegate
        {
            for (int i = 0; i < input.Length; i += 8)
                for (int j = 0; j < 8; j += 2)
                {
                    //append byte (2 chars)
                    result += input[i - j + 6];
                    result += input[i - j + 7];
                }
        };
        Thread loadingThread = new Thread(threadStarter);
        loadingThread.Start();

        return result;
    }

Basically, I'm wondering how am I going to get variables from outside the thread, into the thread. These variables may be changing too. In the case of the code displayed above, the variable I need to use is the string result. The code works if the string is left outside the thread, but then the thread only reads the initial value and never updates that value.

Generalkidd
  • 579
  • 1
  • 8
  • 22
  • You're starting a thread and then *immediately* returning - what effect do you expect that to have? It's not like the string can change afterwards, as strings are immutable... – Jon Skeet Sep 10 '13 at 17:07
  • It's my first time getting involved with multi-threaded programming so I'm not too familiar with how it works. I need that method to return something every time it's called. Before I added the new thread, the program ran way too slow. All I changed from the original code was moving the for loops into a thread, everything else is still the same. What would you suggest I do to make this work without changing the fundamental way this method works? – Generalkidd Sep 10 '13 at 17:14
  • I think you need to take a step back - you're starting a new thread, but basically ignoring what it does. How were you expecting that to work? Adding a new thread doesn't make things faster without changing anything else... you need to really think carefully about what's involved. – Jon Skeet Sep 10 '13 at 17:16
  • By what I can see, I don't think threading is what you are looking for. Threading assume you want to do two things at the same time. Maybe you want to change your algorithm, http://stackoverflow.com/questions/2182002/convert-big-endian-to-little-endian-in-c-without-using-provided-func – the_lotus Sep 10 '13 at 17:16
  • The for-loop calculations in my program consume a large amount of CPU power and there are many of them in my program. It's too much for one CPU thread to handle. I need to move the calculations into a separate thread for performance reasons. Unless there's another way to make performance acceptable, threading seems to be the only way. However, I'm not sure how to structure my program so that I can move the calculations to another thread. – Generalkidd Sep 10 '13 at 17:20
  • Just moving calculations to another thread won't magically speed things up (as Jon said). If you have a main process, and you kick off another thread to do stuff but end up waiting in the main thread for the worker thread to finish before doing anything you're not gaining any benefit. First you need to look at the situation to see if threading is appropriate. Is there other work that can be done while waiting for the other thread to finish? That's one of the first questions you should answer before going down this route. – Tyler W Sep 10 '13 at 18:32
  • Looking more at what you want to do, I really don't think you want to use multithreading. The scenario you presented screams "not thread safe" if you're going to be using external variables that are being changed. Also have you profiled the code you want to multithread? THat might not even be your slowdown point. I can't see that short of a loop taking that long. – Tyler W Sep 10 '13 at 18:40

0 Answers0