C++ 使用线程和排序算法

C++ 使用线程和排序算法,c++,multithreading,sorting,C++,Multithreading,Sorting,我正在尝试将线程与我的两个函数一起使用。在第一个例子中,我试图在.txt文件中写入一些信息,并使用vec.push_-back(str)以填充我的向量,然后在另一个函数SortVec()中对我的录制信息进行排序并在控制台上显示 如果我不使用thread和mutex,我的意思是,如果我只调用main中的那些函数,一切都可以正常工作,不会出错。但是,如果我想使用线程和互斥体来写入一些信息,然后对其进行排序,然后再次写入和排序,并在需要时停止,则会失败 我只在控制台上看到Recording()功能,而

我正在尝试将
线程
与我的两个函数一起使用。在第一个例子中,我试图在
.txt
文件中写入一些信息,并使用
vec.push_-back(str)
以填充我的
向量
,然后在另一个函数
SortVec()
中对我的录制信息进行排序并在控制台上显示

如果我不使用
thread
mutex
,我的意思是,如果我只调用
main
中的那些函数,一切都可以正常工作,不会出错。但是,如果我想使用
线程
互斥体
来写入一些信息,然后对其进行排序,然后再次写入和排序,并在需要时停止,则会失败

我只在控制台上看到
Recording()
功能,而没有
SortVec()
。它的意思是,
std::threadsort(&STring::SortVec,&w)没有被唤醒

你能帮我吗?这有什么不对吗?因为我有点迷路了,不明白为什么会这样

STring.h

#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
#include <mutex>
#include <chrono>

class STring
{
    std::vector<std::string> vec;
    std::mutex mtx;
public:

    STring(){}
    void Recording();
    void SortVec();

};
#include "STring.h"

void STring::Recording()
{
    while (1)
    {
        std::lock_guard<std::mutex> lock(mtx);
        std::string fileName;
        std::string str;
        std::ofstream out("tt1.txt", std::ios_base::app);


        std::cout << "Input smth: ";
        while (std::cin >> str)
        {
            out << str;
            vec.push_back(str);
        }

        std::this_thread::sleep_for(std::chrono::seconds(10));
    }

}

void STring::SortVec()
{
    while (1) 
    {
        std::lock_guard<std::mutex> lock(mtx);
    //  std::vector<std::string>::iterator iter_vec;
        std::sort(vec.begin(), vec.end());


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

        std::this_thread::sleep_for(std::chrono::seconds(5));
    }

}
#include <iostream>
#include "STring.h"
#include <thread>

int main()
{
    STring w;

    std::thread Write(&STring::Recording,&w);
    std::thread Sort(&STring::SortVec, &w);

    Sort.join();
    Write.join();

    system("pause");
    return 0;
}
#pragma一次
#包括
#包括
#包括
#包括
#包括

#包括

我认为在您的情况下使用
std::lock\u-guard
的正确方法是:

void STring::Recording()
{
    while (1)
    {
        // use curly braces to create a nested scope
        {
            std::lock_guard<std::mutex> lock(mtx);
            //
            // your code here...
            //
            // lock_guard releases the lock when it goes out of scope.
        }
        std::this_thread::sleep_for(std::chrono::seconds(10));
    }
}
void字符串::录制()
{
而(1)
{
//使用大括号创建嵌套范围
{
标准:锁和防护锁(mtx);
//
//你的代码在这里。。。
//
//lock_guard在锁超出范围时释放锁。
}
std::this_thread::sleep_for(std::chrono::seconds(10));
}
}

你可能还有其他问题。例如,您的第一个
join()
将永远被阻塞。

您在保持互斥锁的同时将线程置于睡眠状态!那是错的。。。。再一次。。。只是吹毛求疵:
while(1){…}
没有任何正常的退出路径似乎不太好…@WhiZTiM我想,
std::this_thread::sleep_for()
可以帮助我正常退出,不是吗?我该怎么做才对呢?@WhiZTiM我知道,我让一个线程休眠,只有第一个函数工作,但我该怎么做才能让这两个函数都工作?你的程序有比我在这里评论的更多的问题…你考虑过的并行版本(从C++17开始提供)。@Blastfurmace是的,谢谢。是的,我的第一个
join()
已被阻止。是否有一些变量可以唤醒它,或者我应该使用查找其他变量,如condition_variables?@X21您可以尝试一些简单的方法,例如
std::atomic
向线程发出“请完成”的信号。