Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Need help with Threading

    I'm trying to change a two threaded application (main/backgroundworker) into a true multithreaded application. As of now the backgroundworker only keeps the form refreshed while the main actually does all of the work. The amount of work though causes very slow execution time.

    Program is written in C#

    I have this:

    declare parameterized threadstarts (two threads)
    start threads

    Then in the code where the threads are supposed to be running

    Lock{
    initialize components
    }
    Lock{
    Set Component Variables
    }
    Lock{
    work with input file to create string to be worked on
    }
    Then the code searches the string for Regular Expressions like this
    lock
    {
    search for 1 thing
    }
    lock
    {
    search for another thing
    }
    lock
    {
    search for another thing
    }
    and so on
    eventually after the last search it gets to
    lock{
    update progress
    }
    then it will either move to the next fragment of teh input file or be done, if it doesn't loop back to do more searching it hits
    lock{
    further update progress
    }
    end of function

    Right now the end of function code is getting hit before any search besides the first one.

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Well first you have it backwards, your background worker threads should be doing the actual work. Leave refreshing the UI to the main thread.

    Second, it is hard to tell what the problem is from pseudo code. You are probably going to have to post actual code. But as far as making it faster, if you know what you are going to searching for in advance, that is the area where it can be split up into multiple sub-worker threads. However depending on your implementation, that might not be necessary, it might be slow because of the way you coded it. It depends.
    OracleGuy

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    My bad on the background worker, its actually doing all the real work, just I called it mainworker so I've been used to referring to it as main and thats why I wrote that.

    I can't post the actual code because its classified, so I'll try and give a more detailed pseudocode.

    Step 1:
    Check the UI for what regular expressions should be searched for
    Step 2:
    Initialize these regular expression components
    Step 3:
    Using the input file calculate how many 100MB file fragments will be needed to complete the file.
    Step 4:
    Initialize loop to loop through these fragments
    Step 5:
    Actually search for the expressions in this manner:
    if(ishouldsearchforthis)
    matches = regexp.matches(string,expression)
    if(ishouldsearchforthat)
    matches = regexp.matches(string,expression)
    and so on

    Each expression is unique but hardcoded into the program.

    My goal using multithreading is basically to have it so that steps 1-4 run on 1 thread to initialize everything for all of the other threads.
    Step 5 would run each regexp search once per variable and I would hopefully be able to make it so that each of three threads would run 1 at a time so that three could be processed at once.
    After the searching it would go back into a single thread to compile the log files and so on.

    So basically, what I want is for the searching the individual regular expressions to be split between threads, while the rest of it runs as it already does so as to prevent the threads from stepping on each other.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Ok, let me give a more simplified example on how threading can be used to speed things up.

    You have a list of 20 items and you are searching through a large text file for them. When you find them, you add them to a list.

    So to break this into multiple threads, you decide how many threads to use. And would create that many threads all calling the same function with code similar to:

    Your for loop would look like this:
    for(int index = thread_number; index < max_item_count; index += thread_count)

    And you would do your searching for the item in your list at a given index. Then if you find it you would add it to the found list for that thread.

    Then when the for loop ends, it would add the items from its list to the main shared list. This is done since it is shared resource it prevents the thread waiting for its turn to grab the mutex.

    So the function is the same, you just need to pass it the thread number, the thread count can be shared. If this was in a class, it would just be a data member of the class.

    How many processors do you have on the computers to use? If varies, you may want to make the number of threads dynamic based on the number of processors in the computer. I believe the Environment class has a property to tell you the number of CPUs.

    Does that make sense?
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •