Threads and Multi-threading

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Threads and Multi-threading

      Does anyone know of any good tutorial sites for threading and multithreading? I am having trouble getting my threads under control, and being able to do things like suspend, resume, and generally everything else excpet creating and running them.

      Any good books that you'd recommend?
    • Concurrently Running Threads

      I tried the multi-thread incrementer and I cannot get the threads to run at the same time.

      I wrote a class for handling the threads and calling the class, but it doesn't work. The threads are still executing in order.

      Here are my results (keep in mind my function in main puts a 10 delay on the even threads):

      Thread Handle: 184 Thread ID: 5132 Thread Param: 0
      Exit Time: 6:6:55:930

      Thread Handle: 180 Thread ID: 3120 Thread Param: 1
      Exit Time: 6:6:55:930

      Thread Handle: 188 Thread ID: 4512 Thread Param: 2
      Exit Time: 6:7:5:946

      Thread Handle: 192 Thread ID: 3444 Thread Param: 3
      Exit Time: 6:7:5:946

      Thread Handle: 196 Thread ID: 1544 Thread Param: 4
      Exit Time: 6:7:15:961

      Thread Handle: 200 Thread ID: 5548 Thread Param: 5
      Exit Time: 6:7:15:961

      Thread Handle: 204 Thread ID: 4464 Thread Param: 6
      Exit Time: 6:7:25:976

      Thread Handle: 208 Thread ID: 3364 Thread Param: 7
      Exit Time: 6:7:25:976

      Thread Handle: 212 Thread ID: 2516 Thread Param: 8
      Exit Time: 6:7:35:991

      Thread Handle: 216 Thread ID: 3432 Thread Param: 9
      Exit Time: 6:7:35:991

      The inc value: 1000000

      Time = H:M:S:MS

      The post was edited 1 time, last by Roster ().

    • RE: Concurrently Running Threads

      I have no idea what I'm looking at here. ;)

      What is the code suppose to do? How are these timings relevant? Can you be a bit more specific or post your code?

      -Rez
    • RE: Concurrently Running Threads

      Yes yes - post the class you've written and we'll take a look.
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • There is more to this class, but this is doing everything so I'm just posting this:

      MyThread::MyThread(int param_in, LPTHREAD_START_ROUTINE fp, DWORD(*run_func)(int, HANDLE, DWORD))
      {
      param = param_in;
      thread_h = CreateThread(NULL, 0, fp, ¶m, CREATE_SUSPENDED, &thread_id);
      (*run_func)(param, thread_h, thread_id); //Run function from pointer
      if(num_threads == NULL){num_threads=1;}
      else{++num_threads;}
      }

      Thread object constructor loop:

      for(int x = 0; x < 10; ++x)
      {
      TryEnterCriticalSection(&cs_myThread);
      threads[x] = new MyThread(x, (LPTHREAD_START_ROUTINE)ThreadFunction, pThreadFunction); //Param 1: thread object index
      LeaveCriticalSection(&cs_myThread);
      }

      The function that the code runs:

      DWORD ThreadFunction(int a, HANDLE h, DWORD id)
      {
      for(int i = 0; i < 100000; ++i)
      {
      EnterCriticalSection(&cs_inc);
      ++inc;
      LeaveCriticalSection(&cs_inc);
      }

      if(a == 0 || a == 2 || a == 4 || a == 6 || a == 8)
      {
      Sleep(10000);
      }

      EnterCriticalSection(&cs_out);
      GetSystemTime(&st);
      outfile << "Thread Handle: " << (int)h << " Thread ID: " << (int)id << " Thread Param: " << a << endl;
      outfile << "Exit Time: " << st.wHour << ":" << st.wMinute << ":" << st.wSecond << ":" << st.wMilliseconds << endl << endl;
      LeaveCriticalSection(&cs_out);

      return 0;
      }

      The problem:

      The reason for the timer is to slow some of the threads done...therefore the faster threads, if running at the same time, will finish before the others. From the output, even without the timer, they are running one after another.

      I guess my question is, how do we bypass the wait for return process?

      One other thing, I know that threads can be tempermental, if I try to get any information about the thread object from the class, I get an access violation and the app crashes. This happens with the critical sections. Ex. threads[1]->GetThreadHandle() will crash the app...this happens even when ECS/LCS encapsulates the threads[];

      The post was edited 3 times, last by Roster ().

    • I think I see the problem...

      First, your call to CreateThread is setting the creation flag to CREATE_SUSPENDED, which means the thread exists, but it isn't running. Then, in the MyThread constructor, you are calling the thread function, run_func. These two things conspire to make you think your threads are running but they really aren't - you are just running the thread function multiple times in the main thread.

      Try setting replacing the CREATE_SUSPENDED with good old 0 (zero). And remove the call to (*run_func) from the MyThread constructor.

      I've attached the RealTimeProcess.cpp code from the source, which has a class, RealTimeProcess, which is somewhat similar to what you are trying to do. It also has two free functions, ThreadProc and CreateThreads, which also should really help.
      Files
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • Initially I was using '0' but that would crash the application. The problem I was having was that I thought that assigning a function to a thread meant that anytime a that that function was called, it would only run on the thread assigned to run it.

      Ok, I'll check those out.

      The post was edited 1 time, last by Roster ().

    • I figured out what the problem was with my simplest version. The main thread was racing through my final output statements resulting in an incorrect inc value. I put a sleep before those outputs and it works fine.

      A couple other questions, the thread functions like Resume, Suspend, SetPriority, and so on, all return with a failed state when I try to use them... When and where should these be called? What access rights, if any, does a thread need to make changes to another thread?
    • The first parameter to CreateThread() are the security attributes. As long as those are NULL (which it appears they are in your code), I think you have full access from within the process that created the thread.

      In my own engine, I have the main thread do most of the processing and game logic. I have a render thread that takes care of all my rendering and a resource loader that deals with loading resources off of the hard drive. I never actually call SuspendThread() or SetThreadPriority() because they're not needed, though I did verify that they worked when I was building my threading system. In my case, I called them from the main thread.

      -Rez
    • I am having issues with Access Violations with my basic multi-thread graphics engine and I am not sure why. My program runs, keyboard and mouse input works, but the 3D render is only the backround color. I tried using PIX but it loads the app and then closes with no captures.

      Error:

      Unhandled exception at 0x774415de in Ursa Major.exe: 0xC0000005: Access violation reading location 0xfeef0066.
      [frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]

      The access violation points to the UpdateResources call.

      // Update variables that change once per frame
      EnterCriticalSection(&cs_gView);
      cbnc.mView = g_View; // Critical Section Memory Access: g_View
      g_pImmediateContext->UpdateSubresource( g_pCBNeverChanges, 0, NULL, &cbnc, 0, 0 );
      LeaveCriticalSection(&cs_gView);

      I checked the address of g_View and the address of cbnc being sent to D3D and they are different + I am using CS's in render and the g_View matrix update.

      What could be the problem?

      The post was edited 1 time, last by Roster ().

    • I'm not sure. Without context, I'd just be guessing. It may not be the multi-threaded stuff; it might be something completely unrelated. An access violation usually means you're trying to do something with a dangling or uninitialized pointer.

      One extremely useful thing you should do is write your multi-threaded system in such a way that you can switch between single-threaded and multi-threaded with a simple compiler flag. This will let you rule out (or rule in) threading as the cause. That should probably be your next step.

      -Rez
    • I agree with Rez completely - get it working single threaded first!
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • I used the error logging with PIX and found out it was a problem the TextureRV and the Constant Buffer for the ProjMatrix.

      I fixed the Texture problem but am stuck on the access violation problem. As you suggested, it goes away when only using thread-main.

      My program is now Single or Multithreaded. Thanks for the tip.

      The post was edited 2 times, last by Roster ().