multithreading

Why we must use “while” for checking race condition not “if”

我的梦境 提交于 2021-02-07 07:13:11
问题 I read the following code in "Thinking in java". synchronized(obj) { while (condition_not_matched) { obj.wait(); } //continue dosomething(); } What I think: Use "if" is OK, because the "wait" means it must get the obj's lock monitor, and only one thread can executed here. (1)Why here use "while (condition)" not "if" ? (2)What happend when executed "obj.wait()"? Does the currrent thread release the lock of "obj"? (3)And when another thread executed "obj.notify()", what happend of the previous

Accessing different data members belonging to the same object from 2 different thread in C++

回眸只為那壹抹淺笑 提交于 2021-02-07 07:09:05
问题 I have a few objects I need to perform actions on from different threads in c++. I known it is necessary to lock any variable that may be used by more than one thread at the same time, but what if each thread is accessing (writing to) a different data member of the same object? For example, each thread is calling a different method of the object and none of the methods called modify the same data member. Is it safe as long as I don't access the same data member or do I need to lock the whole

Accessing different data members belonging to the same object from 2 different thread in C++

自闭症网瘾萝莉.ら 提交于 2021-02-07 07:08:51
问题 I have a few objects I need to perform actions on from different threads in c++. I known it is necessary to lock any variable that may be used by more than one thread at the same time, but what if each thread is accessing (writing to) a different data member of the same object? For example, each thread is calling a different method of the object and none of the methods called modify the same data member. Is it safe as long as I don't access the same data member or do I need to lock the whole

reduction variable is private in outer context

谁都会走 提交于 2021-02-07 06:52:44
问题 I have the following code: void simulation (MD *md){ double sum; #pragma omp parallel private (move) { for(move = 0; move < maxIterations; ++move) { cicleDoMove(md); cicleForces(md); cicleMkekin(md,sum); // ... } } } where : void cicleMkekin(Md *md, double sum){ #pragma omp for reduction(+ : sum) for (i = 0; i < md->mdsize; i++) { sum += mkekin(..); } // .. } I got the following error: "reduction variable 'sum' is private in outer context" The variable sum is shared not private, in fact if I

Why is volatile keyword not allowed for local variables?

梦想与她 提交于 2021-02-07 06:51:17
问题 Consider the snippet: If in a main thread, I have this inside a method - volatile CountDownLatch latch = new CountDownLatch(3); new Thread(new ProcessThread("Worker1",latch, 20000)).start();//20 secs new Thread(new ProcessThread("Worker2",latch, 60000)).start();//60 secs new Thread(new ProcessThread("Worker3",latch, 40000)).start();//40 secs I see that volatile is shown as an illegal modifier. And only final is permitted. And final guarantees initialization safety . public static class

Command-line options to force googletest to run in single thread

ⅰ亾dé卋堺 提交于 2021-02-07 06:45:06
问题 I have a suite of unit tests that is managed by googletest. These tests are run in multiple threads by default, even when I use --gtest_filter=foo.test so that it only runs a single test. This is causing ambiguity about the cause of a bug I'm trying to hammer out. How can multithreaded testing be turned off in googletest? 回答1: There's no commandline switch for single/multi-threading. libgtest is built either single-threading or multi-threading. To make it single-threading, build gtest with .

Getting broken pipe when passing mysql connection to a python thread

假如想象 提交于 2021-02-07 06:44:13
问题 I'm trying to pass a mysql connection to a thread in python. If i do the initialization of the mysql inside the worker class, there is no error. However, it might be costly for the connection so I tried just passing the mysql connection from the caller function (see code below). But this keeps throwing this error: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe')) Any idea why? I think its because the way we pass the mysql connection def worker(db): """ Distributes the

Python and sqlite3.ProgrammingError: Recursive use of cursors not allowed

吃可爱长大的小学妹 提交于 2021-02-07 06:28:04
问题 i wrote a python program like this that should run in multithreading mode: def Func(host,cursor,db): cursor.execute('''SELECT If_index, Username, Version, Community, Ip_traff FROM HOST WHERE Hostname = ?''',(host,)) #do something #--- Main --- db = sqlite3.connect(os.getcwd()+'\HOST', check_same_thread = False) #opendatabase cursor = db.cursor() #generate a cursor for ii in range(len(host)): #host is a list of ipaddress #for each host i want generate a thread thr = threading.Thread(target =

`volatile` to sync variable between threads

只愿长相守 提交于 2021-02-07 06:27:05
问题 I have a variable int foo that is accessed from two threads. Assuming I have no race-condition issues (access is protected by a mutex, all operations are atomic, or whatever other method to protect from race conditions), there is still the issue of "register caching" (for lack of a better name), where the compiler may assume that if the variable is read twice without being written in between, it is the same value, and so may "optimize" away things like: while(foo) { // <-may be optimized to

Python multithreading model

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-07 06:09:14
问题 I have been studying multithreading in python for a while, however I was confused on a few issues- Firstly, are the threads created by the python threading library user level or kernel level threads? Books say that user level threads must be mapped to kernel threads and the operating system only creates and maintains kernel level threads. Which thread model will be used in the python threading library? Further, who makes the choice between kernel and user level threads? Is it the operating