Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I use CRITICAL_SECTION or not?
    primarykey
    data
    text
    <p>I have a program which has a Ui with which users choose the way to display and do small configurations. It also has a background procedure, which continuously reads data from the network and update the data to display.</p> <p>Now I put them in one process: background procedure: </p> <pre><code> STATE MainWindow::Rcv() { DeviceMAP::iterator dev; for(dev= dev_map.begin(); dev!= dev_map.end(); dev++) { dev-&gt;second.rcvData();//receive data from the network, the time can be ignored. BitLog* log = new BitLog(); dev-&gt;second.parseData(log); LogItem* logItem = new LogItem(); logItem-&gt;time = QString::fromLocal8Bit(log-&gt;rcvTime.c_str()); logItem-&gt;name = QString::fromLocal8Bit(log-&gt;basicInfo.getName().c_str()); logItem-&gt;PIN = QString::fromLocal8Bit(log-&gt;basicInfo.getPIN().c_str()).toShort(); delete log; add_logItem(logItem); } return SUCCESS; } </code></pre> <p>add_logItem:</p> <pre><code>void MainWindow::add_logItem(LogItem* logItem) { writeToFile(logItem); Device* r = getDevbyPIN(QString::number(logItem-&gt;PIN)); if(r == NULL)return; devInfo_inside_widget::States state = logItem-&gt;state; bool bool_list[portsNum_X]; for(int i =0; i &lt; portsNum_X; i++) { bool_list[i] = 0; } for(int i = 0; i &lt; portsNum; i++) { bool_list[i] = (logItem-&gt;BITS[i/8] &gt;&gt; (7 - i%8)) &amp; 0x1; } r-&gt;refresh(state, logItem-&gt;time, bool_list);//update data inside...state, time , BITS... IconLabel* icl = getIConLabelByDev(r);//update data icl-&gt;refresh(state); logDisplayQueue.enqueue(logItem);//write queue here int size = logDisplayQueue.size(); if(size &gt; 100) { logDisplayQueue.dequeue();//write queue here } } </code></pre> <p>The section above has not dealt with any ui operations yet, but when user push a radio button in the ui, the program has to filter the data in the queue and display it in the table widget: </p> <p>ui operations: </p> <pre><code>void MainWindow::filter_log_display(bool bol) { row_selectable = false; ui-&gt;tableWidget-&gt;setRowCount(0);//delete table items all row_selectable = true; int size_1 = logDisplayQueue.size() - 1; ui-&gt;tableWidget-&gt;verticalScrollBar()-&gt;setSliderPosition(0); if(size_1+1 &lt; 100) { ui-&gt;tableWidget-&gt;setRowCount(size_1 + 1); } else { ui-&gt;tableWidget-&gt;setRowCount(100);//display 100 rows at most } if(bol)//filter from logDisplayQueue and display unworking-state-log rows { int index = 0; for(int queue_i = size_1; queue_i &gt;= 0; queue_i--) { LogItem* logItem = (LogItem*)logDisplayQueue.at(queue_i); // read queue here if(logItem-&gt;state == STATE_WORK || logItem-&gt;state == STATE_UN)continue; QString BITS_str = bits2Hexs(logItem-&gt;BITS); ui-&gt;tableWidget-&gt;setItem(index, 0, new QTableWidgetItem(logItem-&gt;time));//time ui-&gt;tableWidget-&gt;setItem(index, 1, new QTableWidgetItem(logItem-&gt;name));//name ui-&gt;tableWidget-&gt;setItem(index, 2, new QTableWidgetItem(BITS_str));//BITS if(queue_i == oldRowItemNo)ui-&gt;tableWidget-&gt;selectRow(index); index++; } ui-&gt;tableWidget-&gt;setRowCount(index); } else//display all rows { for(int queue_i = size_1, index = 0; queue_i &gt;= 0; queue_i--, index++) { LogItem* logItem = (LogItem*)logDisplayQueue.at(queue_i); //read queue here QString BITS_str = bits2Hexs(logItem-&gt;BITS);// finish = clock(); ui-&gt;tableWidget-&gt;setItem(index, 0, new QTableWidgetItem(logItem-&gt;time));//time ui-&gt;tableWidget-&gt;setItem(index, 1, new QTableWidgetItem(logItem-&gt;name));//name ui-&gt;tableWidget-&gt;setItem(index, 2, new QTableWidgetItem(BITS_str));//BITS if(queue_i == oldRowItemNo)ui-&gt;tableWidget-&gt;selectRow(index); } } } </code></pre> <p>So the queue is quite samll and the background procedure is quite frequent(nearly 500 times per sec). That is, the queue will be written 500 times in 1 sec, but displayed time from time by the user.<br> I want to split the functions into two threads and run them together, one rev and update data, one display.<br> If i do not use any lock or mutex, the user may get the wrong data, but if i force the write-data procedure enter critical section and leave critical section everytime, it will be a heavy overload. :)<br> Should I use CRITICAL_SECTION or something else, any suggestions related?(my words could be verbose for you :) , i only hope for some hints :) </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload