Like Tree4Likes
  • 1 Post By Salem
  • 3 Post By grumpy

[C] Basic multi-thread programming help

This is a discussion on [C] Basic multi-thread programming help within the C Programming forums, part of the General Programming Boards category; I want to create 2 threads, one does the max and one gives the average of a list of numbers ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    [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.


    Code:
    #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()
        {
        
          printf("\n");
        
        }
        
        int main()
        {
          int average;
          int min;
          int max;
        
          int how_many;
          int i;
          int status;
          pthread_t tid1,tid2;
        
          printf("How many numbers?: ");
          scanf("%d",&how_many);
          int ar[how_many];
          printf("Enter the list of numbers: ");
          for (i=0;i<how_many;i++){
          scanf("%d",&ar[i]);
          }
        
        //for(i=0;i<how_many;i++)
        //printf("%d\n",ar[i]);
        
                pthread_create(&tid1,NULL,thread1(how_many,ar),NULL);
                pthread_create(&tid2,NULL,thread2,NULL);
                pthread_join(tid1,NULL);
                pthread_join(tid2,NULL);
                return 0;
          exit(0);
        }
    I just made the first thread, which is to print out the min. number, but I have the following errors when compiling:




    Code:
    How many numbers?: 3
        Enter the list of numbers: 1
        2
        3
        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
    Posts
    4,415
    How should I go on and fix the seg. fault?
    O_o

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

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

    Soma

    Multi-thread programming help

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    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.
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    Code:
        #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;
        }
        
        pthread_attr_init(&attr);
        
        pthread_create(&tid,&attr,runner,argv[1]);
        
        pthread_join(tid,NULL);
        
        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]
        
        pthread_exit(0);
        }
    Could you tell me how to store a list of numbers entered by the user, separated by a space


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

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    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
    Code:
    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.
    stahta01, Salem and AndiPersti like this.
    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, Sunshine, 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, 09: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21