multithreading in linux

Below is a sample multithreading program based on C language.


# 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;
}

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s