Problems passing array by reference to threads

后端 未结 3 1961
长情又很酷
长情又很酷 2020-12-21 05:16

I\'m learning threading and I\'ve found some simple examples.

What I\'m hoping to do is create 5 threads, that each assign a random number to an array of 20 int\'s.

3条回答
  •  失恋的感觉
    2020-12-21 05:47

    1. Casting:

    Note the casting!

    void myThread (void *param )
    {
      int *i = (int *) param;
      for (int x = 0; x < 1000000; x++)
      {
        i[x] = rand() % 2 + 1;
      }
    }
    

    2. Scope:

    Both threads are started numRunstimes. Starting them will take some time. Executing them too. The main should not be ended without respecting the threads.You should monitor the threads by means of WaitForMultipleObjects. _beginthread() returns a waitable handle.

    The implementation:

    int main()
    {
      long numRuns;
      HANDLE hThread[MAX_WAIT_OBJECTS];
    
      cin >> numRuns;
      // numRuns to be smaller than MAX_WAIT_OBJECTS/2!!!!
    
    
      for (int i = 0; i < numRuns; i++)
      {
         hThread[i * 2]     = _beginthread( myThread, 0, (void *) (array1) );
         hThread[i * 2 + 1] = _beginthread( myThread2, 0, (void *) (array2) );
         // or better use _beginthreadex(...)
      }
      WaitForMultipleObjects(numRuns * 2, hThread, TRUE, INFINITE);
      // bWaitAll flag set TRUE causes this statement to wait until all threads have finished.
      // dwMilliseconds set to INFINITE will force the wait to not timeout.
    }
    

    This way the main will only finish when all threads have done their job.

    3. Access:

    Both arrays are declared in the main section, so threads are sharing them. In order to protect access, you shoud introduce some kind of exclusivity. The easiest is a Critical Section Object. When all threads2 are only accessing array2 and all threads1 are only accessing array1, I'd suggest to use 2 critical section objects. A critical section can only be accessed by one thread at a time.

    Implementation:

    CRITICAL_SECTION cs1,cs2; // global
    
    int main()
    {
      long numRuns;
      HANDLE hThread[1000];
    
      // critical section object need to be initialized before aquired
      InitializeCriticalSection(&cs1);
      InitializeCriticalSection(&cs2);
    
      cin >> numRuns;
    
      for (int i = 0; i < numRuns; i++)
      {
        hThread[i * 2]     = _beginthread( myThread, 0, (void *) (array1) );
        hThread[i * 2 + 1] = _beginthread( myThread2, 0, (void *) (array2) );
      }
      WaitForMultipleObjects(numRuns * 2, hThread, TRUE, INFINITE);
    }
    

    And the threads (only one shown here):

    void myThread1 (void *param )
    {
      EnterCriticalSection(&cs1); //aquire the critical section object
      int *i = (int *) param;
      for (int x = 0; x < 1000000; x++)
      {
        i[x] = rand() % 2 + 1;
      }
      LeaveCriticalSection(&cs1); // release the critical section object
    }
    

    However: The whole story is yet a bit unclear. You want a number of threads fill the same array from the beginning to the end at the same time. That sounds weird. With the critical section objects as described here, the threads are going to be executed one after eachother anyway. What exactly are you aiming for?

提交回复
热议问题