Pthreads on Mac OS X - a problem with mutexes

I am trying to learn how to program parallel algorithms in C using POSIX streams. My environment is Mac OS X 10.5.5 with gcc 4.

Compilation:

gcc -Wall -D_REENTRANT -lpthread source.c -o test.o

      

So my problem is that if I compile this in an Ubuntu 9.04 box, it runs smoothly in thread order, looks like mutexes on Mac, doesn't work, and threads don't wait for general information.

Mac:

#1
#0
#2
#5
#3
#4

      

ubuntu

#0
#1
#2
#3
#4
#5

      

Any ideas?

Follow below source code:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

#define NUM_THREADS 6
pthread_mutex_t mutexsum;
pthread_t threads[NUM_THREADS];
long Sum;

void *SumThreads(void *threadid){
    int tmp;
    int i,x[10],y[10];

    // Para cada x e y do vetor, jogamos o valor de i, sรณ para meio didรกticos
    for (i=0; i<10 ; i++){
        x[i] = i;
        y[i] = i;
    }


    tmp = Sum;
     for (i=0; i<10 ; i++){
         tmp += (x[i] * y[i]);
     }

       pthread_mutex_lock (&mutexsum);
       Sum += tmp;
       printf("Im thread #%ld sum until now is: %ld\n",threadid,Sum);
       pthread_mutex_unlock (&mutexsum);
       return 0;
}


int main(int argc, char *argv[]){
    int i;
    Sum = 0;

    pthread_mutex_init(&mutexsum, NULL);

    for(i=0; i<NUM_THREADS; i++){
        pthread_create(&threads[i], NULL, SumThreads, (void *)i);
    }

    pthread_exit(NULL);
}

      

+2


source to share


3 answers


There is nothing in the code that would make your threads ANYONE . If it works in some order on Ubuntu, it might be because you're just in luck. Try running 1000 times in Ubuntu and see if you get the same results over and over.

The point is that you have no control over the way the scheduler makes your threads available to the processor (s). Thus, when you iterate through the for loop you create your threads, you cannot assume that the first call to pthread_create will be fired first, or the mutex you create first will be locked. It's up to the scheduler, which is at the OS level and you can't manage it unless you write your own kernel :-).



If you require consistent behavior, why are you running your code in separate threads in the first place? If this is just an experiment, then one solution I can come up with is using pthread_signal to wake up a particular thread and make it work ... Then the awakened thread can wake up the second, etc. Etc.

Hope it helps.

+10


source


To my recollection, the variable you protected is not actually shared between processes. It exists in its own context within each of the threads. So it really is a matter of where each thread is assigned, which determines what will print.

I don't think a single simple mutex will allow you to guarantee correctness if correctness is defined as printing 0, 1, 2, 3 ...



what your code does is create multiple execution contexts using the code in your sum function as its execution code. the variable that you are protecting, if not declared static, will be unique for each call to this function.

in the end, it's a coincidence that you get the correct printout of one system because you don't have a logical method to block threads until it turns them right.

0


source


I am not doing pthreads in C or any other language (but I am programming a thread on high performance computers), so this "answer" may not be helpful to you;

  • What is required in the code for threads to pass the mutex in thread order? I can see that streams are created in id order, but what requires them to execute in that order /

  • If you need your threads to execute in id order, why? It seems that you are creating streams and then serializing them. For what purpose?

  • When I am programming in threads and worry about the order of execution, I often try to create a very large number of threads and see what happens with the order of execution.

As I said, ignore this if my understanding of C and pthreads is too poor.

0


source







All Articles