Thread: [C] Basic multi-thread programming help

  1. #1
    Registered User
    Join Date
    Sep 2012

    [C] Basic multi-thread programming help

    I want to create 2 threads, one does the max and one gives the average of a list of numbers entered in the command line.

    #include <stdio.h>
        #include <pthread.h>
        #include <stdlib.h>
        #include <limits.h>
        void * thread1(int length, int array[] )
        int ii = 0;
        int smallest_value = INT_MAX;
                for (; ii < length; ++ii)
                        if (array[ii] < smallest_value)
                                smallest_value = array[ii];
         printf("smallest is: %d\n", smallest_value);
        void * thread2()
        int main()
          int average;
          int min;
          int max;
          int how_many;
          int i;
          int status;
          pthread_t tid1,tid2;
          printf("How many numbers?: ");
          int ar[how_many];
          printf("Enter the list of numbers: ");
          for (i=0;i<how_many;i++){
                return 0;
    I just made the first thread, which is to print out the min. number, but I have the following errors when compiling:

    How many numbers?: 3
        Enter the list of numbers: 1
        Smallest: 1
        Segmentation fault
    How should I go on and fix the seg. fault?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    How should I go on and fix the seg. fault?

    I'd start by studying the information/tutorials which anduril462 linked.

    Specifically those bits that examine the API because you are not passing a function pointer to the API.


    Multi-thread programming help

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    pthread_create(3): create new thread - Linux man page

    > void * thread1(int length, int array[] )
    Observe that thread functions take only ONE parameter, and it is void*

    > pthread_create(&tid1,NULL,thread1(how_many,ar),NUL L);
    Observe that starting a thread is NOT the same as calling a function.

    If you want to pass a large amount of data into a thread, then use malloc to allocate an array/struct/whatever, then pass that as the last parameter of pthread_create (see examples on the man page).

    Finally, don't run any more code while you have warnings in the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Can you take a look of the new code, this one just adds all the numbers entered and print out the sum.

        #include <pthread.h>
        #include <stdio.h>
        int sum; /* this data is shared by the thread(s) */
        void *runner(void *param); /* threads call this function */
        int main(int argc, char *argv[])
        pthread_t tid; /* the thread identifier */
        pthread_t tid2;
        pthread_attr_t attr; /* set of thread attributes */
        if (argc != 2) {
        fprintf(stderr,"usage: a.out <integer values>\n");
        return -1;
        printf("sum = %d\n",sum);
        /* The thread will begin control in this function */
        void *runner(void *param)
        int i, list[] = atoi(param);
        sum = 0;
        for (i = 1; i <= sizeof(list); i++)
        sum = sum + list[i]
    Could you tell me how to store a list of numbers entered by the user, separated by a space

    this line
    list[] = atoi(param);
    is not right

  5. #5
    Registered User
    Join Date
    Jun 2005
    Quote Originally Posted by Lina_inverse View Post
    Could you tell me how to store a list of numbers entered by the user, separated by a space
    Try working out how you would do that in main() - without using any threads. Once you get that right, work out how to pass necessary data to a thread.

    Quote Originally Posted by Lina_inverse View Post
    this line
    list[] = atoi(param);
    is not right
    Indeed. It is trying to initialise an array with a single value. As such, it will not even compile.

    atoi() converts an array of char (aka a string) into a single integral value. It does not create a list of ints from a string.

    sizeof(list) is also not a valid mechanism to get the number of elements in a list.

    Your basic problem is that you don't even understand basic C and you don't understand the basics of multithreading, but you are trying to use both together.

    Trying to use two things together, when you understand neither, is a recipe for disaster.

    Back off and learn the basics of C first, without any multithreading. That will, realistically, take you a few weeks. Once you've made that start, then start looking at multithreading - you need the C first though to understand what the description of pthread_create() and other things even MEANS.

    Otherwise, you are just wasting your time and also wasting time of people you ask for help.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi-thread programming help
    By Lina_inverse in forum C Programming
    Replies: 1
    Last Post: 02-26-2013, 06:51 PM
  2. multi-thread win32 programming
    By robig2 in forum Windows Programming
    Replies: 22
    Last Post: 01-16-2008, 01:34 PM
  3. Sound/multi thread....
    By CSoFun in forum C++ Programming
    Replies: 5
    Last Post: 01-18-2003, 07:39 PM
  4. Replies: 16
    Last Post: 08-24-2002, 08:44 AM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM

Tags for this Thread