multithreading - C++ 11 Multithread Merge Sort -


i new in c++11 , trying write simple program using thread in c++11. went merge sort, , following code:

#include <iostream> #include <thread> #include <vector> using namespace std;  void merge(vector<int>& vec, int start, int mid, int end) {     vector<int> 1 (vec.begin() + start, vec.begin() + mid + 1);     vector<int> 2 (vec.begin() + mid + 1, vec.begin() + end + 1);      int = 0;     int b = 0;     int index = start;     while (a < one.size() && b < two.size())     {         if (one[a] < two[b])             vec[index ++] = one[a ++];         else              vec[index ++] = two[b ++];     }      // merge left-over elements     while (a < one.size())         vec[index ++] = one[a ++];     while (b < two.size())         vec[index ++] = two[b ++]; }  void merge_sort(vector<int>& vec, int start, int end) { if (start >= end)     return;  int mid = start + (end - start) / 2;  // multi-thread version thread first(merge_sort, vec, start, mid); thread second(merge_sort, vec, mid + 1, end); first.join(); second.join();  /* // single-thread version, testified. merge_sort(vec, start, mid); merge_sort(vec, mid + 1, end);  */  merge(vec, start, mid, end); }   int main() {     int a[] = {4, 2, 5, 9, 7, 1, 3};     vector<int> vec(a, + 7);     merge_sort(vec, 0, 6);     (int = 0; < 7; ++)         cout << vec[i] << endl;     return 0; } 

the underlying algorithm simple: after array split 2 subarrays, 2 threads created take care of subarrays, 1 thread 1 subarray. after joining 2 threads, 2 subarray merged. when tried compiles clang++ 5.1 on macos 10.9.4, following error came out:

applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../lib/c++/v1/thread:332:5: error: attempt use deleted function __invoke(_vstd::move(_vstd::get<0>(__t)), _vstd::move(_vstd::get<_indices>(__t))...); 

then went online , found similar program uses pthread, instead of c++11 thread, same underlying logic. compiled , ran program, , worked. looks this:

#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <iostream> using namespace std;  #define n 2  /* # of thread */  int a[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};  /* target array */  /* structure array index  * used keep low/high end of sub arrays  */ typedef struct arr {     int low;     int high; } arrayindex;  void merge(int low, int high) {         int mid = (low+high)/2;         int left = low;         int right = mid+1;          int b[high-low+1];         int i, cur = 0;          while(left <= mid && right <= high) {                 if (a[left] > a[right])                         b[cur++] = a[right++];                 else                         b[cur++] = a[right++];         }          while(left <= mid) b[cur++] = a[left++];         while(right <= high) b[cur++] = a[left++];         (i = 0; < (high-low+1) ; i++) a[low+i] = b[i]; }  void * mergesort(void *a) {         arrayindex *pa = (arrayindex *)a;         int mid = (pa->low + pa->high)/2;          arrayindex aindex[n];         pthread_t thread[n];          aindex[0].low = pa->low;         aindex[0].high = mid;          aindex[1].low = mid+1;         aindex[1].high = pa->high;          if (pa->low >= pa->high) return 0;          int i;         for(i = 0; < n; i++) pthread_create(&thread[i], null, mergesort, &aindex[i]);         for(i = 0; < n; i++) pthread_join(thread[i], null);          merge(pa->low, pa->high);          //pthread_exit(null);         return 0; }  int main() {         arrayindex ai;         ai.low = 0;         ai.high = sizeof(a)/sizeof(a[0])-1;         pthread_t thread;          pthread_create(&thread, null, mergesort, &ai);         pthread_join(thread, null);          int i;         (i = 0; < 10; i++) printf ("%d ", a[i]);         cout << endl;          return 0; } 

does know wrong program?

for better or worse, std::bind, std::async, , std::thread constructor copy arguments separate storage decouple lifetime current scope. if want pass reference, need wrap in reference_wrapper std::ref (or std::cref const reference):

thread first(merge_sort, std::ref(vec), start, mid); thread second(merge_sort, std::ref(vec), mid + 1, end); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -