title: pthread and multicore on windows
body: <p>my question relates to the pthread library and making use of the multicore system. The system seems to improve under the proper parameters and for small data sizes, the most improvement being around 65000. The data suggests that when you increase the threads it begins to decrease the time it takes but then increases shortly afterward. when thread number = 1,2,4 it might slowly increase and sometimes 8, but then 16 the time begins decreasing back again. In large data sizes there is no improvement and all the times remain fairly close together. 
  If someone could tell me if something is forcing my threads to act sequentially or another issue that would be awesome.</p>
  
  <p>heres the data</p>
  
  <pre><code>1395525080 0 num thread: 1 data size: 1024 0 1395525080
  1395525080 0 num thread: 2 data size: 1024 0 1395525080
  1395525080 0 num thread: 4 data size: 1024 0 1395525080
  1395525080 15 num thread: 8 data size: 1024 0 1395525080
  1395525080 47 num thread: 16 data size: 1024 0 1395525080
  1395525080 31 num thread: 32 data size: 1024 0 1395525080
  1395525080 16 num thread: 1 data size: 4096 0 1395525080
  1395525080 0 num thread: 2 data size: 4096 0 1395525080
  1395525080 0 num thread: 4 data size: 4096 0 1395525080
  1395525080 15 num thread: 8 data size: 4096 0 1395525080
  1395525080 78 num thread: 16 data size: 4096 0 1395525080
  1395525080 31 num thread: 32 data size: 4096 0 1395525080
  1395525080 140 num thread: 1 data size: 65536 0 1395525080
  1395525081 156 num thread: 2 data size: 65536 0 1395525081
  1395525081 109 num thread: 4 data size: 65536 0 1395525081
  1395525081 94 num thread: 8 data size: 65536 0 1395525081
  1395525081 93 num thread: 16 data size: 65536 0 1395525081
  1395525081 187 num thread: 32 data size: 65536 0 1395525082
  1395525082 171 num thread: 1 data size: 75536 0 1395525082
  1395525082 172 num thread: 2 data size: 75536 0 1395525082
  1395525082 141 num thread: 4 data size: 75536 0 1395525083
  1395525083 109 num thread: 8 data size: 75536 0 1395525083
  1395525083 140 num thread: 16 data size: 75536 0 1395525083
  1395525083 234 num thread: 32 data size: 75536 0 1395525084
  1395525084 203 num thread: 1 data size: 85536 0 1395525084
  1395525084 203 num thread: 2 data size: 85536 0 1395525084
  1395525084 172 num thread: 4 data size: 85536 0 1395525085
  1395525085 202 num thread: 8 data size: 85536 0 1395525085
  1395525085 125 num thread: 16 data size: 85536 0 1395525085
  1395525085 187 num thread: 32 data size: 85536 0 1395525086
  1395525086 125 num thread: 1 data size: 55536 0 1395525086
  1395525086 109 num thread: 2 data size: 55536 0 1395525086
  1395525086 141 num thread: 4 data size: 55536 0 1395525086
  1395525086 78 num thread: 8 data size: 55536 0 1395525086
  1395525087 140 num thread: 16 data size: 55536 0 1395525087
  1395525087 156 num thread: 32 data size: 55536 0 1395525087
  1395525120 153271 num thread: 1 data size: 70000000 153 1395525274
  1395525398 152630 num thread: 2 data size: 70000000 152 1395525551
  1395525675 154846 num thread: 4 data size: 70000000 154 1395525830
  1395525956 153988 num thread: 8 data size: 70000000 153 1395526110
  1395526236 153956 num thread: 16 data size: 70000000 153 1395526390
  1395526515 157935 num thread: 32 data size: 70000000 157 1395526673
  </code></pre>
  
  <p>heres the code, it does a traditional bucket sort, i have two other similar ones with similar data that also do bucket sorts, the sequential code generates almost exactly the same values. </p>
  
  <pre><code>struct bucket
  {
      std::vector&lt;int&gt; data;
  
  } ;
  
  
  void *sort_bucket(void *unsorted_bucket);
  int _tmain(int argc, _TCHAR* argv[])
  {
      int array_N[] = {1024, 4096, 65536,75536,85536,55536, 70000000, 16777216};
      int array_number_of_threads[] = {1, 2, 4, 8, 16, 32};
      std::vector&lt;int&gt; N;
      std::vector&lt;int&gt; number_of_threads;
      number_of_threads.assign(array_number_of_threads, array_number_of_threads+6);
      N.assign(array_N, array_N+7);
  
      for(int size_index = 0; size_index &lt; N.size(); size_index++)
      {
          for(int thread_index = 0; thread_index &lt; number_of_threads.size(); thread_index++)
          {
              std::vector&lt;int&gt; unsorted_data;
              std::vector&lt;int&gt; sorted_data;
              std::vector&lt;std::thread&gt; thread_array;
              std::vector&lt;bucket&gt; buckets;
  
              std::vector&lt;pthread_t&gt; thread;
  
              while(buckets.size() &lt; number_of_threads[thread_index]){ // checks against the number of threads and creates the number of buckets
                  bucket new_bucket;
                  pthread_t new_thread;
                  buckets.push_back(new_bucket);
                  thread.push_back(new_thread);
              }
  
              for(int index = 0; index &lt; N[size_index]; index++) // gathers the data
              {
                  unsorted_data.push_back(rand() % N[size_index]);
              }
  
              clock_t t = 0;
              t = clock();
              time_t start = 0;
              time_t end = 0;
  
              time(&amp;start);
              std::cout &lt;&lt; start &lt;&lt; " ";
  
              int difference = N[size_index]/number_of_threads[thread_index];
              int placeholder = 0;
              for(int index = 0; index &lt; N[size_index]; index++) {//calculates which bucket the data belong in and places the data in that bucket
                  //std::cout &lt;&lt; unsorted_data[index] &lt;&lt; " " &lt;&lt; difference &lt;&lt; " ";
                  placeholder = unsorted_data[index]/difference;
                  //std::cout &lt;&lt; placeholder &lt;&lt; std::endl;
                  buckets[placeholder].data.push_back(unsorted_data[index]);
              }
              for(int index = 0; index &lt; number_of_threads[thread_index]; index++){ // sends the data to the threads
                  //thread_array.push_back(std::thread(sort_bucket ,buckets[index]));
                  pthread_create(&amp;thread[index],
                                 NULL,
                                 sort_bucket ,
                                 (void*) &amp;buckets[index].data);
              }
              // bring the data back to the root process
              for(int index = 0; index &lt; number_of_threads[thread_index]; index++)        {
                  void *data;
                  struct bucket *ret_bucket;
                  pthread_join(thread[index],(void**) &amp;data);
                  ret_bucket = (struct bucket *) data;
                  sorted_data.insert(sorted_data.end(), ret_bucket-&gt;data.begin(), ret_bucket-&gt;data.end());
                  //sorted_data.assign(ret_bucket-&gt;data.begin(), ret_bucket-&gt;data.end());
              }
              /*
               for(int index = 0; index &lt; sorted_data.size(); index++)
               {
               std::cout &lt;&lt; sorted_data[index] &lt;&lt; " ";
               }
               */
  
              t = clock() - t;
              std::cout &lt;&lt; t &lt;&lt; " ";
              t = t/CLOCKS_PER_SEC;
              std::cout &lt;&lt; "num thread: " &lt;&lt; number_of_threads[thread_index] &lt;&lt; " ";
              std::cout &lt;&lt; "data size: " &lt;&lt; N[size_index] &lt;&lt; " ";
              std::cout &lt;&lt; t &lt;&lt; " ";
              time(&amp;end);
              std::cout &lt;&lt; end &lt;&lt; std::endl;
  
  
  
              sort(unsorted_data.begin(), unsorted_data.end());
  
              for(int index = 0; index &lt; unsorted_data.size(); index++)
              {
                  if(unsorted_data[index] != sorted_data[index])
                  {
                      std::cout &lt;&lt; "data sorting failed" &lt;&lt; std::endl;
                  }
              }
          }
      }
      int placeholder;
      std::cin &gt;&gt; placeholder;
      return 0;
  }
  
  void *sort_bucket(void *unsorted_bucket)
  {  
      bucket *temp_sorted_bucket = (struct bucket *) unsorted_bucket;  
      std::sort(temp_sorted_bucket-&gt;data.begin(), temp_sorted_bucket-&gt;data.end()); 
  
      /*for(int index = 0; index &lt; temp_sorted_bucket-&gt;data.size(); index++)
       {
       std::cout &lt;&lt; temp_sorted_bucket-&gt;data.at(index) &lt;&lt; " ";
       }*/
      pthread_exit(temp_sorted_bucket);
      return 0; 
  }
  </code></pre>