0

I need to parse large text that is similar to XML. Because the text it is not in memory ( I have a StreamReader object) placing that stream on memory is where I take the most time. So on one thread I place that stream into an array (memory). And I have another thread that process that array. But I am having wierd behavieours. For example take a look at this image:

enter image description here

Note that listToProcess[counter] = buffer and right now that should be listToProcess[10] = buffer Note that the debugger says that listToProcess[10]=null why!? . the other thread just reads the items it does not modify them. At first I thought that maybe the other thread was making that item = null but that is not the case. why am I experiencing this behavior?


In case you want to see my code here it is:

        Semaphore sem = new Semaphore(0, 1000000);
        bool w;
        bool done = false;

        // this task is responsible for parsing text created by main thread. Main thread
        // reads text from the stream and places chunks in listToProces[]
        var task1 = Task.Factory.StartNew(() =>
        {
            sem.WaitOne(); // wait so there are items on list (listToProcess) to work with                
                                // counter to identify which chunk of char[] in listToProcess we are ading to the dictionary
                int indexOnList = 0;

                while (true)
                {
                    if (listToProcess[indexOnList] == null)
                    {
                        if (done)
                            break;

                        w = true;
                        sem.WaitOne();
                        w = false;



                        if (done)
                            break;

                        if (listToProcess[indexOnList] == null)
                        {
                            throw new NotFiniteNumberException();
                        }
                    }

                    // add chunk to dictionary
                    ProcessChunk(listToProcess[indexOnList]);

                    indexOnList++;
                }

        }); // close task1

        bool releaseSem = false;

        // this main thread is responsible for placing the streamreader into chunks of char[] so that
        // task1 can start processing those chunks
        int counter = 0;
        while (true)
        {
            char[] buffer = new char[2048];

            // unparsedDebugInfo is a streamReader object
            var charsRead = unparsedDebugInfo.Read(buffer, 0, buffer.Length);

            if (charsRead < 1)
            {
                listToProcess[counter] = pattern;
                break;
            }

            listToProcess[counter] = buffer;
            counter++;

            if (releaseSem)
            {
                sem.Release();
                releaseSem = false;
            }

            if (counter == 10 || w)
            {
                releaseSem = true;
            }
        }

        done = true;

        sem.Release();
       task1.Wait();

Edit

Sorry in other words why do I hit this break point:

enter image description here

I thought that counter was the problem but maybe I am doing something wrong with the semaphore...

2
  • I don't understand the question. There are 10 char arrays in the listToProcess array. The code is doing exactly what its suppose to do, it sets the valuye of releaseSem to true. As to the reason the 11th element is null is because you added the 10th element then increased the counter to 10. Commented Jul 11, 2012 at 16:02
  • Sorry I think I explained the question wrong because I thouught the problem had to do with counter. I updated the question you are right. sorry for not explaining my self correctly... Commented Jul 11, 2012 at 17:41

1 Answer 1

7

You have a counter++ so the one you updated before that was at index 9, not index 10.

Meaning : your claim that it set

listToProcess[10] = buffer:

Is incorrect: it set

listToProcess[9] = buffer:
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks you are right. The reason why I ask that question was because at some point on the code I get an exception because the item that I am about to read is null. I will work on an edit on the next 20 min. Thanks a lot for the help!
Hi @Marc Gravell, what would happen if 1 variable is updated EXACTLY AT THE SAME TIME by 2 different threads (Considering there is no synchronization)? I know maybe this is not the right question but it has some relation. Thanks in advance
@Daniel one update would win, one update would be lost, atbitrarily (there's also a chance of "torn" values with at least one reader thread and one writer thread, if the value isn't guaranteed to be atomic)
hi @Marc Gravell thanks +1 and marked as useful answer. is a non-atomic value for example a ++ (implying get-increment-set). do these 2 cases that could happen apply in any programming language?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.