Posts

Showing posts with the label Multithreading

C++ Terminate Called Without An Active Exception

Answer : When a thread object goes out of scope and it is in joinable state, the program is terminated. The Standard Committee had two other options for the destructor of a joinable thread. It could quietly join -- but join might never return if the thread is stuck. Or it could detach the thread (a detached thread is not joinable). However, detached threads are very tricky, since they might survive till the end of the program and mess up the release of resources. So if you don't want to terminate your program, make sure you join (or detach) every thread. How to reproduce that error: #include <iostream> #include <stdlib.h> #include <string> #include <thread> using namespace std; void task1(std::string msg){ cout << "task1 says: " << msg; } int main() { std::thread t1(task1, "hello"); return 0; } Compile and run: el@defiant ~/foo4/39_threading $ g++ -o s s.cpp -pthread -std=c++11 el@defiant ~/foo4/39_threading ...

Binary Semaphore Vs A ReentrantLock

Answer : there is no real reason ever to have a binary semaphore as everything that a binary semaphore can do can also be done by a ReentrantLock If all you need is reentrant mutual exclusion, then yes, there is no reason to use a binary semaphore over a ReentrantLock. If for any reason you need non-ownership-release semantics then obviously semaphore is your only choice. Also since reentrant locks also provide one lock per object, isn't it always a better idea to prefer a reentrant lock to a binary semaphore? It depends on the need. Like previously explained, if you need a simple mutex, then don't choose a semaphore. If more than one thread (but a limited number) can enter a critical section you can do this through either thread-confinement or a semaphore. I have checked a post here that talks about difference between a binary semaphore and a mutex but is there a thing like a mutex in Java? ReentrantLock and synchronized are examples of mu...

Adding A Delay Without Thread.sleep And A While Loop Doing Nothing

Answer : Something like the following should give you the delay you need without holding up the game thread: private final long PERIOD = 1000L; // Adjust to suit timing private long lastTime = System.currentTimeMillis() - PERIOD; public void onTick() {//Called every "Tick" long thisTime = System.currentTimeMillis(); if ((thisTime - lastTime) >= PERIOD) { lastTime = thisTime; if(variable) { //If my variable is true boolean = true; //Setting my boolean to true /** *Doing a bunch of things. **/ //I need a delay for about one second here. boolean = false; //Setting my boolean to false; } } } long start = new Date().getTime(); while(new Date().getTime() - start < 1000L){} is the simplest solution I can think about. Still, the heap might get polluted with a lot of unreferenced Date objects, which, depending on how often you get to create such a pseudo-dela...

Automating The InvokeRequired Code Pattern

Answer : Lee's approach can be simplified further public static void InvokeIfRequired(this Control control, MethodInvoker action) { // See Update 2 for edits Mike de Klerk suggests to insert here. if (control.InvokeRequired) { control.Invoke(action); } else { action(); } } And can be called like this richEditControl1.InvokeIfRequired(() => { // Do anything you want with the control here richEditControl1.RtfText = value; RtfHelpers.AddMissingStyles(richEditControl1); }); There is no need to pass the control as parameter to the delegate. C# automatically creates a closure. UPDATE : According to several other posters Control can be generalized as ISynchronizeInvoke : public static void InvokeIfRequired(this ISynchronizeInvoke obj, MethodInvoker action) { if (obj.InvokeRequired) { var args = new object[0]; obj.Invoke(action, args); } else { action...