45 lines
927 B
C
45 lines
927 B
C
#include<pthread.h>
|
|
#include <stdio.h>
|
|
|
|
/* Example program creating thread to compute square of value */
|
|
|
|
int value; /* thread stores result here */
|
|
void *my_thread(void *param); /* the thread */
|
|
|
|
main (int argc, char *argv[])
|
|
{
|
|
pthread_t tid; /* thread identifier */
|
|
int retcode;
|
|
|
|
/* check input parameters */
|
|
if (argc != 2) {
|
|
fprintf (stderr, "usage: a.out <integer value>\n");
|
|
exit(0);
|
|
}
|
|
|
|
/* create the thread */
|
|
retcode = pthread_create(&tid,NULL,my_thread,argv[1]);
|
|
if (retcode != 0) {
|
|
fprintf (stderr, "Unable to create thread\n");
|
|
exit (1);
|
|
}
|
|
|
|
/* wait for created thread to exit */
|
|
pthread_join(tid,NULL);
|
|
printf ("I am the parent: Square = %d\n", value);
|
|
}
|
|
|
|
|
|
/* The thread will begin control in this function */
|
|
void *my_thread(void *param)
|
|
{
|
|
int i = atoi (param);
|
|
|
|
printf ("I am the child, passed value %d\n", i);
|
|
value = i * i;
|
|
|
|
/* next line is not really necessary */
|
|
pthread_exit(0);
|
|
}
|
|
|