Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. threads ( c language ) in Ubunt

threads ( c language ) in Ubunt

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eng zeyad
    wrote on last edited by
    #1

    how to program that uses 4 threads to compute the sum of an array of 1000 integers.

    D CPalliniC A 3 Replies Last reply
    0
    • E Eng zeyad

      how to program that uses 4 threads to compute the sum of an array of 1000 integers.

      D Offline
      D Offline
      den2k88
      wrote on last edited by
      #2

      Well thanks but I've already did my homeworks, you could do it as well. From http://www.codeproject.com/Messages/2922875/HOW-TO-ASK-A-QUESTION.aspx[^]:

      2. Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
      ...
      11. If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.

      E 1 Reply Last reply
      0
      • D den2k88

        Well thanks but I've already did my homeworks, you could do it as well. From http://www.codeproject.com/Messages/2922875/HOW-TO-ASK-A-QUESTION.aspx[^]:

        2. Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
        ...
        11. If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.

        E Offline
        E Offline
        Eng zeyad
        wrote on last edited by
        #3

        my question is how to create an array using threads . i have posted all the code . but 1 thing is missing is how to implement thread in the array . regards

        1 Reply Last reply
        0
        • E Eng zeyad

          how to program that uses 4 threads to compute the sum of an array of 1000 integers.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          "POSIX Threads Programming"[^].

          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

          In testa che avete, signor di Ceprano?

          E 2 Replies Last reply
          0
          • CPalliniC CPallini

            "POSIX Threads Programming"[^].

            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

            E Offline
            E Offline
            Eng zeyad
            wrote on last edited by
            #5

            #include
            <stdio.h>
            #include
            <stdlib.h>

            // The array that holds the data
            int array[1000]
            ;

            void testSum()
            {
            int sum=0;

            int j;

            for(j=0
            ; j< 1000 ; j++)

            {
            sum+=array[j];

            } 
            
               printf("Testing without threads, Sum is : %d \\n",sum); 
            

            }

            // This function reads a file with 1000 integers,
            an integer is stored in
            // each line.The function stores the integers in the array

            void readfile(char* file_name)

            {

            char ch;

            FILE *fp;

            fp = fopen(file_name,"r"); // read mode

            if( fp == NULL )
            {
            perror("Error while opening the file.\n");

             exit(EXIT\_FAILURE);
            

            }

            char line \[5\];
            

            /* line size */

            int i=0;
            

            printf("Reading file: ");

            fputs(file\_name,stdout);
            
            printf("\\n");
            
            
            while ( fgets ( line, sizeof line, fp) != NULL ) /\* read a line \*/
            {
               if (i < 1000)
               {
                 array\[i\]=atoi(line); 
            
               }
            
                //debug code
                //fputs ( line, stdout ); /\* write the line \*/
              i++;
            

            }

            // debug code 
            //printf("i is : %d \\n",i);
            

            fclose(fp);

            printf("Reading file Complete, integers stored in array.\\n\\n"); 
            

            }

            int main(int argc, char* argv[])
            {

            if (argc != 2) {
            fprintf(stderr,"usage: a.out <file name>\n");

            /\*exit(1);
            

            */
            return -1;

            }

            readfile(argv[1]);

            //Debug code for testing only
            testSum();

            return 0;

            }

            1 Reply Last reply
            0
            • CPalliniC CPallini

              "POSIX Threads Programming"[^].

              THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

              E Offline
              E Offline
              Eng zeyad
              wrote on last edited by
              #6

              i don't want to bother you ! ,

              1 Reply Last reply
              0
              • E Eng zeyad

                how to program that uses 4 threads to compute the sum of an array of 1000 integers.

                A Offline
                A Offline
                Albert Holguin
                wrote on last edited by
                #7

                0. Split your array into four parts (use the pointer to determine where you're doing the split, don't make another copy of the data) 1. Create worker threads to sum each with their portion (pointer to the beginning and size). 2. Wait for all threads to finish, sum their results. If this was a program that needed to do this over and over again, I'd also create a "thread pool" that is precreated. Creating threads on the fly is actually a pretty slow process, you want to do it sparingly.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups