Calling a void* function from main in C - c

Currently I am working on a program that uses threads to calculate the sum of square roots. My program works, however one of the requirements is to use the main thread to find the initial value, and as soon as I call the function Void *calc from main, the program breaks. Is there a certain way to make such a function call? Is this because the function is a pointer? Any help is appreciated.
#include <pthread.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#define NUM_THREADS 3
int ARGV;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
double total = 0;
void *calc(void* t){
int ph = (int)t + 1;
int start, stop, interval_size;
interval_size = ARGV/(NUM_THREADS + 1);
start = ((ph) * interval_size) + 1;
stop = (ph * interval_size) + 1;
double ttl;
int i;
for (i = start; i <= stop; i++){
ttl = ttl + sqrt(i);
printf("Total Thread %i %lf\n", ph, ttl);
}
pthread_mutex_lock(&m);
total = total + ttl;
pthread_mutex_unlock(&m);
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
int i;
double ttl;
ARGV = atoi(argv[1]);
pthread_t ti[NUM_THREADS];
calc(0);
for (i = 0; i < NUM_THREADS; i++) {
pthread_create(&ti[i], NULL, calc,(void *)i);
}
/*for (i = 1; i <= (ARGV / 4) ; i++){
ttl = ttl + sqrt(i);
}*/
for (i = 0; i < NUM_THREADS; i++) {
pthread_join(ti[i], NULL);
}
total = total + ttl;
printf("Result: %lf\n", total);
}
The program breaks as in the function seems to only be called once, instead of each thread using the function. The only value printed out is some vague incorrect number.

Your calc function does pthread_exit. Now pthread_exit can and should be called from the main thread, so that's fine
To allow other threads to continue execution, the main thread
should terminate by calling pthread_exit() rather than exit(3).
But since this happens before any other thread has been created, the program just exits straight away, without ever starting other threads.

Related

Monte Carlo with threading

This is what I am trying to accomplish.Write a multithreaded program in C (or C++/C#) that creates 5 threads. Each thread should generate 1,000 random points and count the number of points that occur within the circle. The main thread should wait for the five threads to terminate one after another. Once a thread is terminated, the main thread updates the value of PI using the total number of points in the circle and the total number of points generated by the terminated thread. For example, the main thread waits for the first thread to terminate. When the first thread is terminated, the main thread incorporates the total number of points in the circle obtained by the first thread to update the value of PI. Next, the main thread waits for the second thread to terminate. When the second thread is terminated, the main thread updates the value of PI using the total number of points in the circle obtained by the second thread, and so on.
I keep getting an error saying that non-void function doesn't return a value, so is there anyway I can get around that or what are some alternatives for me?
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
long incircle = 0;
long ppt; /* points per thread*/
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *runner() {
long incircle_thread = 0;
unsigned int rand_state = rand();
long i;
for (i = 0; i < ppt; i++) {
double x = rand_r(&rand_state) / ((double)RAND_MAX + 1) * 2.0 - 1.0;
double y = rand_r(&rand_state) / ((double)RAND_MAX + 1) * 2.0 - 1.0;
if (x * x + y * y < 1) {
incircle_thread++;
}
}
pthread_mutex_lock(&mutex);
incircle += incircle_thread;
pthread_mutex_unlock(&mutex);
}
int main(int argc, const char *argv[])
{
if (argc != 3) {
fprintf(stderr, "usage: ./pi <total points> <threads>\n");
exit(1);
}
long totalpoints = atol(argv[1]);
int thread_count = atoi(argv[2]);
ppt = totalpoints / thread_count;
time_t start = time(NULL);
srand((unsigned)time(NULL));
pthread_t *threads = malloc(thread_count * sizeof(pthread_t));
pthread_attr_t attr;
pthread_attr_init(&attr);
int i;
for (i = 0; i < thread_count; i++) {
pthread_create(&threads[i], &attr, runner, (void *) NULL);
}
for (i = 0; i < thread_count; i++) {
pthread_join(threads[i], NULL);
}
pthread_mutex_destroy(&mutex);
free(threads);
double points_per_thread = 0.0;
printf("Pi: %f\n", (4. * (double)incircle) / ((double)points_per_thread * thread_count));
printf("Time: %d sec\n", (unsigned int)(time(NULL) - start));
return 0;
}
'''
The return type of 'runner' is void*, so that's what it needs to return
In your case, it looks like you would just want to add
return NULL;

pthread_join hangs accordingly to random global variable value

I have built this code utilizing pthreads. The goal is to build an array X[N][D] and assign random values to it. You could read the elements of this array as the coefficients of some points.
On the next step I am trying to calculate an array distances[N]which holds all the distances between the last element (Nth) and each other element. The distances calculation is executed using pthreads.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#define N 10
#define D 2 //works for any d
#define NUM_THREADS 8
//double *distances;
//int global_index = 0;
pthread_mutex_t lock;
double *X;
typedef struct
{
//int thread_id;
double *distances;
int *global_index ;
pthread_mutex_t lock;
double *X;
}parms;
void *threadDistance(void *arg)
{
parms *data = (parms *) arg;
double *distances = data->distances;
double *X = data->X;
int *global_idx = data -> global_index;
int idx,j;
//long id = (long)arg;
pthread_mutex_lock(&lock);
while(*global_idx<N)
{
//printf("Thread #%ld , is calculating\n", id);
idx = *(global_idx);
(*global_idx)++;
pthread_mutex_unlock(&lock);
for(j=0 ; j<D; j++)
{
distances[idx] = pow(X[(j+1)*N-1]-X[j*N+idx], 2);
//printf("dis[%d]= ", dis);
//printf("%f\n",distances[idx]);
}
//printf("global : %d\n", *global_idx);
}
pthread_exit(NULL);
}
void calcDistance(double * X, int n, int d)
{
int i;
int temp=0;
pthread_t threads[NUM_THREADS];
double *distances = malloc(n * sizeof(double));
parms arg;
arg.X = X;
arg.distances = distances;
arg.global_index = &temp;
for (i=0 ; i<NUM_THREADS ; i++)
{
pthread_create(&threads[i], NULL, threadDistance, (void *) &arg);
}
for(i = 0 ; i<NUM_THREADS; i++)
{
pthread_join(threads[i], NULL);
}
/*----print dstances[] array-------*/
printf("--------\n");
for(int i = 0; i<N; i++)
{
printf("%f\n", distances[i]);
}
/*------------*/
free(distances);
}
int main()
{
srand(time(NULL));
//allocate the proper space for X
X = malloc(D*N*(sizeof(double)));
//fill X with numbers in space (0,1)
for(int i = 0 ; i<N ; i++)
{
for(int j=0; j<D; j++)
{
X[i+j*N] = (double) (rand() / (RAND_MAX + 2.0));
}
}
calcDistance(X, N, D);
return 0;
}
The problem is that the code executes completely only when N=100000. If N!=100000 the code just hangs and I have found that the source of the problem is the pthread_join() function. First of all I cannot understand why the hang depends on the value of N.
Secondly, I have tried printf()ing the value of global_index (as you can see it is commented out in this particular sample of code). As soon as I uncomment the printf("global : %d\n", *global_idx); command the program stops hanging, regardless of the value of N.
It seems crazy to me as the differences between hanging and not hanging are so irrelevant.
regarding:
pthread_mutex_lock(&lock);
while(*global_idx<N)
{
// ...
pthread_mutex_unlock(&lock);
The result is that after the first iteration of the loop, the mutex is always unlocked. Suggest moving the call to pthread_mutex_lock() to inside the top of the loop.
after making the above corrections, I then set N to 10000. Then re-compiled, etc. The result was a seg fault event, so the mis-handling of the mutex is not the only problem.
regarding:
* First of all I cannot understand why the hang depends on the value of N.*
it seems the program is actually crashing with a seg fault event, not hanging

c - progress for each thread

I've got a program that takes n numbers that generates a sum of each number from 0 to N. A new thread is created for each number given:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct sum_runner_struct {
long long limit;
long long answer;
};
// Thread function to generate sum of 0 to N
void* sum_runner(void* arg)
{
struct sum_runner_struct *arg_struct = (struct sum_runner_struct*) arg;
long long sum = 0;
for (long long i = 0; i <= arg_struct->limit; i++)
{
sum += i;
}
arg_struct->answer = sum;
pthread_exit(0);
}
int main(int argc, char **argv)
{
if (argc < 2)
{
printf("Usage: %s <num 1> <num 2> ... <num-n>\n", argv[0]);
exit(-1);
}
int num_args = argc - 1;
struct sum_runner_struct args[num_args];
// Launch thread
pthread_t tids[num_args];
for (int i = 0; i < num_args; i++)
{
args[i].limit = atoll(argv[i + 1]);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tids[i], &attr, sum_runner, &args[i]);
}
// Wait until thread is done its work
for (int i = 0; i < num_args; i++)
{
pthread_join(tids[i], NULL);
printf("Sum for thread %d is %lld\n", i, args[i].answer);
}
}
I want to display the progress of each thread (maybe in a percentage?) and from there I can calculate overall progress given each thread progress. I don't know how I can implement the progress for each thread though, how could I go about doing this?
One way to do it would be to add a global mutex, plus add a new member variable like long long current_index to your sum_runner_struct.
Every so often (e.g. maybe once every 1 million iterations of the for-loop?), each thread would then lock the mutex, set arg_struct->current_index=i;, and then unlock the mutex.
Then the main thread could then occasionally lock the mutex, iterate over the array sum_runner_structs to print out each thread's current_index value, and also tally up the sum of all of the values for the global-progress calculation, then unlock the mutex.

Thread access static variable cause segmentation error [duplicate]

This question already has answers here:
initializing a static variable in header
(4 answers)
Closed 5 years ago.
I'm trying to compare performance of methods about adding two matrix.
one method is just to add.
the other method is to use threads.
but I have a trouble about segmentation error when a thread access a static variable!
Here is my code.
main.c
#include "matrixProcessor.h"
void main()
{
time_t s0, e0;
int i;
int status;
inputVec1 = (int*)malloc(sizeof(int)*(SIZE*SIZE));
inputVec2 = (int*)malloc(sizeof(int)*(SIZE*SIZE));
outputVec = (int*)malloc(sizeof(int)*(SIZE*SIZE));
srand(time(NULL));
initializeVector(inputVec1);
initializeVector(inputVec2);
//printf("=== INPUT VECTOR 1 ===\n");
//printVector(inputVec1);
//printf("=== INPUT VECTOR 1===\n");
//printVector(inputVec2);
//s0 = clock();
//addVec(inputVec1, inputVec2, outputVec);
//e0 = clock();
//printf("Basic Method Time : %f (s)\n",(double)(e0-s0)/CLOCKS_PER_SEC);
s0 = clock();
for(i = 0; i < NUM_THREAD; i++)
{
printf("%d-Thread Call\n",i);
pthread_create(&tid[i], NULL, &addProc, (void*)&i);
sleep(1);
}
e0 = clock();
printf("=== OUTPUT VECTOR===\n");
printVector(outputVec);
printf("Thread Method Time : %f (s)\n",(double)(e0-s0)/CLOCKS_PER_SEC);
free(inputVec1);
free(inputVec2);
free(outputVec);
}
matrixProcessor.c
#include "matrixProcessor.h"
void initializeVector(int* matPtr)
{
int i = 0;
for(i = 0; i < SIZE*SIZE ; i++)
{
matPtr[i] = rand()%100;
}
}
void addVec(int* inputVec1, int* inputVec2, int* outputVec)
{
int i = 0;
for(i = 0; i < SIZE * SIZE; i++)
{
outputVec[i] = inputVec1[i] + inputVec2[i];
}
}
void* addProc(void *p)
{
int* idx = (int*)p;
int count = (SIZE * SIZE) / NUM_THREAD;
int i;
printf("idx value : %d\n",*idx);
printf("Test : %d ", inputVec1[0]);
for(i = (*idx) * count ; i < (*idx)*count + count; i++)
{
outputVec[i] = inputVec1[i] + inputVec2[i];
}
return NULL;
}
void printVector(int* vec)
{
int i = 0;
int j = 0;
for(i = 0; i < SIZE ; i++)
{
for(j = 0; j < SIZE; j++)
{
printf("%d\t", vec[SIZE * i + j]);
}
printf("\n");
}
}
matrixProcessor.h
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define SIZE 10
#define NUM_THREAD 10
pthread_t tid[NUM_THREAD];
static int* inputVec1;
static int* inputVec2;
static int* outputVec;
void initializeVector(int* matPtr);
void printVector(int* vec);
void addVec(int* inputVec1, int* inputVec2, int* outputVec);
void* addProc(void *p);
when compiling, i use -static - lpthread options.
I'm sure that accessing static variable cause segmentation error,
because this program prints some messages before reaching a code line which access static variable.
Here is result.
0-Thread Call
idx value : 0
Segmentation Error! ./main
Please someone help me..!
There are multiple problems in your code. The two that jump right out to me are listed below.
First, this one:
pthread_create(&tid[i], NULL, &addProc, (void*)&i);
What's the value of i when the child thread accesses it? It can be anything, because it changes as the main thread spawns more threads and continues to run.
Second, what do you think these free() statements do when they're executed while the child threads are still running:
free(inputVec1);
free(inputVec2);
free(outputVec);
When your code makes those calls, the child threads are still running since you don't call pthread_join() to make sure they've all completed.
You likely get the segmentation violation because your threads are accessing free()'d memory.

Reading array from threads, c, cygwin

I'm pretty new to threads and would like some insight. I'm trying to get the percentage each thread has completed for its calculation. Each thread will report its percentage to a different element of the same array. I have this working with pthread_join immediately after pthread_create and a separate thread for reading all the values of the array and printing the percentage but when I have all threads running after each other without waiting for the previous one to finish I get some weird behavior. This is how I'm accessing the shared (global) array.
//global
int *currentProgress;
//main
currentProgress = malloc(sizeof(int)*threads);
for(i=0; i<threads; i++)
currentProgress[i] = 0;
//child threads
currentProgress[myId] = (int)percent; //myId is unique
//progress thread
for(i=0; i<threads; i++)
progressTotal += currentProgress[i];
progressTotal /= threads;
printf("Percent: %d", progressTotal);
This is essentially the code I think is not being used correctly for multi-threads. When I print out the state of the shared array, I notice that as soon as another thread starts accessing the array (different element though), the previous element immediately goes to some random number... -2147483648 and when the latter element finishes the prior element continues like normal. Should I be using semaphores for this? I thought I could access different elements of an array at the same time and I thought reading them wasn't an issue.
This is the entire code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>
#include <pthread.h>
#include <string.h>
#define STDIN 0
int counter = 0;
uint64_t *factors;
void *getFactors(void *arg);
void *deleteThreads(void *arg);
void *displayProgressThread(void *arg);
int *currentProgress;
struct data
{
uint64_t num;
uint64_t incrS;
uint64_t incrF;
int threads;
int member;
} *args;
int main(int argc, char *argv[])
{
if(argc < 3) {printf("not enough arguments"); exit(1);}
int i;
int threads = atoi(argv[2]);
pthread_t thread_id[threads];
pthread_t dThread;
currentProgress = malloc(sizeof(int)*threads);
for(i=0; i<threads; i++)
currentProgress[i] = 0;
args = (struct data*)malloc(sizeof(struct data));
args->num = atoll(argv[1]);
args->threads = threads;
uint64_t increment = (uint64_t)sqrt((uint64_t)args->num)/threads;
factors = (uint64_t*)malloc(sizeof(uint64_t)*increment*threads);
pthread_create(&dThread, NULL, displayProgressThread, (void*)args);
//for the id of each thread
args->member = 0;
for(i=0; i<threads; i++)
{
args->incrS = (i)*increment +1;
args->incrF = (i+1)*increment +1;
pthread_create(&thread_id[i], NULL, getFactors, (void*)args);
usleep(5);
}
for(i=0; i<threads; i++)
{
pthread_join(thread_id[i], NULL);
}
sleep(1);
printf("done\n");
for (i=0; i<counter; i++)
printf("\n%llu : %llu", factors[++i], factors[i]);
return 0;
}
void *getFactors(void *arg)
{
uint64_t count;
int myId;
int tempCounter = 0, i;
struct data *temp = (struct data *) arg;
uint64_t number = temp->num;
float total = temp->incrF - temp->incrS, percent;
myId = temp->member++;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
for(count=temp->incrS; count<=temp->incrF; count++)
{
percent = (float)(count-temp->incrS)/total*100;
currentProgress[myId] = (int)percent;
if (number%count == 0)
{
factors[counter++] = count;
factors[counter++] = number/count;
}
usleep(1);
}
usleep(1);
pthread_exit(NULL);
}
void *displayProgressThread(void *arg)
{
struct data *temp = (struct data *) arg;
int toDelete = 0;
while(1)
{
int i;
int progressTotal = 0;
char *percent = malloc(sizeof(char)*20);
for(i=0; i<toDelete; i++)
printf("\b \b");
for(i=0; i<temp->threads; i++){
progressTotal += currentProgress[i];
}
progressTotal /= temp->threads;
printf("|");
for(i=0; i<50; i++)
if(i<progressTotal/2)
printf("#");
else
printf("_");
printf("| ");
sprintf(percent, "Percent: %d", progressTotal);
printf("%s", percent);
toDelete = 53 + strlen(percent);
usleep(1000);
fflush(stdout);
if(progressTotal >= 100)
pthread_exit(NULL);
}
}
There are some non synchronized pieces of code that are accessed by the threads which cause this problem.
One first place to be synchronized is:
myId = temp->member++;
But more importantly is that, the main thread is doing:
args->incrS = (i)*increment +1;
args->incrF = (i+1)*increment +1;
while at the same time in the threads:
for(count=temp->incrS; count<= temp->incrF; count++)
{
percent = (float)(count-temp->incrS)/total*100;
currentProgress[myId] = (int)percent;
if (number%count == 0)
{
factors[counter++] = count;
factors[counter++] = number/count;
}
usleep(1);
}
The unsynchronized accesses mentioned above affect the calculation of percent value which results in such abnormal happenings. You have to do synchronization in all these places in order to get the kind of behavior you would expect.

Resources