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 */

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

/* get the default attributes */

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;

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

Leave a Reply

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