stuff
This commit is contained in:
60
Examples/pthreads/pthreads3.c
Normal file
60
Examples/pthreads/pthreads3.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* pthreads3.c - Shows how to pass parameter values to thread routines
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// Function to check if a number is prime
|
||||
// Returns 1 if prime, 0 if not prime
|
||||
int is_prime(int v)
|
||||
{
|
||||
for(int i = 2; i < v; i++) {
|
||||
if(v % i == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Thread routine
|
||||
// calls is_prime to check if the input argument is prime
|
||||
//
|
||||
// NOTE: that parameters and return value are pointers
|
||||
void* thread_routine(void* args)
|
||||
{
|
||||
int* val = (int*)args;
|
||||
if(is_prime(*val)) {
|
||||
printf("THREAD %lu FOUND that %d is prime\n", pthread_self(), *val);
|
||||
} else {
|
||||
printf("THREAD %lu FOUND that %d is not prime\n", pthread_self(), *val);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int val1 = 2;
|
||||
int val2 = 100;
|
||||
|
||||
// Create 2 threads, passing a different value to each
|
||||
pthread_t thr1;
|
||||
pthread_t thr2;
|
||||
if(pthread_create(&thr1, NULL, thread_routine, (void*)&val1) == -1) {
|
||||
printf("COULD NOT CREATE A THREAD\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(pthread_create(&thr2, NULL, thread_routine, (void*)&val2) == -1) {
|
||||
printf("COULD NOT CREATE A THREAD\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Wait for the threads to finish
|
||||
pthread_join(thr1, NULL);
|
||||
pthread_join(thr2, NULL);
|
||||
|
||||
// Return success
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user