Reading Cell Values in Multiple Threads Simultaneously with C++
Contents
[
Hide
]
Needing to read cell values in multiple threads simultaneously is a common requirement. This article explains how to use Aspose.Cells for this purpose.
To read cell values in more than one thread simultaneously, set Worksheet.GetMultiThreadReading() to true. If you do not, you might get the wrong cell values.
The following code:
- Creates a workbook.
- Adds a worksheet.
- Populates the worksheet with string values.
- It then creates two threads that simultaneously read values from random cells. If the values read are correct, nothing happens. If the values read are incorrect, then a message is displayed.
If you comment this line:
testWorkbook->get_Worksheets()->Get(0)->get_Cells()->set_MultiThreadReading(true);
then the following message is displayed:
if (s != "R" + row + "C" + col)
{
MessageBox::Show("This message box will show up when cells read values are incorrect.");
}
Otherwise, the program runs without showing any message which means all values read from cells are correct.
#include <iostream>
#include <thread>
#include <random>
#include <chrono>
#include <string>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace std;
static Workbook testWorkbook;
U16String IntToU16String(int value) {
wstring ws = to_wstring(value);
return U16String(reinterpret_cast<const char16_t*>(ws.c_str()));
}
void ThreadLoop()
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> rowDist(0, 9999);
uniform_int_distribution<> colDist(0, 99);
while (true)
{
try
{
int row = rowDist(gen);
int col = colDist(gen);
U16String s = testWorkbook.GetWorksheets().Get(0).GetCells().Get(row, col).GetStringValue();
U16String expected = U16String(u"R") + IntToU16String(row) + U16String(u"C") + IntToU16String(col);
if (s != expected)
{
cout << "This message will show up when cells read values are incorrect." << endl;
}
}
catch (...) {}
}
}
void TestMultiThreadingRead()
{
testWorkbook = Workbook();
testWorkbook.GetWorksheets().Clear();
testWorkbook.GetWorksheets().Add(u"Sheet1");
for (int row = 0; row < 10000; row++)
{
for (int col = 0; col < 100; col++)
{
U16String value = U16String(u"R") + IntToU16String(row) + U16String(u"C") + IntToU16String(col);
testWorkbook.GetWorksheets().Get(0).GetCells().Get(row, col).SetValue(value);
}
}
// Commenting this line will show a pop-up message
// testWorkbook.GetWorksheets().Get(0).GetCells().SetMultiThreadReading(true);
thread myThread1(ThreadLoop);
thread myThread2(ThreadLoop);
this_thread::sleep_for(chrono::seconds(5));
myThread1.detach();
myThread2.detach();
}
int main()
{
Aspose::Cells::Startup();
TestMultiThreadingRead();
Aspose::Cells::Cleanup();
return 0;
}