thread-safety

Boost equivalent of ManualResetEvent?

怎甘沉沦 提交于 2019-12-20 10:52:06
问题 I'm wondering if there is a boost equivalent of ManualResetEvent? Basically, I'd like a cross-platform implementation... Or, could someone help me mimic ManualResetEvent's functionality using Boost::thread? Thanks guys 回答1: It's pretty easy to write a manual reset event when you have mutexes and condition variables. What you will need is a field that represents whether your reset event is signalled or not. Access to the field will need to be guarded by a mutex - this includes both setting

Updating VCL from the same thread that created the UI. Why?

杀马特。学长 韩版系。学妹 提交于 2019-12-20 10:46:16
问题 I know that I must call Synchronize to update the vcl from a thread that did not create the controls or send a message to the window. I have often heard the word not thread safe but I can't find an actual explanation about what is happening. I know the application might crash with an access violation, but again I don't know why? Please shed a light on this topic. 回答1: About GDI thread safety in Windows, see this reference article. It clearly states that you can access safely handles from

Any satisfactory approaches to unit testing thread safety in Java?

依然范特西╮ 提交于 2019-12-20 08:56:15
问题 I am looking at improving a package that I believe not to be threadsafe when its input is shared between multiple worker threads. According to TDD principles, I should write some tests that fail in the first instance, and these would certainly be useful in assessing the problem. I realise that this is not a simple thing to acheive, and that naively, multi-threaded tests will be nondeterministic as the operating system will determine scheduling and the exact order that various operations are

Difference between synchronized and re-entrant lock? [duplicate]

故事扮演 提交于 2019-12-20 08:39:32
问题 This question already has an answer here : What's the difference in using ReentrentLock and Synchronized(object)? [duplicate] (1 answer) Closed 2 years ago . I have used the synchronized keyword and re-entrant locks in Java, but I don't understand how they differ, or which is appropriate for a given situation. How do I decide when should I use synchronized and when I should use re-entrant locks? 回答1: A ReentrantLock is: A reentrant mutual exclusion Lock with the same basic behavior and

Difference between synchronized and re-entrant lock? [duplicate]

天大地大妈咪最大 提交于 2019-12-20 08:38:50
问题 This question already has an answer here : What's the difference in using ReentrentLock and Synchronized(object)? [duplicate] (1 answer) Closed 2 years ago . I have used the synchronized keyword and re-entrant locks in Java, but I don't understand how they differ, or which is appropriate for a given situation. How do I decide when should I use synchronized and when I should use re-entrant locks? 回答1: A ReentrantLock is: A reentrant mutual exclusion Lock with the same basic behavior and

Thread-safe C++ stack

早过忘川 提交于 2019-12-20 08:29:31
问题 I'm new to C++ and am writing a multi-threaded app whereby different writers will be pushing objects onto a stack and readers pulling them off the stack (or at least pushing the pointer to an object).. Are there any structures built-into C++ which can handle this without adding locking code etc.? If not, what about the Boost libraries? EDIT: Hi. Thanks for the initial great answers. I guess one reason I thought this could be built-in was that I was thinking purely in x86 space and thought

Java wait and notifyAll: IllegalMonitorStateException

不想你离开。 提交于 2019-12-20 07:27:03
问题 I am Java newbie (and RoR developer). I have a simple program. Ball is shared amont players. Ball should be passed to random Player. Ok here goes the code: class Ball { private int currentPlayer; public void setCurrentPlayer( int currentPlayer, int fromWho ) { this.currentPlayer = currentPlayer; System.out.println( "Ball:setCurrentPlayer " + fromWho + " ---> " + currentPlayer ); } public int getCurrentPlayer() { return currentPlayer; } } class Player implements Runnable { private int myID;

Are java variables themselves thread safe? When updating variables? [duplicate]

牧云@^-^@ 提交于 2019-12-20 06:44:09
问题 This question already has answers here : Thread-safe setting of a variable (Java)? (5 answers) Closed 3 years ago . Suppose I have two threads updating an object, and one thread reading from that object with no synchronization. Obviously, this is run condition. However, I am wondering if the variable itself can only partially written. public class CommonObject extends Object { static int memberVar=-1; } public class Input1Thread extends Thread { public void run() { while(true) CommonObject

Python execute threads by order

落花浮王杯 提交于 2019-12-20 05:43:19
问题 I have the following code: import threading def send_to_server(lst): #Some logic to send the list to the server. while 1: lst = [] for i in range(1000): lst.append(i) task = threading.Thread(target=send_to_server,args(copy(lst),)) task.start() I have a few Questions: 1) The idea for using threads is because sending to server takes time and I want to continue generating the data without any stop. The problem with this code is that if I created thread #3 and its taking long time to process, By

Particular Thread Count

时光怂恿深爱的人放手 提交于 2019-12-20 05:35:17
问题 I want to know how many active threads are there for a particular Thread class. Lets say I have a class T which extends thread. In some other class (Ex: Demo) , I want to get the thread count for the T class Thread. I do know Thread.activeCount() method but it will get the count for a thread group. It does not server my need here. Lets say I have T1 and T2 classes which extends thread and In the Demo class I want to get How many T2 active threads are there. How should I achieve this? Any