Thread: multithreading question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    The example output was multiplying two 25x25 arrays with 25 threads available. Since then I've changed the code like this:
    Code:
    void *matmult(void *ptr) {
      int i, j, pos = (int)ptr;
      while (rowcount < m - 1) {
        pthread_mutex_lock(&mutex);
        if (rowcount < m - 1)
          ++rowcount;
        else {
          pthread_mutex_unlock(&mutex);
          return NULL;
        }
        pthread_mutex_unlock(&mutex);
        sleep(1); //lets other threads into queue before running and going back
        printf("thread %d is processing row %d\n",pos,rowcount);
        for (i = 0; i < k; ++i) {
          C[rowcount][i] = 0;
          for (j = 0; j < n; ++j)
            C[rowcount][i] += B[rowcount][j] * A[j][i];
        }
      }
    }
    and the thread creation code like:
    Code:
      pthread_t *tid = new pthread_t[numthreads];
      for (i = 0; i < numthreads; ++i)
        pthread_create(&tid[i],NULL,matmult,(void*)i);
      for (i = 0; i < numthreads; ++i)
        pthread_join(tid[i],NULL);
    These changes make it so that each thread is actually identified, and they step through the rowslike I had in mind. (0 1 2 3 0 1 2 3 0 1 2 3) etc. Still, I agree that replacing sleep with sched_yield would be a better solution, but that'll have to wait until I get back to school to try out.
    Last edited by ichijoji; 04-12-2005 at 08:05 PM.
    Illusion and reality become impartiality and confidence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. MySql & multithreading question
    By Thantos in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-01-2004, 10:25 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM