TiGreX
TiGreX

Reputation: 1816

misunderstanding how the threads work

I have a problem, big problem with threads in vb.net

First of all I want to tell that I didn't work with threads before (just on the school), I read lot of pages about it, but none of them could help me for my problem.

My main question here is understand the logic, and after if is possible, solve the problem that I have, both are related, then I going to explain the problem.

The code hasn't comments and/or documentation related, and is a program developed lot of years ago and the guy that did this is not working on the office, nobody knows how it works :S

I have a list called listOfProccess, and when is only 1 works fine.

in the callback function in QueueUserWorkItem fill the information about p, then execute the thread, I suppose

That list contain an array with information type

listOfProccess[].type = 'a/b/c/d/e/f/g/'

also the list include an ID.

Code:

If listOfProccess.Count > 0 Then

    Threading.ThreadPool.SetMinThreads(1, 1)

    Threading.ThreadPool.SetMaxThreads(4, 4)


    For Each p In listOfProccess
        Try

            Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(Object p.function))


        Catch e As Exception
            sendMail("mail@mail", "[email protected]", "", e.StackTrace)
        End Try
    Next

Problems: I have two problems here:

  1. Sometimes execute an item in the list i.e. 'a' in a infinite loop, and expends all resources of the machine, but if i close and restart again, works, I didn't know if is a problem with the threads or not, sincerely I think that is other thing, because this problem starts two or tree weeks ago, and the program still running during a year.

  2. This one i think is related about threads, if I have two (or more) p on the list like this:

    p[1].type = 'a/b/c/d/e/f/g/' p[1].ID = 1

    p[2].type = 'ww/xx/ff/yy/aa/rr/' p[2].ID =2

when the system execute something like this, the way that it follows is 'random' ie. takes for the first one, a, b,c and after this it does ww, and come back to the first. the problem is bigger if I have more items in the list, like 4 or 5; this is not a very big problem, because the program works, it not works 100% fine, but works, is more to try the understand why it works on this way.

Any help is welcome.

Upvotes: 0

Views: 121

Answers (1)

VMAtm
VMAtm

Reputation: 28355

The second problem is a race condition problem, as you can't guarantee the order of the execution for the threads, there is a non-zero probability that your threads will replace each other's values. There are a lot of wayss to solve this issue: algorythms (either lock-oriented and lock-free), synchronization techniques and so on, and there is no silver-bullet solution for this.

First problem is unclear for me, as I can't understand what exactly do you mean by a infinite loop - may be this can happen if you link items to deleted (from other thread) ones and there is no way to go out of your task, as the links in a list are broken. This still should be solved with sychronization.

I think your should start with MSDN articles or some book about multithreading in general, and after that you should split your program step by step for a small parts you understand.

Update:

p.function - about the infinite loop you should consider the code of this delegate. If there is a condition to restart work, you should check a recursion limit. For example, if there is an optimistic locking algorythm, your code can find out that the update it tried isn't valid, and restart it. And if the links are broken, it will never end it's task, and will stay forever in an infinite loop.

Upvotes: 1

Related Questions