Thread: Compiler Warnings in little multithread program

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    69

    Compiler Warnings in little multithread program

    Hey,

    as an experiment and an exercise I try to create a program which does two things at the same time:

    The first thread shall wait for users input an override an int array.
    The other thread shall constantly sum up the values every 30s.



    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    void job(int nums[]){
        while(1){
            int sum = 0;
            int length = (int)(sizeof(nums)/sizeof(nums[0])); //Compiler Warning Here
            for(int i = 0; i < length; i++){
                sum += nums[i];
            }
            printf("Length is %d", sum/length);
            sleep(30); //Compiler Warning here
        }
    
    
    }
    int main(int argc,char **argv){
        pthread_t *calc;
        int counter = 0;
        int nums[20];
        pthread_create(calc, NULL, *job, &nums); //Compiler Warning here
        while(1){
            char buf[50];
            fgets(buf, 50, stdin);
            nums[counter++%20] = strtol(buf, NULL ,10);
        }
        printf("Unreachable code");
    
    
    }
    Unfortunately this does not run properly.
    I am explicty wondering about the first warning and the third.

    Solution:
    The error was here:
    Code:
    pthread_t *calc;
        int counter = 0;
        int nums[20];
    pthread_create(calc, NULL, *job, &nums); //Compiler Warning here
    Thread closed
    Last edited by SuchtyTV; 02-23-2019 at 02:02 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    There are a number of errors in your code. The following "works", except that without using a mutex it is not safe to modify the array in one thread when the other thread is also accessing it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
     
    #define ARRAY_SIZE 10
    #define SECONDS_TO_SLEEP 5
     
    void *job(void *data) {
        int *nums = data;
        while (1) {
            int sum = 0;
            for (int i = 0; i < ARRAY_SIZE; i++)
                sum += nums[i];
            printf("average: %.2f\n", (double)sum / ARRAY_SIZE);
            sleep(SECONDS_TO_SLEEP);
        }
        return NULL;
    }
     
    int main() {
        int nums[ARRAY_SIZE] = {0};
     
        pthread_t calc;
        if (pthread_create(&calc, NULL, job, nums) != 0) {
            fprintf(stderr, "Error: pthread_create\n");
            exit(EXIT_FAILURE);
        }
     
        for (int i = 0; ; ) {
            char buf[50];
            fgets(buf, sizeof buf, stdin);
            nums[i++ % ARRAY_SIZE] = atoi(buf);
        }
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Warnings
    By RJKinsman in forum C Programming
    Replies: 5
    Last Post: 12-17-2015, 10:16 AM
  2. Compiler Warnings
    By DeanWinchester in forum C Programming
    Replies: 17
    Last Post: 12-25-2011, 10:59 AM
  3. Compiler warnings
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-22-2003, 01:13 PM
  4. compiler warnings
    By Roaring_Tiger in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 12:06 PM
  5. compiler warnings
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-31-2002, 02:12 PM

Tags for this Thread