简体   繁体   English

在C ++中结束函数

[英]Ending a function in C++

I have a C++ function called write() that is supposed to write a large number of bytes on the users HDD. 我有一个名为write()的C ++函数,该函数应该在用户HDD上写入大量字节。 This woud normally take more than 30 seconds, so I want to give the user the ability to Abort operation from a GUI (created with Qt). 这通常需要30秒钟以上,因此我想为用户提供从GUI(使用Qt创建) Abort operation的功能。

So I have the button generate_button . 所以我有按钮generate_button When this button is clicked() , I want to have something to end the function, wherever its progress is. clicked()此按钮时,无论其进度在哪里,我都希望有一些东西可以结束该函数。

I thought about threads, but I am not sure. 我考虑过线程,但不确定。 Can you advice me please? 你能给我建议吗?

I would probably use a thread. 我可能会使用一个线程。 It should be quite simple to check a variable to see if the operation has been canceled. 检查变量以查看操作是否已取消应该非常简单。

Use a mutex to lock access to your cancel variable. 使用互斥锁来锁定对您的cancel变量的访问。 That will make sure it is read and written in a proper way for multiple threads. 这样可以确保以正确的方式对多个线程进行读写。 Another option is if you are using C++11 use an atomic variable. 另一个选择是,如果您使用的是C ++ 11,请使用原子变量。

Break your large write into blocks of smaller size. 将较大的写操作分成较小的块。 8 to 64 kilobytes should work. 8至64 KB应该可以工作。 After writing each block check your cancel variable and if set, exit the thread. 写入每个块后,检查您的cancel变量,如果已设置,请退出线程。

Place the code that actually does the writing in a worker thread. 将实际进行编写的代码放在工作线程中。 Have a shared variable (one that is either atomic, or protected by a mutex). 具有一个共享变量(一个原子的或受互斥锁保护的变量)。 Have the worker thread check its value each iteration. 让工作线程在每次迭代时检查其值。 If the user presses the "Abort" button, set the value for the variable. 如果用户按下“中止”按钮,请设置变量的值。

You should use threads if this is a long running operation. 如果这是长期运行的操作, 则应使用线程。

Since you are using C++11, std::atomic<bool> would probably serve you well. 由于您使用的是C ++ 11,因此std::atomic<bool>可能会为您提供std::atomic<bool>服务。

Threaded guarantees that you will have a responsive GUI. 线程保证您将拥有响应式GUI。 But there is a learning curve to using a thread in this manner. 但是以这种方式使用线程有一个学习曲线。

A threadless way to do this is to have in your routine that writes to the harddrive in the GUI thread, but gives time to the GUI thread to stay responsive. 一种无线程的方法是在您的例程中将其写入GUI线程中的硬盘驱动器,但给GUI线程留出时间以保持响应。

QObject::connect(my_cancel_button, SIGNAL(clicked()), file_writer, SLOT(setCanceled()));

// open file for writing
QFile file("filename.txt");
file.open(//... );//

while(still_have_data_to_write && !canceled)
{
    write( <1 MB of data> ); // or some other denomination of data

    qApp->processEvents();// allows the gui to respond to events such as clicks on buttons

    // update a progress bar... using a counter as a ratio of the total file size
    emit updateProgressBar(count++);
}

if( canceled )
{
     file.close();
     // delete the partial file using QDir
}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM