CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2003
    Posts
    53

    Multithreading help

    Hi,

    I have a program which runs a worker thread on startup like this:

    _param = new THREADSTRUCT;
    _param->_this = this;
    AfxBeginThread(StartThread, _param);
    SetThreadPriority(_param, THREAD_PRIORITY_IDLE);

    The worker function StartThread has a while(TRUE) loop which runs indefinitely in the background and works fine.

    The problem is my main program is not working right in some circumstances while the worker thread is active. For example, I have a button which runs an executable using CreateProcess. This takes absolutely forever to start the exe while the worker thread is running. I usually have to close my program before the exe called by CreateProcess actually starts.

    Why is this worker thread causing so many problems when I try to do certain things from the main program?

    Thanks in advance,
    Jim

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Jim,

    You are addressing one of the fundamental aspects of multithreaded design.

    Your while loop in the worker thread is attempting to demand full run-time from the operating system.

    Try putting

    Code:
    Sleep(10);
    at the bottom of your thread and see how this goes.

    OK so it helps. Now you must begin to understand how this multithreading stuff actually works. The operating system (Windows in this case) attempts to appropriately schedule and synchronize all threads, processes and drivers in the system. Your new worker thread attempts to demand 100% resources. Windows will manage the thread but this causes conflicts such as the ones described.

    Study up on multithreading and thread synchronization.

    These topics are well addressed in Advanced Windows by Mr. Jeff Richter (http://www.amazon.com/exec/obidos/tg...glance&s=books), also available from all good public libraries so you don't have to spend any money.

    Sincerely, Chris.
    You're gonna go blind staring into that box all day.

  3. #3
    Join Date
    Mar 2003
    Posts
    53
    hi dude_1967,

    First off, thanks a lot for the help. You are exactly right, looks like I've jumped in without knowing enough about the way multithreading works. I imagined the thread would run quietly in the background whenever the main program wasn't busy etc.

    Adding the Sleep(10) fixed my problem right away, but I will obviously need to read up more and possibly re-do the code.

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    OK, good.

    Jim, there are several mechanisms for thread synchronization and multitasking scheduling.

    A lot of this stuff is described to some extent in the existing help files. You might try to look up functions such as:

    Code:
    WaitForSingleObject(...)
    WaitForMultipleObjects(...)
    
    CreateSemaphore(...)
    
    CreateCriticalSection(...)
    EnterCriticalSection(...)
    LeaveCriticalSection(...)
    DestroyCriticalSection(...)
    Try to gain an understanding of this stuff and I'm sure your design will profit.

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  6. #6
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by dude_1967
    Study up on multithreading and thread synchronization.

    These topics are well addressed in Advanced Windows by Mr. Jeff Richter (http://www.amazon.com/exec/obidos/tg...glance&s=books), also available from all good public libraries so you don't have to spend any money.
    Note that Amazon shows the ASIN as "1572315482" which is also the ISBN and that Amazon and the United States Library of Congress shows the author as "Jeffrey Richter". The Publisher is Microsoft Press.

    The US LOC book catalog can be searched at Library of Congress Online Catalogs.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured