1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| #include <pthread.h>
#include <stdio.h>
/* Parameters to print_function. */
struct char_print_parms
{
/* The character to print. */
double sum;
/* The number of times to print it. */
long count;
};
/* Prints a number of characters to stderr, as given by PARAMETERS,
which is a pointer to a struct char_print_parms. */
void* char_print (void* parameters)
{
/* Cast the cookie pointer to the right type. */
struct char_print_parms* p = (struct char_print_parms*) parameters;
int i;
for (i = 1; i < p->count; i++)
p->sum = p->sum + i;
return NULL;
}
/* The main program. */
int main ()
{
pthread_t thread1_id;
pthread_t thread2_id;
struct char_print_parms thread1_args;
struct char_print_parms thread2_args;
thread1_args.sum = 0.0;
thread1_args.count = 000000000;
pthread_create (&thread1_id, NULL, &char_print, &thread1_args);
thread2_args.sum = 0.0;
thread2_args.count = 200000000;
pthread_create (&thread2_id, NULL, &char_print, &thread2_args);
pthread_join (thread1_id, NULL);
pthread_join (thread2_id, NULL);
fprintf(stderr,"%f \n",thread1_args.sum);
fprintf(stderr,"%f \n",thread2_args.sum);
/* Now we can safely return. */
return 0;
} |
Partager