#include <stdio.h> // fputc(), stderr(), NULL
#include <pthread.h> // for pthread_t, pthread_create()
#include <unistd.h> // for sleep()
void * print_xs (void * unused) // The parameter is unused
{
while(1) // for ever
{
fputc ('x', stderr); // print x's to stderr
sleep(1); // pause for 1 second
}
return NULL; // Does not return
}
int main()
{
pthread_t thread_id;
/* Create a new thread. The new thread will run the print_xs
function. */
pthread_create (&thread_id, NULL, &print_xs, NULL);
while(1) /* Print o's continuously to stderr */
{
fputc ('o', stderr);
sleep(1); // pause for 1 second
}
return 0;
}
/*
gcc create1.c -o create1 -lpthread
./create1
oxoxoxoxoxxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxooxoxox^C
// ended with Ctrl^C
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <stdio.h> // fputc(), stderr(), NULL
#include <pthread.h> // for pthread_t, pthread_create(), pthread_join()
#include <unistd.h> // for sleep()
struct char_print_parms // Parameters to print function
{
char character; // The character to print
int count; // The number of times to print it
};
/* 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 = 0; i < p->count; i++)
{
fputc (p->character, stderr);
sleep(1); // pause for 1 second
}
return NULL;
}
int main()
{
pthread_t thread1_id, thread2_id;
struct char_print_parms thread1_args, thread2_args;
thread1_args.character = 'x'; // Create a thread to print
thread1_args.count = 30; // 30 'x's
pthread_create (&thread1_id, NULL, &char_print, &thread1_args);
thread2_args.character = 'o'; // Create a thread to print
thread2_args.count = 20; // 20 'o's
pthread_create (&thread2_id, NULL, &char_print, &thread2_args);
/* Make sure the first thread has finished: */
pthread_join (thread1_id, NULL);
/* Make sure the second thread has finished: */
pthread_join (thread2_id, NULL);
fputc ('\n', stderr);
return 0; // Now we can safely return
}
/*
gcc create2.c -o create2 -lpthread
./create2
xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxooxxxxxxxxxxx
./create2
xoxoxoxoxoxooxoxoxoxoxoxoxoxoxoxoxoxoxoxxxxxxxxxxx
*/
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <stdio.h> // for printf()
#include <pthread.h> // for pthread_t, pthread_create(), pthread_join()
unsigned long primes[5000];
unsigned idx = 0; // index of primes[]
/* Compute successive prime numbers. Return the
nth prime number, where `n' is the value pointed to by *arg. */
void * compute_prime (void * arg);
int main()
{
pthread_t thread;
unsigned which_prime = 5000;
unsigned long *prime;
primes[idx++] = 2; // primes[0] = 2;
/* Start the computing thread, up to the 5,000th prime number. */
pthread_create (&thread, NULL, &compute_prime, &which_prime);
/* Wait for prime number thread to complete, and get the result. */
pthread_join (thread, (void**) &prime);
/* Print the largest prime it computed. */
printf("The %uth prime number is %lu.\n", which_prime, *prime);
printf("The %uth prime number is %lu.\n", which_prime, primes[idx-1]);
return 0;
}
/* Compute successive prime numbers. Return the
nth prime number, where `n' is the value pointed to by *arg. */
void * compute_prime (void * arg)
{
unsigned n = *((unsigned*) arg);
if (n <= idx) // first prime is primes[0]
{return (void*) (primes+n-1);} // &primes[n-1]
// else
unsigned long candidate = primes[idx-1]+1; // last recorded prime + 1
unsigned long half;
unsigned i, is_prime;
while (1)
{
is_prime = 1; // (re)set
half = candidate / 2;
for (i = 0; i < idx && primes[i] <= half; i++)
{ /* Test primality by successive division (modulo): */
if (candidate % primes[i] == 0)
{
is_prime = 0; // not prime
break; // out of for(), test next candidate
}
}
if (is_prime)
{ /* Is this the prime number we're looking for? */
primes[idx++] = candidate;
// printf("primes[%u] = %lu\n", idx-1, candidate);
if (n <= idx)
{ // Return the desired prime number as the
return (void*) (primes+idx-1); // thread return value
} // &primes[idx-1], &candidate
}
// else
++candidate; // the next candidate
}
return NULL;
}
/*
gcc primes.c -o primes -lpthread
./primes
The 5000th prime number is 48611.
The 5000th prime number is 48611.
https://prime-numbers.info/number/5000th-prime
*/
In main(), prime must be a pointer such that &prime can be converted to void**, argument of pthread_join().
We cannot return &candidate from compute_prime() because candidate is a local variable. However, we can say,
int *p = &candidate;
return (void*)p;
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
*********************************************************************************
#include <stdio.h> // fputc(), stderr(), NULL
#include <pthread.h> // for pthread_t, pthread_attr_t, pthread_attr_setdetachstate,
// PTHREAD_CREATE_DETACHED, pthread_create(), pthread_attr_destroy()
#include <unistd.h> // for sleep()
void * print_xs (void * unused) // The parameter is unused
{
while(1) // for ever
{
fputc ('x', stderr); // print x's to stderr
sleep(1); // pause for 1 second
}
return NULL; // Does not return
}
int main()
{
pthread_t thread_id;
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
/* Create a new thread to run the print_xs function: */
pthread_create (&thread_id, &attr, &print_xs, NULL);
pthread_attr_destroy (&attr);
while(1) /* Print o's continuously to stderr */
{
fputc ('o', stderr);
sleep(1); // pause for 1 second
}
return 0;
}
/*
gcc create_one.c -o create_one -lpthread
./create_one
oxoxoxxoxoxoxooxxoxooxoxoxxooxxoxooxoxoxoxxooxoxoxoxoxoxoxoxoxoxox^C
// ended with Ctrl^C
*/