multithreading

JNI thread model?

我只是一个虾纸丫 提交于 2021-02-17 21:47:10
问题 When I call a C/C++ from Java, is a new thread created by JavaVM or JNI to run the C/C++ code while my Java thread is waiting? I ask this because my C/C++ code runs something on the GPU and I need to check a specific buffer to get the result back. Once I have the result, I need to call my Java function again. So I was thinking of creating a thread on the C++ side that continuously checks the buffer and once there is some data available, makes a call back to the Java side. 回答1: The JNI does

Semaphore - What is the use of initial count?

拟墨画扇 提交于 2021-02-17 07:33:12
问题 http://msdn.microsoft.com/en-us/library/system.threading.semaphoreslim.aspx To create a semaphore, I need to provide an initial count and maximum count. MSDN states that an initial count is - The initial number of requests for the semaphore that can be granted concurrently. While it states that maximum count is The maximum number of requests for the semaphore that can be granted concurrently. I can understand that the maximum count is the maximum number of threads that can access a resource

Tips to prevent deadlocks in java

杀马特。学长 韩版系。学妹 提交于 2021-02-17 07:27:14
问题 I am studying java threads and deadlocks, I understand deadlock's examples but I wonder if there are general rules to follow to prevent it. My question is if there are rules or tips that can be applied to the source code in java to prevent deadlocks? If yes, could you explain how to implement it? 回答1: Some quick tips out of my head don't use multiple threads (like Swing does, for example, by mandating that everything is done in the EDT) don't hold several locks at once. If you do, always

My console app shutdown prematurely when using async / await?

痴心易碎 提交于 2021-02-17 07:04:16
问题 I have a console app that all it does is loop through all the customers and send emails to specific ones and then shutdown. I noticed that some of the functions in MailKit’s offer asynchronous so I tried using them instead of non-aysnc and when I did that, it executed the first statement (emailClient.ConnectAsync) on and then I noticed that my console app was shutting down. It didn’t crash. The execution returned to Main() and continued executing after my call to my SendReports() function.

My console app shutdown prematurely when using async / await?

▼魔方 西西 提交于 2021-02-17 07:03:11
问题 I have a console app that all it does is loop through all the customers and send emails to specific ones and then shutdown. I noticed that some of the functions in MailKit’s offer asynchronous so I tried using them instead of non-aysnc and when I did that, it executed the first statement (emailClient.ConnectAsync) on and then I noticed that my console app was shutting down. It didn’t crash. The execution returned to Main() and continued executing after my call to my SendReports() function.

c# Stop Thread with a button click

风流意气都作罢 提交于 2021-02-17 05:37:21
问题 How can I end my thread with a button click? I start my thread with a button click. new Thread(SampleFunction).Start(); and my thread: void SampleFunction() { int i = 0; while (true) { string Seconds = DateTime.Now.ToString("ss"); if (Seconds == "00") { int i2 = i++; string myString = i2.ToString(); AppendTextBox(myString); Thread.Sleep(2000); } } } public void AppendTextBox(string value) { if (InvokeRequired) { this.Invoke(new Action<string>(AppendTextBox), new object[] { value }); return; }

python threading method stuck

落花浮王杯 提交于 2021-02-17 05:27:10
问题 I have a class MyClass which creates 7 threads when it is initialized. One thread is a TCPServer, and the other six are objects of MyClass which the TCPServer uses to process requests. My intention is to create method which can run in the background of MyClass and maintain the 6 threads. The 6 threads correspond to 2 distributed objects obj1 and obj2 replicated with an implementation of Raft called PySyncObj. There are three threads per object. Each cluster of objects obj_1_cluster and obj_2

C# thread-safety on increment/decrement operations [duplicate]

帅比萌擦擦* 提交于 2021-02-17 04:47:24
问题 This question already has answers here : How come INC instruction of x86 is not atomic? [duplicate] (3 answers) Closed 6 years ago . I was trying to investigate a simple piece of code with two threads accessing a shared integer variable, one incrementing and the other decrementing it: static int n = 0; static void Main() { var up = new Task(() => { for (int j = 0; j < 400000; j++) ++n; }); up.Start(); for (int i = 0; i < 400000; i++) --n; up.Wait(); Console.WriteLine(n); Console.ReadKey(); }

Do I need to clean up Thread objects in Java?

怎甘沉沦 提交于 2021-02-17 03:27:13
问题 In my Java application I have a Runnable such as: this.runner = new Runnable({ @Override public void run() { // do something that takes roughly 5 seconds. } }); I need to run this roughly every 30 seconds (although this can vary) in a separate thread. The nature of the code is such that I can run it and forget about it (whether it succeeds or fails). I do this as follows as a single line of code in my application: (new Thread(this.runner)).start() Now, this works fine. However, I'm wondering

OpenMP not waiting all threads finish before end C program

我怕爱的太早我们不能终老 提交于 2021-02-17 03:19:36
问题 I have the following problem: My C program must count the number of occurrences of a list of words in a text file. I use OpenMP for this, and the program, in theory, has the correct logic. When I put some printfs inside a For Loop the result of the program is correct and always the same. When I remove printfs the result is incorrect, and with each execution its value changes. Given this scenario I think the reason is related to the execution time. With printfs the execution time is increased,