multithreading in linux

Below is a sample multithreading program based on C language.

[sourcecode language=’cpp’]

# include

/* parameter that will be passed to each matrix */
struct v{

};

/* function that will be executed in each thread */
void *runonThread(void *param){
struct v* data = (struct v*) param;
/* put your application here */
pthread_exit(0);
}

int main()
{
pthread_t pid[NUMTHREADS];
pthread_attr_t attr;

/* get the default attributes */
pthread_attr_init(&attr);

for(i=0; i… = … ;
/*passing ‘data’ as parameter to each thread*/
pthread_create(&pid[i], &attr,
runonThread, data);
}

/*waiting for the threads to complete */
for(i=0; i<NUMTHREADS; i++)
pthread_join(pid[i], NULL);

return 0;
}
[/sourcecode]

You may download a sample application to count matrix multiplication that uses multithreads, Matrix_Mul.zip

Leave a Reply

Your email address will not be published. Required fields are marked *