race-condition

How to avoid race condition when checking if file exists and then creating it?

十年热恋 提交于 2020-01-01 17:09:25
问题 I'm thinking of corner cases in my code and I can't figure out how to avoid problem when you check if file exists, and if it does not, you create a file with that filename. The code approximately looks like this: // 1 status = stat(filename); if (!status) { // 2 create_file(filename); } Between the call to 1 and 2 another process could create the filename. How to avoid this problem and is there a general solution to this type of problems? They happen often in systems programming. 回答1: You're

Strange behavior of a Java thread associated with System.out [duplicate]

会有一股神秘感。 提交于 2020-01-01 05:39:27
问题 This question already has an answer here : Loop doesn't see value changed by other thread without a print statement (1 answer) Closed 4 years ago . I have a simple TestThreadClientMode class to test a race condition. I tried two attempts: When I run the following code with System.out.println(count); commented in the second thread, the output was: OS: Windows 8.1 flag done set true ... and the second thread was alive forever. Because the second thread never sees change of the done flag which

downloading from AWS S3 while file is being updated

懵懂的女人 提交于 2020-01-01 04:56:05
问题 This may seem like a really basic question, but if I am downloading a file from S3 while it is being updated by another process, do I have to worry about getting an incomplete file? Example: a 200MB CSV file. User A starts to update the file with 200MB of new content at 1Mbps. 16 seconds later, User B starts download the file at 200Mbps. Does User B get all 200MB of the original file, or does User B get ~2MB of User A's changes and nothing else? 回答1: User B gets all 200MB of the original file

How to fix a race condition in node js + redis + mongodb web application

ぃ、小莉子 提交于 2019-12-31 05:23:06
问题 I am building a web application that will process many transactions a second. I am using an Express Server with Node Js. On the database side, I am using Redis to store attributes of a user which will fluctuate continuously based on stock prices. I am using MongoDB to store semi-permanent attributes like Order configuration, User configuration, etc., I am hitting a race condition when multiple orders placed by a user are being processed at the same time, but only one would have been eligible

How do I deal with concurrent changes in a web application?

社会主义新天地 提交于 2019-12-30 06:24:08
问题 Here are two potential workflows I would like to perform in a web application. Variation 1 user sends request server reads data server modifies data server saves modified data Variation 2: user sends request server reads data server sends data to user user sends request with modifications server saves modified data In each of these cases, I am wondering: what are the standard approaches to ensuring that concurrent access to this service will produce sane results? (i.e. nobody's edit gets

How to prevent a race condition when multiple processes attempt to write to and then read from a file at the same time

ε祈祈猫儿з 提交于 2019-12-29 07:48:16
问题 I have the following code (simplified for clarity): import os import errno import imp lib_dir = os.path.expanduser('~/.brian/cython_extensions') module_name = '_cython_magic_5' module_path = os.path.join(lib_dir, module_name + '.so') code = 'some code' have_module = os.path.isfile(module_path) if not have_module: pyx_file = os.path.join(lib_dir, module_name + '.pyx') # THIS IS WHERE EACH PROCESS TRIES TO WRITE TO THE FILE. THE CODE HERE # PREVENTS A RACE CONDITION. try: fd = os.open(pyx_file,

postgresql company id based sequence

不打扰是莪最后的温柔 提交于 2019-12-29 00:44:07
问题 I have a database with companies and their products, I want for each company to have a separate product id sequence. I know that postgresql can't do this, the only way is to have a separate sequence for each company but this is cumbersome. I thought about a solution to have a separate table to hold the sequences CREATE TABLE "sequence" ( "table" character varying(25), company_id integer DEFAULT 0, "value" integer ) "table" will be holt the table name for the sequence, such as products,

mysql insert race condition

痴心易碎 提交于 2019-12-28 12:34:47
问题 How do you stop race conditions in MySQL? the problem at hand is caused by a simple algorithm: select a row from table if it doesn't exist, insert it and then either you get a duplicate row, or if you prevent it via unique/primary keys, an error. Now normally I'd think transactions help here, but because the row doesn't exist, the transaction don't actually help (or am I missing something?). LOCK TABLE sounds like an overkill, especially if the table is updated multiple times per second. The

Working with Slices and Golang sync.Map Structure

孤街浪徒 提交于 2019-12-25 19:37:49
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Working with Slices and Golang sync.Map Structure

末鹿安然 提交于 2019-12-25 19:37:33
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map