DataTable Multithreading Operation error Condition

Source: Internet
Author: User
Tags try catch

Recently when writing an HTTP interface with a DataTable this powerful tool, interface with the browser run without any problems. The concurrency problem was not considered at that time, and later, a stress test tool was used to enlarge the concurrency test and 1000+/s the speed test. Found the program error. The program error is the first reaction or to check the code, is not a problem with the code. The discovery logic is all right, then opens the interface with the browser, finds everything OK, and then thinks that it must be caused by multiple threading operations concurrently.

We all know that when different threads access the same resource in multi-threading, the lock method is used to achieve thread synchronization, that is, the same resource can only be manipulated by one thread at a time.

I began by adding the following function to manipulate the DataTable:

1 Object New Object(); 2 3 Lock (Oblock) 4 {5     // My datatable code for the rows Operation 6 }

A confident start to test with stress testing tools. The result is still an error; because it is concurrency you can hardly debug breakpoints with VS, so it's not good to find specific errors somewhere. The specific exception reason and the line of code are then found in the Try catch print error log. The errors are sometimes different, with "internal index corruption", "no rows at location 0", and so on. Add lock also reported wrong to feel very puzzled; When this code is locked, it should not be manipulated by other threads, and it will not cause problems with the index of the row.

Later find some information found, because we usually the operation of the DataTable is basically the rows operation, so I lock on the code inside only locked the datatable rows; but because I didn't operate the columns, So other threads can also access the Columns collection of the DataTable. But the rows are shared by a columns, and the culprit should be the result of not locking columns.

Workaround:

Through Baidu know. NET DataTable.Rows collection. SyncRoot synchronization objects are designed to lock the Rows collection in a DataTable in a multithreaded environment and also lock the Columns collection.

So we just need to add the following in front of the DataTable Code:

 1 // you want to manipulate the Datatable2 datatable  dtb  = new  datatable  (); 3 lock4 {5 Span style= "color: #008000;" >// your datatable processing code 6 }  


Note: Not all DataTable errors are made in multithreaded operations, depending on the situation. However, it is best to add rows.syncroot locks for thread-safe operation under multi-threading.

DataTable Multithreading Operation error Condition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.