Posts

Showing posts with the label multithreading

Atomicness for a global variable shared by threads

Atomicness for a global variable shared by threads Okay, trivial question. Problem is, every answer I find on here has 3/4 conflicting answers. I have a very simple problem. I have a global variable called ABORT_SIGNAL. As of now, this is declared volatile int ABORT_SIGNAL . I understand that this does not do what I wish... which is... volatile int ABORT_SIGNAL I have two threads which may write to ABORT_SIGNAL. It will start at 0, and go to 1 after a number of seconds. Every other thread will be reading this variable on a regular basis, checking to see if the value has been set to 1. Would the following achieve this .... #include <stdint.h> atomic_int ABORT_SIGNAL; ... // When updating the value ... atomic_store(&ABORT_SIGNAL, someValue); // When reading the value ... if (atomic_load_explicit(&ABORT_SIGNAL, memory_order_relaxed)) doSomething() Others have also suggested that I would need to do something like the following. After each write issue atomic_thread_fence(m...

Is the memory block allocated by a thread is on the same affinity as the thread itself until the thread exit?

Is the memory block allocated by a thread is on the same affinity as the thread itself until the thread exit? This is a question about NUMA. For example, in the code below, is the buffer allocated at the local memory of the thread/process throughout its life? for (int th = 0; th < maxThreads; th++) { threads[th] = std::thread([&, th] { int* buffer = new int[1000]; // do something delete buffer; } } Update: to make the question more straightforward, let me ask in this way. If I have 10 simultaneous threads (say, t0 to t9) launched, and within the scope of each threads, it allocates a memory block(say, m0 to m9). Would thread t_n always operates on m_n (n from 0 to 9) before the thread exit, or thread 0 might migrate and operate on memory 9? The memory block in my situation is not very large, usually only a couple of mega-bytes. This will be implementation specific; what is your compiler? ...

A simple way to run a piece of python code in parallel?

A simple way to run a piece of python code in parallel? I have this very simple python code: Test = 1; def para(): while(True): if Test > 10: print("Test is bigger than ten"); time.sleep(1); para(); # I want this to start in parallel, so that the code below keeps executing without waiting for this function to finish while(True): Test = random.randint(1,42); time.sleep(1); if Test == 42: break; ...#stop the parallel execution of the para() here (kill it) ..some other code here Basically, I want to run the function para() in parallel to the other code, so that the code below it doesn't have to wait for the para() to end. However, I want to be able to access the current value of the Test variable, inside of the para() while it is running in parallel (as seen in the code example above). Later, when I decide, that I am done with the para() running in parallel, I would like to know how to kill it both from the main thread, but also fro...

In multithreading, why the other threads are not working when I have paused one thread for execution

In multithreading, why the other threads are not working when I have paused one thread for execution I have three threads in my programming.According to the threading concept all the threads run asynchronously. So every thread is executed by the processor randomly. If I have just paused the execution of one thread by using thread.sleep() then other thread should work . Right?? And when the sleep time for the thread is over then it should again continue with its execution. But in my case when I am pausing one thread by using t2.sleep(10000) then all other threads are also getting paused. But they should have worked normally while the t2 thread is in wait.Can anyone tell me why is it so? import java.util.*; class square extends Thread { public int x; public square(int x) { this.x = x; } public void run() { System.out.println("Square of the number is " + x*x); } } class cube extends Thread ...

Can ConcurrentLinkedDeque have a fixed size and overwrite old elements?

Can ConcurrentLinkedDeque have a fixed size and overwrite old elements? If I got this right ConcurrentLinkedDeque acts as a stack right if you use pollLast() ? pollLast() Now my issue is that I need a set size of ConcurrentLinkedDeque. My producer doesn't stop, so even if I have 16GB of ram I will run out eventually. SO is it possible to se a fixed size ? My implementation: ConcurrentLinkedDeque<String> queue = new ConcurrentLinkedDeque<>(); Producer (Thread 1): runs queue.add(line); Consumer (Thread 2): runs queue.pollLast(); queue.add(line); queue.pollLast(); Please note that both threads run in a while true loop . This is because of the requirements. That is why I am using ConcurrentLinkedDeque and not ArrayBlockingQueue or SynchronousQueue because it is non-blocking. ConcurrentLinkedDeque ArrayBlockingQueue SynchronousQueue Also do I need to declare anything synchronised ? synchronised Use a fixed size BlockingQueue which would all...

Use sychronized or not below code generating same hashcode for all threads. How?

Use sychronized or not below code generating same hashcode for all threads. How? "we make the global access method synchronized so that only one thread can execute getInstance method at a time" without using synchronized keyword below code acting like threadsafe already. Car.java public class Car { private static Car car; private Car() { // TODO Auto-generated constructor stub } public static Car getInstance(){ if(car==null){ car=new Car(); } return car; } } Test.java public class Test { public static void main(String args) { Thread t1=new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+" Running"); System.out.println(Car.getInstance().hashCode()); System.out.println(Thread.currentThread().getName()+" Finishing"); } }); t1.start(); Thread t2=new Thread(new Runnable() { @Override public...