簡體   English   中英

vector.push_back() 推送線程時(編譯期間)產生刪除函數錯誤

[英]vector.push_back() produces deleted function error when pushing a thread (during compilation)

我不明白為什么,但vector<thread>.push_back()會產生錯誤。 我嘗試用.emplace_back()替換產生錯誤的行,但我仍然收到此錯誤。

總的來說,我的代碼試圖根據輸入顯示哪些數字是素數。 我確實知道並且已經測試過 splitString() 和 isPrime() 函數可以完美運行,只是我對 main() 的實現不正確並且在編譯時會產生錯誤。

這是我的代碼;

#include <iostream>
#include <string>
#include <cstdlib>
#include <thread>
#include <vector>
using namespace std;
bool isPrime(int number) {
    int scan = number - 1;
    while(scan > 1){
        if(number % scan == 0){
            return false;
        }
        scan = scan - 1;
    }
    return true;
}
vector<string> splitString(string String, string seperator){
    vector<string> result;
    size_t pos = 0;
    string token;
    while((pos = String.find(seperator)) != string::npos){
        token = String.substr(0, pos);
        result.push_back(token);
        String.erase(0, pos + seperator.length());
    }
    result.push_back(String);
    return result;
}
string getPrimes(string param){
    vector<string> splitstring = splitString(param, ",");
    string outstring = "";
    int currentScan = stoi(splitstring[0]);
    int higher = stoi(splitstring[1]);
    while(currentScan <= higher){
        bool res = isPrime(currentScan);
        if(res == true){
            outstring = outstring + to_string(currentScan) + ",";
            cout << to_string(currentScan) + ",";
        }
        currentScan = currentScan + 1;
    }
    return outstring;
}
int main(int argc, char ** argv) {
    int initVal = atoi(argv[0]);
    int threadLength = atoi(argv[1]);
    int threadCount = atoi(argv[2]);
    vector<thread> threads;
    int threadPart = 1;
    while(threadPart <= threadCount){
        int threadStart = (threadPart * threadLength) + initVal;
        int threadEnd = threadStart + threadLength;
        string inp = to_string(threadStart) + "," + to_string(threadEnd);
        thread th(getPrimes, inp);
        threads.push_back(th);
        threadPart = threadPart + 1;
    }
    for(int i = 0; i < threads.size(); i++){
        threads[i].join();
    }
}

這是產生的錯誤;

In file included from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32/bits/c++allocator.h:33,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/allocator.h:46,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/string:41,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/locale_classes.h:40,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/ios_base.h:41,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ios:42,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ostream:38,
                 from C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/iostream:39,
                 from primes.cpp:1:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h: In instantiation of 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::thread; _Args = {std::thread&}; _Tp = std::thread]':
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/alloc_traits.h:475:4:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::thread; _Args = {std::thread&}; _Tp = std::thread; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::thread>]'
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/vector.tcc:103:30:   required from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::thread&}; _Tp = std::thread; _Alloc = std::allocator<std::thread>]'
primes.cpp:55:32:   required from here
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/ext/new_allocator.h:136:4: error: use of deleted function 'std::thread::thread(std::thread&)'
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from primes.cpp:4:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/thread:109:5: note: declared here
     thread(thread&) = delete;
     ^~~~~~

一般來說,我是 C++ 和 C 的新手。 為什么會發生這個錯誤? 我該如何解決?

似乎是因為不允許復制線程。

您可以使用threads.push_back(std::move(th)); 而不是threads.push_back(th); .

另一種選擇是使用

threads.push_back(thread(getPrimes, inp));

代替

thread th(getPrimes, inp);
threads.push_back(th);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM