Вопрос

I am trying to write a parallel bubblesort function. I am running into an error when using boost::bind:

void swap(vector<int>& input, int i, int j)
{
    if (input[i] > input[j])
    {   
        int temp = input[j];
        input[j] = input[i];
        input[i] = temp;
    }   
    return;
}

void parallel_bubblesort(vector<int>& input, int, int)
{

    int i, j, temp;

    for (i = 0; i < input.size(); i++)
    {   
        boost::asio::io_service ioService;
        boost::thread_group threadpool;
        for (j = 0; j < input.size() - 1; j++)
        {   
            ioService.post(boost::bind(&swap, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));
        }   

        for (int t = 0; t < NUM_THREADS; t++)
        {   
            threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
        }   

        threadpool.join_all();
        ioService.stop();
    }   
    return;
}

It snags on the line

ioService.post(boost::bind(&swap, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));

The error says

no matching function for call to ‘bind(<unresolved overloaded function type>, std::vector<int, std::allocator<int> >&, int, int)’

This seems weird, because none of these are overloaded at all, does anybody know what this means?

Edit: Here is the full program

#include <iostream>
#include <chrono>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <boost/thread.hpp>
#include <boost/asio/io_service.hpp>
#define NUM_THREADS 4

using namespace std;
using namespace boost;
using namespace boost::this_thread;

int check_solution(const vector<int>&);
void bubblesort(vector<int>&);
void swap1(vector<int>&, int, int);
void parallel_bubblesort(vector<int>&);

int main()
{
    string line, token;
    string file = "test.txt";
    vector<int> unsorted_integers;

    //print out filename
    cout << file << endl;

    ifstream myfile(file);

    //open file
    if(myfile.is_open())
    {   
        //load everything into a vector
        while (getline(myfile, line))
        {   
            int temp;
            std::istringstream ss(line);
            std::getline(ss, token, '\n');
            temp = atoi(token.c_str());
            unsorted_integers.push_back(temp);
        }   
    }   

    vector<int> unsorted_integers2(unsorted_integers);
 //starts clock
    std::chrono::high_resolution_clock::time_point start2 = std::chrono::high_resolution_clock::now();

    //run the sort function and check for correctness
    parallel_bubblesort(unsorted_integers2);
    if(check_solution(unsorted_integers2))
    {
        cout << "The parallel sort solution is correct" << endl;
    }
    else
    {
        cout << "The parallel sort solution is incorrect" << endl;
    }

    //stops clock and prints times
    std::chrono::high_resolution_clock::time_point end2 = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> total_time2 = std::chrono::duration_cast<std::chrono::duration<double>>(end2 - start2);
    cout << "The parallel sort function took " << total_time2.count() << " seconds." << endl;

    return 0;
}


void swap1(vector<int>& input, int i, int j)
{
    if (input[i] > input[j])
    {
        int temp = input[j];
        input[j] = input[i];
        input[i] = temp;
    }
    return;
}

void parallel_bubblesort(vector<int>& input, int, int)
{

    int i, j;

    for (i = 0; i < input.size(); i++)
      {
        boost::asio::io_service ioService;
        boost::thread_group threadpool;
        for (j = 0; j < input.size() - 1; j++)
        {
            ioService.post(boost::bind(&swap1, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));
        }

        for (int t = 0; t < NUM_THREADS; t++)
        {
            threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
        }

        threadpool.join_all();
        ioService.stop();
    }
    return;
}

int check_solution(const vector<int>& solution)
{
    vector<int> correct_solution(solution);
    std::sort(correct_solution.begin(), correct_solution.end());
    return (solution == correct_solution);
}
Это было полезно?

Решение

You are probably doing using namespace std; somewhere, and std::swap is a thing. I suggest you simply rename your swap function to something else.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top