-2

I have this code, and have a function named STARTWORK(int THREADNR) which cannot be called by 2 threads. Basically all the work of my program is contained in this function, where in the WORKVOID1() and WORKVOID2() i use separate for() functions to run through the data, and the for() function is split in half so that the 2 functions cover half-half of the data to be much faster and work on multicore processor.The problem is that if there is only the main thread, it works like a charm, but if i try to split the STARTWORK() into 2 parts in WORKVOID1() and WORKVOID2() each in multiple threads it doesn't work, so please help me fix it.

  public void OPTIMIZATION_ITERATION()
    {
           Thread WORK = new Thread(WORKVOID);
                WORK.Name = "T1";
                WORK.Start();
           Thread WORK2 = new Thread(WORKVOID2);
               WORK2.Name = "T2";
                WORK2.Start();
    }
            public void WORKVOID()
            {
                for (ALPHA = 0.001; ALPHA <= 0.5; ALPHA += 0.001)
                STARTWORK(1);
            }
            public void WORKVOID2()
            {
                for (ALPHA = 0.5; ALPHA <= 1; ALPHA += 0.001)
                STARTWORK(2);
            }

  public void STARTWORK(int THREAD)
{
//.......bunch of calculations then it writes it to file
    System.IO.StreamWriter WRITE = new System.IO.StreamWriter("OUTPUT_T"+THREAD+".txt", true);
    WRITE.BaseStream.Seek(0, SeekOrigin.End);
   WRITE.WriteLine(/*..calculations are written to file..*/);
    WRITE.Close();
    WRITE.Dispose();
}

The STARTWORK(int THREADNR) contains a parameter which will assign a number that will write the data to file so that the 2 will have different filenames, while ALPHA is a global double variable.

Mr. Curious
  • 11
  • 1
  • 1
  • 5
  • Are you aware of TPL `Paralell.For` ? I'm not sure what you're trying to achieve, what's the problem etc. Also your *Please help me fast!* isn't really going to help you. – Sriram Sakthivel Oct 27 '14 at 15:00
  • No I am not, can you please explain how to use it? – Mr. Curious Oct 27 '14 at 15:03
  • Please read some [tutorials](http://msdn.microsoft.com/en-us/library/dd460713%28v=vs.110%29.aspx) and [this](http://www.albahari.com/threading/part5.aspx), try to use it. Come back if you struck. We're happy to help. At the moment, I can't help more without knowing what you're trying to achieve. – Sriram Sakthivel Oct 27 '14 at 15:07
  • I`ve added more code to @Blaatz0r request so hopefully it's more clear now, if there is any comment you wish to share on the new code then i appreciate it. – Mr. Curious Oct 27 '14 at 15:09
  • 1
    Based on last edit, see [this](http://stackoverflow.com/a/19304339/1997232) answer regarding `StreamWriter` and multi-threading. – Sinatr Oct 27 '14 at 15:11
  • I'm sorry, I don't understand what is the problem. You say it doesn't work, what does that mean? What exactly doesn't work? What is the expected behavior? instead what happens? Also if you can tell what you're trying to achieve it will be helpful. – Sriram Sakthivel Oct 27 '14 at 15:15
  • I guess your problem is being `ALPHA` shared variable you need some synchronization when updating it. You need `Interlocked.Add` or `lock` or whatever to synchronize the access. – Sriram Sakthivel Oct 27 '14 at 15:17
  • Ok i shall fix the StreamWriter, but what about ALPHA, if ALPHA is a shared variable then so does the rest of them in the STARTWORK, does that mean that i need to lock all of them? – Mr. Curious Oct 28 '14 at 04:43

3 Answers3

0

if your working in c# you might want to try this.

 public void OPTIMIZATION_ITERATION()
{
       Thread WORK = new Thread(new ThreadStart(WORKVOID));
            WORK.Name = "T1";
            WORK.Start();
       Thread WORK2 = new Thread(new ThreadStart(WORKVOID2));
           WORK2.Name = "T2";
            WORK2.Start();
}
        public void WORKVOID()
        {
            for (ALPHA = 0.001; ALPHA <= 0.5; ALPHA += 0.001)
            STARTWORK(1);
        }
        public void WORKVOID2()
        {
            for (ALPHA = 0.5; ALPHA <= 1; ALPHA += 0.001)
            STARTWORK(2);
        }
Blaatz0r
  • 1,205
  • 1
  • 12
  • 24
  • What you have changed other than redundant delegate creation? – Sriram Sakthivel Oct 27 '14 at 14:58
  • the fact that the threads start. next time first write your comment wait for an answer then down the post. It's common sense. – Blaatz0r Oct 27 '14 at 14:59
  • Sorry doesn't work, the file what the STARTWORK(int THREADNR) uses is empty, its almost as if all the variables which the STARTWORK uses are simultaneously used without backup, i thought multiple threads make all global variables local in a backup, don't they to evade this conflict? – Mr. Curious Oct 27 '14 at 14:59
  • Do you have some more code to see whats the problem? – Blaatz0r Oct 27 '14 at 15:01
  • @Blaatz0r I don't really understand what your comment means. What do you mean by *the fact that the threads start*? Also nobody will be in a position to comment, wait for your reply, and downvote. If something obviously wrong they downvote and move on. – Sriram Sakthivel Oct 27 '14 at 15:04
  • Than you shouldn't even be on this site I try to give the guy some positive feedback and help to figure out his problem. Instead of just down voting posts and 'moving' on. – Blaatz0r Oct 27 '14 at 15:06
  • @Blaatz0r Ok i added more code, unfortunately this is all i can add as my code is confident, it's basically a bunch of calculation going around in the STARTWORK and then it writes it to file. – Mr. Curious Oct 27 '14 at 15:07
  • @Blaatz0r, don't worry, downvoting is not permanent. If your answer is helpful/correct (or become such after edit based on comments) someone may upvote or downvoter himself may remove his downvoting. – Sinatr Oct 27 '14 at 15:08
  • What positive feedback.? How does your answer helps? Your answer doesn't change anything whatsoever(unless am missing something). Without explanation of what you changed from OP's code and Why you did so, it is not useful to anyone. In this case, AFAICS your answer makes no difference. – Sriram Sakthivel Oct 27 '14 at 15:08
  • Same for your comment how does it help to point a person to a bloody tutorial. And if your missing something that is completely your problem.I'm trying to help him not you. – Blaatz0r Oct 27 '14 at 15:14
  • My comment helps by pushing OP in right direction. Care to say **how does your answer helps**? Also be nice, that's not bloody tutorial but useful one. – Sriram Sakthivel Oct 27 '14 at 15:19
  • 1
    @Blaatz0r i'm not the one who downvoted but what is different in your code? it looks like a copy and paste job to me!. You are saying in your post "if your working in c# you might want to try this". We know it is C# for two reason, 1) we looked at the tags, 2) we can see it's c#....... – Mo Patel Oct 27 '14 at 15:41
  • 1
    @MoPatel, I saw your comment in regards to the copy paste action, but if you look more closely to the edited post of the OP, you'll see that he has copied the answer of Blaatz0r. – RvdV79 Jan 08 '15 at 08:53
0

I think the problem is ALPHA. As soon as you start the second thread you set it equal to the condition that the first thread is looking for (ALPHA <= .5), then the first thread increments ALPHA by .001. The net result is that the first thread is highly unlikely to write anything to the file (maybe one line, depending on timing).

Beyond that, I don't really see the point of trying to to do this in parallel, if you are writing to the same disk, it's not going to help anything. If the calculations you are doing are CPU intense, then it might be a better approach to make the calculations parallel and then write everything to disk after it's done, or in chunks if memory is an issue.

richard
  • 196
  • 1
  • 3
  • 6
0

Global variables such as ALPHA cannot be used safely in multi-threaded contexts. This is because both threads are allowed to read/write at the exact same time which causes a race condition. I recommend you change your work methods to accept a double parameter. That way, each method would be using a local variable instead of a global shared variable.

Richmar1
  • 191
  • 10