race-condition

Does writing the same value to the same memory location cause a data race?

我怕爱的太早我们不能终老 提交于 2019-12-05 12:37:10
问题 Consider the following code that writes the same value to the same memory location from multiple threads: void f(int* buf, int n, int* p) { for(int i = 0; i < n; i++) buf[i] = i; *p = buf[n/2]; } void g(int* buf, int n) { int x1, x2; thread t1(f, buf, n, &x1); thread t2(f, buf, n, &x2); t1.join(); t2.join(); assert(x1 == x2); } Although it's interesting, I'm of less concern of what guarantees the standard gives, since I guess it gives none. What I do care is what will be the behavior of the

In multithreading: How to determine which thread stops first

旧时模样 提交于 2019-12-05 05:57:43
Write a class named RaceHorse that extends Thread. Each RaceHorse has a name and run() method that displays the name 5000 times. Write a Java application that instantiates 2 RaceHorse objects. The last RaceHorse to finish is the loser. This is the question. I have written the code for the two classes two run the thread Here are the codes: RaceHorse class RaceHorse extends Thread { public String name; public RaceHorse(String name) { this.name = name; } public void run() { for(int i = 1 ; i <= 5000; i++) { System.out.println(i+" "+name); } System.out.println(name+" finished."); } } Runner class

Intermittent ClassCastException from ElementNSImpl to own type during unmarshalling

℡╲_俬逩灬. 提交于 2019-12-05 05:14:23
We are experiencing an exceedingly hard to track down issue where we are seeing ClassCastExceptions sometimes when trying to iterate over a list of unmarshalled objects. The important bit is sometimes , after a reboot the particular code works fine. This seems to point in the direction of concurrency/timing/race condition. I can confirm that neither the JAXBContext, nor the marshallers and unmarshallers are being used concurrently. We've gone as far as serializing access to them through locking. However, since we run on an OSGi platform where individual bundles are getting initialized

Is this a race condition?

↘锁芯ラ 提交于 2019-12-05 03:44:25
The definition of a race condition: A race condition or race hazard is a flaw in a system or process whereby the output or result of the process is unexpectedly and critically dependent on the sequence or timing of other events. Consider the following pseudocode: Global variable i initialized to 6; Thread 1: acquire(lock l) increment global variable i, i.e. i++; Thread 2: acquire(lock l) double the value of global var i, i.e.: i*=2; If T1 acquires the lock l first and T2 second the value of i will be 14. On the other hand, if T2 acquires the lock l first and T1 second the value of i will be 13

Terrible performance - a simple issue of overhead, or is there a program flaw?

ぐ巨炮叔叔 提交于 2019-12-04 23:41:48
问题 I have here what I understand to be a relatively simple OpenMP construct. The issue is that the program runs about 100-300x faster with 1 thread when compared to 2 threads. 87% of the program is spent in gomp_send_wait() and another 9.5% in gomp_send_post . The program gives correct results, but I wonder if there is a flaw in the code that is causing some resource conflict, or if it is simply that the overhead of the thread creation is drastically not worth it for a a loop of chunk size 4. p

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

情到浓时终转凉″ 提交于 2019-12-04 15:54:45
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. You're supposed to create the file anyway, and let the OS know whether or not you want a new file to be created in

syscall_thread_switch iOS 8.3 race - CocoaLumberjack bug? how to debug this?

南笙酒味 提交于 2019-12-04 15:13:32
I'm hitting a race-condition in my app, where all or all but 1 threads get stuck on syscall_thread_switch whenever I pause debugging. It reproduces much more often on the simulator, but also on the iPad Air. There is ALWAYS at least 2 threads stuck in CocoaLumberjack's queueLogMessage: -- see screenshots. I've never seen this before on 8.1 and 8.2, but i'm hitting it often on 8.3. I'm not claiming this is an 8.3 bug :) This is a level of complexity i've never had to debug before, so i'm not sure what to do. I hope I'm providing enough information, please let me know if you need more (please be

Combining a multi-message incoming SMS message from Twilio

Deadly 提交于 2019-12-04 14:09:30
I'm building an SMS-enabled app that allows the user to communicate with an external contact. As incoming messages arrive, I save them to my database so that they can be displayed in the app. In testing, everything is working great for one-off messages, but I'm having trouble with messages longer than 160 characters, which are actually broken up into multiple messages. When an incoming message comes in via SMS, I'm checking whether we already got a message from that number within the past few seconds. If we have, I append to the existing record in my DB, rather than saving a new record. //

Explain race condition in double checked locking

百般思念 提交于 2019-12-04 14:02:10
void undefined_behaviour_with_double_checked_locking() { if(!resource_ptr) #1 { std::lock_guard<std::mutex> lk(resource_mutex); #2 if(!resource_ptr) #3 { resource_ptr.reset(new some_resource); #4 } } resource_ptr->do_something(); #5 } if a thread sees the pointer written by another thread, it might not see the newly-created instance of some_resource, resulting in the call to do_something() operating on incorrect values. This is an example of the type of race condition defined as a data race by the C++ Standard, and thus specified as undefined behaviour. Question > I have seen the above

PL/pgSQL column name the same as variable

大兔子大兔子 提交于 2019-12-04 08:05:46
I'm new to plpgsql and I'm trying to create function that will check if a certain value exists in table and if not will add a row. CREATE OR REPLACE FUNCTION hire( id_pracownika integer, imie character varying, nazwisko character varying, miasto character varying, pensja real) RETURNS TEXT AS $BODY$ DECLARE wynik TEXT; sprawdzenie INT; BEGIN sprawdzenie = id_pracownika; IF EXISTS (SELECT id_pracownika FROM pracownicy WHERE id_pracownika=sprawdzenie) THEN wynik = "JUZ ISTNIEJE"; RETURN wynik; ELSE INSERT INTO pracownicy(id_pracownika,imie,nazwisko,miasto,pensja) VALUES (id_pracownika,imie