Fortsätt till huvudinnehåll

Inlägg

Visar inlägg från maj, 2017

Some reflections on time consumption creating new threads

Today I wrote some tests with the goal to verify thread safety on a part of production code. Using TDD I wrote a test that started two new threads that both updated the same object, and while they were running I verified the object's data. By putting this in a loop I was able to get a unit test that failed on each run. By adding locks at the correct places I got the test to pass. But looking at the time it took to run the test I noticed that it was quite slow. I knew that the updates to the object's data were fast and started wondering if it was the start and join of the threads that were slowing it down. So, instead of using new Thread(task) I switched to using Task.Run(task) and task.Wait() instead of thread.Join() . The main difference here being task adding a job to a pool of worker threads while new Thread creates a completely new thread that is stopped and disposed after it has run to completion. By doing this small change the test time decreased by 25% (from 80

Some database theory

For reasons at work I am currently looking into how storing and retrieving data from databases works. Was introduced to an abbrivation that I didn't know of earlier, ACID. ACID is a combination of Atomicity, Consistency, Isolation, and Durability. Atomicity - requires each transaction to be "all or nothing". A transaction may consist of several writes to the database, however, if any of these fail, or is interrupted, the entire transaction must be invalidated in order to have the database in a valid state. Consistency - requires that any transaction will bring the database from one valid state to another. No transactions may currupt the database. Isolation - ensures that concurrent transactions results in a system state that would be obtained if they were executed sequentially . Read that sentence again, it is a tricky one. Be able to perform concurrent transactions in a way that the result is like they have been done sequentially... Durability - ensures that a tra