在类的公共函数中使用多线程

时间:2017-01-08 21:59:35

标签: c++ multithreading visual-studio c++11

我是c ++线程的新手,我不知道如何删除我在visual studio中的错误。它给出了2个错误,如下所示: 1)C2672:'的std ::调用'找不到匹配的重载函数。 2)C2893:无法专门化函数类型' unknown-type std :: invoke(_Callable&&,_ Typers&& ...)'

这是我的代码:

#include <iostream>
#include <string>
#include <thread>
using namespace std;
struct q_node {
    q_node *previous;
    q_node *next;
    string data;

};
class q {
private:
    q_node * aa;
    q_node * bb;
    q_node * ans;
    q_node *front;
    q_node *current;
    int element;
    int counter1;
    int counter2;
public:
    q() {
        front = NULL;
        current = NULL;
        element = 0;
        aa = NULL;
        bb = NULL;
    }
    void enque(string team) {
        q_node *t = new q_node;
        t->previous = NULL;
        t->next = NULL;
        t->data = team;
        if ((front == NULL) && (current == NULL) && (element == 0)) {
            front = t;
        }
        else {
            current->next = t;
            t->previous = current;
        }
        element++;
        current = t;
    }
    void insert_at_index(int n, string x) {
        q_node * t = new q_node;
        t->previous = NULL;
        t->next = NULL;
        t->data = x;
        q_node *temp, *temp2;
        temp = front;
        temp2 = temp->next;
        if (n == 1) {
            t->next = temp;
            temp->previous = t;
            front = t;
        }
        else {
            for (int i = 0; i < element - 1; i++) {
                if (i == (n - 2)) {
                    temp->next = t;
                    t->previous = temp;
                    t->next = temp2;
                    temp2->previous = t;
                    temp2 = front;
                }
                temp = temp->next;
                temp2 = temp2->next;
            }

        }
        element++;
    }

    void deque() {
        q_node *temp = front;
        front = front->next;
        front->previous = NULL;
        temp->next = NULL;
        element--;
        delete temp;
    }
    void display() {
        q_node *t = front;
        for (int i = 0; i < element; i++) {
            cout << " " << t->data << " ";
            t = t->next;
        }
    }
    inline void checking() {                                 //decide values for both integer e.g for 9 (4,5)
        if ((element % 2) == 0) {
            counter1 = ((element / 2) - 1);
            counter2 = ((element / 2) + 1);
        }
        else {
            counter1 = ((element / 2));
            counter2 = ((element / 2) + 1);
        }
    }
    void search_first(string x) {               //transverse from start
        q_node *t = front;
        int i = 0;
        for (; i < counter1; i++) {
            if (t->data == x) {
                bb= t;
                break;
            }
            t = t->next;
        }
        if (i == counter1) {
            bb= NULL;
        }
    }
    void search_last(string x) {                    //transverse from end
        q_node *t = current;
        int i = 0;
        for (; i < counter2; i++) {
            if (t->data == x) {
                aa= t;
            }
            t = t->previous;
        }
        if (i==counter2) {
            aa = NULL;
        }
    }
    void get_search(string aaa) {

        thread t(&q::search_first, aaa);
        thread tt(&q::search_last, aaa);
        t.join();
        tt.join();
        if (aa != NULL&&bb != NULL) {
            if (aa != NULL) {
                ans = aa;
            }
            else {
                ans = bb;
            }
        }
        else {
            ans = NULL;
        }
    }
    void smart_insertion(string a) {
        get_search(a);
        if (ans!=NULL) {
            q_node * t = new q_node;
            t->data = a;
            t->previous = ans;
            t->next = (ans->next);
            ans->next = t;
            (ans->next)->previous = t;
        }
        else {
            enque(a);
        }
    }
};

我正在尝试使用2个线程从两侧读取链接列表,以便提高整体速度。这里的队列是使用双向链表实现的。有人可以帮我解决这些错误。

1 个答案:

答案 0 :(得分:0)

要在非静态成员函数上调用线程,您需要向其传递指向将运行它的对象的指针。 this就是这样:

    thread t(&q::search_first, this, aaa);
    thread tt(&q::search_last, this, aaa);

当然我不承诺你的代码能够实现你正在做的事情,但是这会让你超越这个错误并且代码会编译。