multithreading

Lock on an object that might change during code execution

一世执手 提交于 2020-02-21 13:01:21
问题 Let's suppose I have a thread that locks on an object reference Thread #1 lock(myObj) { ... } later in code I have myObj = new XYZObj(); and then Thread #2 locks on it lock(myObj) { ... } Will this code be thread safe, if the object reference has changed? When the object reference changes, the first lock is still valid? 回答1: Locks work on instances, not variables. The lock statement will hold its own reference to the instance so that it will only exit the instance you entered. The spec says:

Why the following C# multi-threaded code does not output zero though it does in debugger?

那年仲夏 提交于 2020-02-21 10:45:24
问题 class Program { private static volatile int value; public static void Increment() { for (int i =0; i <100000; i++) { value++; } } public static void Decrement() { for (int j =0 ; j < 100000; j++) { value--; } } public static void ThreadTest() { value = 0; var incrementThread = new Thread(Increment); var decrementThread = new Thread(Decrement); incrementThread.Start(); decrementThread.Start(); incrementThread.Join(); decrementThread.Join(); Console.WriteLine("Value of value {0}", value); }

Why the following C# multi-threaded code does not output zero though it does in debugger?

允我心安 提交于 2020-02-21 10:44:50
问题 class Program { private static volatile int value; public static void Increment() { for (int i =0; i <100000; i++) { value++; } } public static void Decrement() { for (int j =0 ; j < 100000; j++) { value--; } } public static void ThreadTest() { value = 0; var incrementThread = new Thread(Increment); var decrementThread = new Thread(Decrement); incrementThread.Start(); decrementThread.Start(); incrementThread.Join(); decrementThread.Join(); Console.WriteLine("Value of value {0}", value); }

Processing huge CSV file using Python and multithreading

无人久伴 提交于 2020-02-20 05:26:33
问题 I have a function that yields lines from a huge CSV file lazily: def get_next_line(): with open(sample_csv,'r') as f: for line in f: yield line def do_long_operation(row): print('Do some operation that takes a long time') I need to use threads such that each record I get from the above function I can call do_long_operation . Most places on Internet have examples like this, and I am not very sure if I am on the right path. import threading thread_list = [] for i in range(8): t = threading

Processing huge CSV file using Python and multithreading

不打扰是莪最后的温柔 提交于 2020-02-20 05:26:08
问题 I have a function that yields lines from a huge CSV file lazily: def get_next_line(): with open(sample_csv,'r') as f: for line in f: yield line def do_long_operation(row): print('Do some operation that takes a long time') I need to use threads such that each record I get from the above function I can call do_long_operation . Most places on Internet have examples like this, and I am not very sure if I am on the right path. import threading thread_list = [] for i in range(8): t = threading

Threads and fork(). How can I deal with that? [duplicate]

旧街凉风 提交于 2020-02-19 19:07:01
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: fork in multi-threaded program If I have an application which employs fork() and might be developed as multithreaded, what are the thumb rules/guidelines to consider to safely program this kind of applications? 回答1: The basic thumb rules, according to various internet articles like ( http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them , fork in multi-threaded program ) are: (Main)

Threads and fork(). How can I deal with that? [duplicate]

人走茶凉 提交于 2020-02-19 19:06:24
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: fork in multi-threaded program If I have an application which employs fork() and might be developed as multithreaded, what are the thumb rules/guidelines to consider to safely program this kind of applications? 回答1: The basic thumb rules, according to various internet articles like ( http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them , fork in multi-threaded program ) are: (Main)

Tensorflow: Load data in multiple threads on cpu

天涯浪子 提交于 2020-02-18 23:04:06
问题 I have a python class SceneGenerator which has multiple member functions for preprocessing and a generator function generate_data() . The basic structure is like this: class SceneGenerator(object): def __init__(self): # some inits def generate_data(self): """ Generator. Yield data X and labels y after some preprocessing """ while True: # opening files, selecting data X,y = self.preprocess(some_params, filenames, ...) yield X, y I used the class member function sceneGenerator.generate_data()

Tensorflow: Load data in multiple threads on cpu

偶尔善良 提交于 2020-02-18 22:56:55
问题 I have a python class SceneGenerator which has multiple member functions for preprocessing and a generator function generate_data() . The basic structure is like this: class SceneGenerator(object): def __init__(self): # some inits def generate_data(self): """ Generator. Yield data X and labels y after some preprocessing """ while True: # opening files, selecting data X,y = self.preprocess(some_params, filenames, ...) yield X, y I used the class member function sceneGenerator.generate_data()

Behavior of entrySet().removeIf in ConcurrentHashMap

人走茶凉 提交于 2020-02-18 05:50:32
问题 I would like to use ConcurrentHashMap to let one thread delete some items from the map periodically and other threads to put and get items from the map at the same time. I'm using map.entrySet().removeIf(lambda) in the removing thread. I'm wondering what assumptions I can make about its behavior. I can see that removeIf method uses iterator to go through elements in the map, check the given condition and then remove them if needed using iterator.remove() . Documentation gives some info about