- This
parallel programming
pattern is
created
as follows:
void *worker(int *id) // Not syntactically correct, but easier to code...
{
// Use *id as int parameter (The official way is: void * and cast)
}
int main(int argc, char *argv[])
{
pthread_t tid[N];
int arg[N];
/* ------------------------------------
Create the worker thread group
------------------------------------ */
for ( int i = 0; i < N; i++ )
{
arg[i] = i;
pthread_create(&tid[i], NULL, worker, &arg[i]);
}
/* ---------------------------------------
Wait for the worker threads to finish
--------------------------------------- */
for ( int i = 0; i < N; i++ )
pthread_join(tid[i], NULL);
}
|
|