Posts

Showing posts with the label multithreading

C++ QThread and connect causing crashes

C++ QThread and connect causing crashes My QThread counter crashes giving odd results for what the number should be as the Thread counts properly but in the SetLabel function I get a different number to whats in the QThread ands it then crashes after 3 seconds and the label doesnt seem to update. QThread* CountThread = new QThread; Counter* Count = new Counter(); Count->moveToThread(CountThread); connect(CountThread, SIGNAL(started()), Count, SLOT(Process())); connect(Count, &Counter::SecondsUpdate, this, [=]{ SetLabel(Count->Seconds) ;}); connect(Count, SIGNAL(finished()), CountThread, SLOT(quit())); CountThread->start(); void Counter::Process() { int secs = 0; while (secs < 1000) { qDebug() << "hello" << secs; secs += 1; Sleep(1000); emit SecondsUpdate(); } emit finished(); } void BaseWindow::SetLabel(int Seconds) { qDebug() << Seconds; this->Test->setText(QString("Seconds: " + QString::number(Seconds))); } c...

How can I use openmp and AVX2 simultaneously with perfect answer?

How can I use openmp and AVX2 simultaneously with perfect answer? I wrote the Matrix-Vector product program using OpenMP and AVX2. However, I got the wrong answer because of OpenMP. The true answer is all of the value of array c would become 100. My answer was mix of 98, 99, and 100. The actual code is below. I compiled Clang with -fopenmp, -mavx, -mfma. #include "stdio.h" #include "math.h" #include "stdlib.h" #include "omp.h" #include "x86intrin.h" void mv(double *a,double *b,double *c, int m, int n, int l) { int k; #pragma omp parallel { __m256d va,vb,vc; int i; #pragma omp for private(i, va, vb, vc) schedule(static) for (k = 0; k < l; k++) { vb = _mm256_broadcast_sd(&b[k]); for (i = 0; i < m; i+=4) { va = _mm256_loadu_pd(&a[m*k+i]); vc = _mm256_loadu_pd(&c[i]); vc = _mm256_fmadd_pd(vc, va, vb); _mm...

Replacing busy loop and sleep in thread

Replacing busy loop and sleep in thread I have a Thread that calls a certain service. The service defines a callback function that can be called multiple times as long as there is data onProcessing(). The onCompletion() is called once it is finished. public CallThread implements Runnable{ public boolean isCompleted = false; public void run(){ ResponseObserver response = new ResponseObserver(){ public void onException(){ //Errors could happen sometimes so the program should just issue another request } public void onCompletion(){ isCompleted = true; //process the result } public void onProcessing(){ //This could be called multiple time } } //Service is the object that calls the service Service service = getService(); while(!isCompleted){ //Request object is populated Request req...

JAVA - how to modify SWT UI while looping in thread

JAVA - how to modify SWT UI while looping in thread I'm trying to implement a Desktop app that loops trough a function and sets a textfield on the UI every 1 sec. But I either get org.eclipse.swt.SWTException: Invalid thread access when I don't use the display or the UI is really sluggish when I do display.asyncExec(new Runnable() { My code looks like this: public void open() { Display display = Display.getDefault(); shell = new Shell(); shell.setSize(486, 322); shell.setText("SWT Application"); Button btnStartLoop = new Button(shell, SWT.NONE); btnStartLoop.addMouseListener(new MouseAdapter() { @Override public void mouseDown(MouseEvent e) { SwingUtilities.invokeLater(new Runnable() { public void run() // updates displayArea { while (true) { try { text.setText("Text has been set"); ...

Limit number of active threads for parts of code

Limit number of active threads for parts of code I have a multi threaded Java program that mostly works with Apache HTTP Components with proxies. However there is a section in the code that is related to uploading photos which takes up quite a bit of bandwidth and if I don't limit the concurrent threads at that section, I would start to get timeouts. Right now this is what I have, it only allows 1 thread to access the section of the code for uploading photos public class CommonUtil { private static final Object _uploadPhoto = new Object(); public void start(){ //other code synchronized (_uploadPhoto) { //all code related to photo upload } //other code } } The problem with the code above is that it does not consider the current proxy linked to the thread. I want my program to be able to not allow more than thread to upload photos only if it's the same proxy. In the program I do have the ability to check which proxy is being...

C++ thread error: no type named ‘type’ MINGW

C++ thread error: no type named ‘type’ MINGW I'm currently doing a project for my operational systems class and I have to make a program to find prime numbers to run in threads. So I did this: #include <iostream> #include <cmath> #include <thread> #define THREADNUMBER 100 using namespace std; int CONTADOR = 0; int CONTADORTHREADEXECUTADA = 0; int primeRange(int v1, int v2) { int a, limit; bool isprime; for (int i = v1; i <= v2; i++) { if (i % 2 != 0 && i % 3 != 0) { isprime = true; limit = i / 2; if(i == 1) isprime =false; limit = (int)sqrt(i); //General case for(a=2; a <= limit; a++){ if(i % i == 0 && i != 2){ isprime = false; break; } } if (isprime) { CONTADOR++; } } } CONTADORTHREADEXECUTADA++; return 1; } int main(int argc, char *argv[ ] ) { int number1 = atoi(argv[1]); int num...