views:

68

answers:

3

Hi,

what the difference when the number of threads is determined, as e.g.:

for (i*10){   
    ...   
    pthread_create(&thread[i], NULL, ThreadMain[i], (void *) xxx);   
    ...
}

and when it is undetermined, just like this:

    ...  
    pthread_create(&threadID, NULL, ThreadMain, (void *) xxx);   
    ...

In my case the number i can varry from 1 to 10. If e.g. I use the first method, I need to create 10times as e.g.:

void *ThreadMain1(void *xxx)
{  
    ...  
}

until ...

void *ThreadMain10(void *xxx)
{  
    ...  
}

But if I use the second method, I need to create just :

void *ThreadMain(void *xxx)
{  
    ...  
}

So which one is correct?

Thanks for your time and replies,

+2  A: 

If the threads are doing the same tasks, they should use the same function (with different input maybe), so one ThreadMain is the correct way.

Drakosha
+2  A: 
  • threads doing the same tasks should use the same function
  • threads doing different tasks should use different functions
A: 

The first method is not valid syntax unless ThreadMain has been defined as an array of function pointers. That is:

pthread_create(&thread[i], NULL, ThreadMain[1], (void *) xxx);

does not call a function called ThreadMain1 unless you have previously done something along the lines of:

FuncPtr_t ThreadMain[10];
ThreadMain[1] = &ThreadMain1;

If you are simply trying to create 10 threads running the same task then the second method is correct.

Patrick
Sorry ! you are right , this is why i had a problemthe number of connections depends on the problem to simulate (as it can have 1 or more), but is limited to 10.Actually I have with synchronisation this is why I am trying both. If three clients coneect, the comunication should be synchronized sequentially, i.e. 1,2,3 at every exchange, not until the first terminates and then start the second ... How is this possible?Thanks a lot for your reply
make
I don't really understand what you're trying to do. I think you should start another thread and explain what you are trying to achieve rather than how you think you should achieve it. Maybe you should be passing in the number of the thread as the parameter xxx but if you want things done sequentially you may not need threads at all...
Patrick