【问题标题】:C++ multithreading interruptionsC++ 多线程中断
【发布时间】:2016-02-14 15:28:20
【问题描述】:

正如您在主函数中看到的,我创建了一组线程,它们执行完全相同的函数但具有不同的参数。该函数只是打印出向量的值。现在的问题是这些线程相互干扰。我的意思是一个线程在另一个线程开始之前没有完成打印(cout),它就像 sdkljasjdkljsad。我想要某种混乱的顺序,例如:

Thread 1 Vector[0]
Thread 2 Vector[0]
Thread 1 Vector[1]
Thread 3 Vector[0]
Thread 4 Vector[0]
Thread 2 Vector[1]

而不是:

Thread 1 Thread 2 Vector[0] Vector[0]
Thread 2 Vector[1]
Thread 1 Thread 4 Vector[1] Thread 3 Vector[0] Vector[1]

我该如何解决这个问题?附言数据文件只是球员姓名、体重和每行卧推的列表。将这些转换为字符串并放入向量中(是的,听起来很愚蠢,但我只是在完成一项任务)。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <string>
#include <thread>
#include <sstream>
#include <iomanip>
#include <boost/thread.hpp>
#include <boost/bind.hpp>


using namespace std;

 vector<string> Kategorijos;
 vector< vector<string> > Zaidejai;
 ifstream duom("duom.txt");

 string precision(double a) {
     ostringstream out;
     out << setprecision(6) << a;
     return out.str();
 }

void read() {
    string tempKat;
    int tempZaidSk;
    vector<string> tempZaid;

    string vardas;
    int svoris;
    double pakeltasSvoris;
    while (duom >> tempKat >> tempZaidSk) {

        Kategorijos.push_back(tempKat);
        for (int i = 0; i < tempZaidSk; i++) {

            duom >> vardas >> svoris >> pakeltasSvoris;
            tempZaid.push_back(vardas + " " + to_string(svoris) + " " + precision(pakeltasSvoris));
        }
        Zaidejai.push_back(tempZaid);
        tempZaid.clear();
    }
    duom.close();
}


void writethreads(int a) {
    int pNr = a+1;
    for (int i = 0; i < (int)Zaidejai[a].size(); i++) {
        cout << endl << "Proceso nr: " << pNr << " " << i << ": " << Zaidejai[a][i] ;
    }
}

void print() {
    for (int i = 0; i < (int)Kategorijos.size(); i++) {
        cout << "***   " << Kategorijos[i] << "   ***" << endl;
        for (int j = 0; j < (int)Zaidejai[i].size();  j++) {
            cout << j+1<<") "<< Zaidejai[i][j] << endl;
        }
        cout << endl;
    }
    cout << "-------------------------------------------------------------------" << endl;
}

int main()
{   
    read();
    print();

    boost::thread_group threads
        ;
    for (int i = 0; i < (int)Kategorijos.size(); i++) {

        threads.create_thread(boost::bind(writethreads, i));

    }

    threads.join_all();

    system("pause");
    return 0;
}

【问题讨论】:

  • 我必须使用线程以及控制台,这些是任务的要求。我用 Java 做过同样的事情,唯一的区别是我一个接一个地创建了 5 个独立的线程,而且效果很好。但是,这有点取决于情况,因为我知道数据文件中有 5 个数据块。使用 c++,我想确保可以在不接触代码的情况下更改数据文件,这就是我使用 for 循环和线程组的原因。

标签: c++ multithreading c++11


【解决方案1】:

欢迎关注线程同步问题!当一次只有一个线程可以使用资源时,用于控制该资源的锁是互斥锁。您还可以存储一个线程的数据以在最后输出,或者您可以让线程在屏障处同步。

【讨论】:

  • 那里有错字。希望投反对票的人将其删除。
【解决方案2】:

控制台写道,您可以使用适当的互斥锁来同步它们。但在这种情况下,使用控制台输出,可能根本不使用线程。否则将打印发送到处理它的专用线程。

使用通常的cout 重载operator &lt;&lt; 的替代方法是将内容写入本地缓冲区或stringsteam(包括新行),然后通过单个函数调用将其写入控制台。单个函数调用将帮助控制台编写器一次只写入一个缓冲区的内容。

【讨论】:

  • “将内容写入本地缓冲区并一次调用转发”是最好的解决方案。让操作系统负责锁定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多