Execute class method or function(static method) on main thread from another thread C/C++
By : Mikka
Date : March 29 2020, 07:55 AM
Hope this helps No, you'll have to roll your own. One way to do this would be to keep a global function pointer that your main thread checks each iteration of the main loop. For example: code :
// Global variables
pthread_mutex_t gMutex = PTHREAD_MUTEX_INITIALIZER;
void (*gFuncPtr)(void*);
void *gFuncArg;
// Your program's main loop in the main thread
while(programIsRunning())
{
// If we have a function to call this frame, call it, then do the rest of
// our main loop processing
void (*funcPtr)(void*);
void *funcArg;
pthread_mutex_lock(&gMutex);
funcPtr = gFuncPtr;
funcArg = gFuncArg;
gFuncPtr = NULL;
pthread_mutex_unlock(&gMutex);
if(funcPtr != NULL)
(*funcPtr)(funcArg);
// Rest of main loop
...
}
// Call this function from another thread to have the given function called on
// the main thread. Note that this only lets you call ONE function per main
// loop iteration; it is left as an exercise to the reader to let you call as
// many functions as you want. Hint: use a linked list instead of a single
// (function, argument) pair.
void CallFunctionOnMainThread(void (*funcPtr)(void*), void *funcArg)
{
pthread_mutex_lock(&gMutex);
gFuncPtr = funcPtr;
gFuncArg = funcArg;
pthread_mutex_unlock(&gMutex);
}
|
Difference between User Defined Thread and System Thread (For Sleep Method)
By : user3261526
Date : March 29 2020, 07:55 AM
may help you . Thread.sleep(10) will sleep the currently executing thread, i.e. not necessarily one of the threads you have in your array, more likely the main thread that spawns the others looking at your example, because it's from that thread you call it. If what you want to achieve is to make your spawning process wait until all it's childs have finished working, then Java provides a built in mechanism for this, namely the join method. What it does is to make the thread wherein you make the call wait until the thread on which you call it finishes. If this is indeed what you want to achieve, then replace your last for-loop with the following: code :
for(Thread t : TCreate){ //Consider naming variables with initial lowercase
t.join();
//Should be called after it's been started but not until you have started all of them
}
//Execution resumes here when all spawned threads have completed their task.
|
What is the difference between calling the thread.start() method in an constructor and a method
By : Vishal Kalbande
Date : March 29 2020, 07:55 AM
Hope that helps First of all please start your class name with a capital letter since this is convention in java. It doesn't matter if you start your thread in the constructor or in a method. One problem is that you access UI elements outside of the Event Dispatching Thread (EDT), which is the only one which is allowed to access, UI elements. code :
public class FlashThread implements Runnable {
private JLabel temp;
Thread thread;
Color randColor;
public FlashThread(JLabel toFlash) {
temp = toFlash;
thread = new Thread(this);
thread.start();
}
public void run() {
final Random r = new Random();
while (true) {
SwingUtilities.invokeAndWait(new Runnable(){
public void run() {
// this will be executed in the EDT
temp.setForeground(new Color(r.nextInt(246) + 10,
r.nextInt(246) + 10, r.nextInt(246) + 10));
// don't perform long running tasks in the EDT or sleep
// this would lead to non-responding user interfaces
}
});
// Let our create thread sleep
try {
Thread.sleep(r.nextInt(500));
} catch (InterruptedException e) {
}
}
}
}
|
difference between parameterizedThreadstart, Threadstart and Thread
By : user3742552
Date : March 29 2020, 07:55 AM
seems to work fine ThreadStart and ParameterizedThreadStart are delegate types, defined like this:
|
C# Basic Multi-Threading Question: Call Method on Thread A from Thread B (Thread B started from Thread A)
By : ran levi
Date : January 02 2021, 06:48 AM
I wish did fix the issue. Typically, this is unnecessary. You can call a method on any object from any thread, and this is a good thing... UI components and some legacy COM components tend to be the only items which must be accessed from a specific thread. Instead of trying to call a method on a different thread, normally, you'll try to use synchronization (ie: lock(...) and similar) to protect access to the data itself, and make it safe to work with from multiple threads.
|