1

I tried to make a bitonic sorter using std::tread and std::condition_variable. Something goes wrong and I don't know what.

I get error message: undefined reference to 'pthread_create'

/opt/lintula/gcc/include/c++/10.3.0/thread:142: error: undefined reference to pthread_create' main.o: In function std::thread::thread<void (&)(int*, int*), int*&, int*&, void>(void (&)(int*, int*), int*&, int*&)': /opt/lintula/gcc/include/c++/10.3.0/thread:142: undefined reference to `pthread_create'

#include <iostream>
#include <array>
#include <vector>
#include <cmath>
#include <thread>
#include <mutex>
#include <condition_variable>

int arr[] = {};

std::condition_variable cond;
std::mutex mtx;
bool reserved = false;

void swap_items(int *ptr_h, int *ptr_l)
{
   std::swap(arr[*ptr_h], arr[*ptr_l]);
}

bool is_value_valid(int n) {
   if ( n < 0 ) {
      return false;
   }
   return true;
}

int main()
{
int n;
std::cout << "Please enter an integer value: ";
std::cin >> n;

while ( !is_value_valid(n) ) {
   std::cout << "Value must be positive!" << std::endl;
   std::cout << "Please enter an integer value: ";
   std::cin >> n;
}

int arr_l = pow(2,n);

int *ptr_h;
int *ptr_l;

const unsigned NUM_OF_THREADS = arr_l / 2;

std::vector<std::thread> threads;

for ( unsigned i = 0; i < NUM_OF_THREADS; i++ ) {
   threads.push_back(std::thread(swap_items, ptr_h, ptr_l));
}

arr[arr_l];

for ( int i = 0; i < arr_l; i++ ) {
   arr[i] = rand() % 100;
}

std::cout << "Original list: ";
for ( int i = 0; i < arr_l; i++ ) {
   std::cout << arr[i] << ",";
}
std::cout << std::endl;

// BITONIC SORTER
for ( int k = 2; k <= arr_l; k *= 2 ) {

   for ( int j = k/2; j > 0; j /= 2 ) { 
   //is halved at every iteration, with truncation of fractional parts

      for ( int h = 0; h < arr_l; h++ ) {
         int l = (h^j);

         if ( l > h ) {

            if ( ( ((h&k) == 0) && (arr[h] > arr[l]) ) || ( ((h&k) != 0) && (arr[h] < arr[l]) ) ) {
               std::unique_lock<std::mutex> lck(mtx);

               while (!reserved) {
                  cond.wait(lck);
               } 
               ptr_h = &h;
               ptr_l = &l;

               threads[h].join();
               }
            }
         }
         std::unique_lock<std::mutex> lck(mtx);
         reserved = true;
         cond.notify_all();
      }
   }

   // print sorted list
   std::cout << "Sorted list: ";
   for ( int i = 0; i < arr_l; i++ ) {
      std::cout << arr[i] << ",";
   }
   std::cout << std::endl;

   return EXIT_SUCCESS;
}
kimara
  • 11
  • 2
  • 1
    So, does it work with threads if you don't do any sorting? Please, if things go wrong, reduce the scope of your problem first. Then, search for the error message. I have a feeling what's wrong, and I'm sure you could find according solutions easily. As a new user here, please also read [ask] and take the [tour]. – Ulrich Eckhardt May 19 '22 at 16:22
  • add `-lpthread` to your build: https://godbolt.org/z/Gvxn6obao – Marek R May 19 '22 at 16:23
  • @MarekR I believe `-pthread` is preferred, https://stackoverflow.com/q/23250863/2752075 – HolyBlackCat May 19 '22 at 16:56

0 Answers0