根据硬件并发性将任务划分为线程

时间:2017-04-05 04:19:34

标签: c++ multithreading c++11 concurrency thread-safety

我试图运行这个简单的程序,并想知道为什么输出会出错。代码查询硬件并发,然后尝试启动该数量的线程并执行一些任务。为了完成这项任务,我要写入已调整大小的矢量的各个元素,但结果仍然是错误的 -

#include <iostream>
#include <thread>
#include <vector>
#include <functional>

void myMethod(std::vector<int> &v, int threadNumber) {
    for(int i = threadNumber - 1; i < v.size(); i+= threadNumber) {
        v[i] = threadNumber;
    }
}

int main() {
    const auto numThread = std::thread::hardware_concurrency();
    std::vector<int> vec;
    vec.resize(100, 0);

    if(numThread < 2) {
        std::cout << "Not running\n";
        return 0;
    }

    std::vector<std::thread> vT;
    for(int i = 1; i <= numThread; ++i) {
        vT.emplace_back(myMethod, std::ref(vec), i);
    }
    for(auto &t: vT) { t.join(); }


    for(const auto &i: vec) {
        std::cout << i << ' ';
    }
    std::cout << std::endl;
}

输出为 -

1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4 1 3 1 4 3 2 1 4 1 2 3 4

但我期待 - 1 2 3 4 1 2 3 4 ...

1 个答案:

答案 0 :(得分:1)

Imports MySql.Data.MySqlClient Public Class Form3 Public Myexcel As Microsoft.Office.Interop.Excel.Application Dim completed As Boolean = False Dim rows As New ExcelRows Dim lvitem As ListViewItem Private Structure ExcelRows Dim id As String Dim fname As String Dim lname As String Dim mi As String Dim course As String Dim Year As String Dim no As String Dim picture As String End Structure Private ExcelRowList As List(Of ExcelRows) = New List(Of ExcelRows) Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click OpenFileDialog1.FileName = Nothing If OpenFileDialog1.ShowDialog = DialogResult.OK Then TextBox1.Text = OpenFileDialog1.FileName End If If getinfo() = True Then For Each xitem In ExcelRowList lvitem = ListView1.Items.Add(xitem.no) lvitem.SubItems.AddRange(New String() {xitem.id, xitem.lname, xitem.fname, xitem.course, xitem.Year, xitem.picture}) Next End If End Sub Private Function getinfo() As Boolean Myexcel.Workbooks.Open(Me.TextBox1.Text) Myexcel.sheet("EXPORT").Activate() Myexcel.Range("A3").Activate() Do If Myexcel.ActiveCell.Value > Nothing Or Myexcel.ActiveCell.Text > Nothing Then rows.no = Myexcel.ActiveCell.Value Myexcel.ActiveCell.Offset(0, 1).Activate() rows.id = Myexcel.ActiveCell.Value Myexcel.ActiveCell.Offset(0, 1).Activate() rows.lname = Myexcel.ActiveCell.Value Myexcel.ActiveCell.Offset(0, 1).Activate() rows.fname = Myexcel.ActiveCell.Value Myexcel.ActiveCell.Offset(0, 1).Activate() rows.mi = Myexcel.ActiveCell.Value Myexcel.ActiveCell.Offset(0, 1).Activate() rows.course = Myexcel.ActiveCell.Value Myexcel.ActiveCell.Offset(0, 1).Activate() rows.picture = Myexcel.ActiveCell.Value ExcelRowList.Add(rows) Myexcel.ActiveCell.Offset(1, -6).Activate() Else completed = True Exit Do End If Loop Myexcel.Workbooks.Close() Myexcel = Nothing Return completed End Function End Class 函数中,您增加变量myMethod的数量错误 -

i

实际应该是 -

void myMethod(std::vector<int> &v, int threadNumber) {
    for(int i = threadNumber - 1; i < v.size(); i+= threadNumber) {
        v[i] = threadNumber;
    }
}