Hi Santosh
Yes you are right, i think too it's not a problem of thread.
What's happening when that API(third party function) using in my application, it's stuck, mean it's not finishing execution,(stoping the execution in mid-way).
I have used the API, where you have suggest me in your demo code. And API will return any value when it will finish the execution either, but it's not coming out of that API.
But when same API, i have tried with only C, in one thread the api is executing and in another thread something printing in console it's working properly. As, API has written in C. I am using (extern "C").
i am giving you both demo code:
C-Code:
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>
void *tfunc(void *arg);
char msg[]="Hai Thread testing";
int run_var=1;
int selection=0;
int main()
{
pthread_t thread;
int res,count=0;
void *thr_result;
res=pthread_create(&thread,NULL,tfunc,(void *)msg);
if(res!=0){printf("\n thread failed\n");exit(0);}
while(count < 30)
{
printf("main\n");
count++;
sleep(1);
}
res=pthread_join(thread,&thr_result);
if(res!=0){printf("\n Join failed\n");exit(0);}
return 0;
}
void *tfunc(void *arg)
{
int count=0;
printf("\n running thread %s\n",(char *)arg);
sleep(1);
strcpy(msg,"Exiting");
printf("in thread\n");
third_party_function(); // this is my third party API
pthread_exit("THank u");
}
C-Code:
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>
void *tfunc(void *arg);
char msg[]="Hai Thread testing";
int run_var=1;
int selection=0;
int main()
{
pthread_t thread;
int res,count=0;
void *thr_result;
res=pthread_create(&thread,NULL,tfunc,(void *)msg);
if(res!=0){printf("\n thread failed\n");exit(0);}
while(count < 30)
{
printf("main\n");
count++;
sleep(1);
}
res=pthread_join(thread,&thr_result);
if(res!=0){printf("\n Join failed\n");exit(0);}
return 0;
}
void *tfunc(void *arg)
{
int count=0;
printf("\n running thread %s\n",(char *)arg);
sleep(1);
strcpy(msg,"Exiting");
printf("in thread\n");
third_party_function(); // this is my third party API
pthread_exit("THank u");
}
To copy to clipboard, switch view to plain text mode
Now my Qt-Application code:
i am calling qt-thread in one of my slot.
myclass.h
{
Q_OBJECT
public:
private:
private slots:
int executionSlot();
};
{
Q_OBJECT
public:
explicit MyThread
(QObject *parent
= 0) signals:
void dataReceived();
protected:
void run();
};
class MyClass : public QDialog
{
Q_OBJECT
public:
MyClass(QWidget *parent = 0);
private:
QPushButton *startButton;
private slots:
int executionSlot();
};
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0)
: QThread(parent){}
signals:
void dataReceived();
protected:
void run();
};
To copy to clipboard, switch view to plain text mode
myclass.cpp
void MyThread::run()
{
qDebug("thread running now");
while(1)
{
qDebug("In thread....before API");
int ret = third_party_function(); // this is my third party API
// If in place of API, i am using this for loop, for debugging, it's working fine
/*
for(int i=0; i<10; i++){
qDebug("opening modem");
sleep(1);
} */
//If i am using API, it's not coming here.
qDebug("In thread....after API");
emit dataReceived();
break;
}
qDebug("thread finishing now");
}
int MyClass::executionSlot()
{
qDebug("creating prog dlg");
prgDialog prgDlg; //This dialog i have created for showing busy wait proccessing, animation type
MyThread thread;
prgDlg.connect(&thread, SIGNAL(dataReceived()), &prgDlg, SLOT(close()));
thread.start();
thread.moveToThread(&thread);
prgDlg.exec();
qDebug("exiting slot...");
return 0;
}
void MyThread::run()
{
qDebug("thread running now");
while(1)
{
qDebug("In thread....before API");
int ret = third_party_function(); // this is my third party API
// If in place of API, i am using this for loop, for debugging, it's working fine
/*
for(int i=0; i<10; i++){
qDebug("opening modem");
sleep(1);
} */
//If i am using API, it's not coming here.
qDebug("In thread....after API");
emit dataReceived();
break;
}
qDebug("thread finishing now");
}
int MyClass::executionSlot()
{
qDebug("creating prog dlg");
prgDialog prgDlg; //This dialog i have created for showing busy wait proccessing, animation type
MyThread thread;
prgDlg.connect(&thread, SIGNAL(dataReceived()), &prgDlg, SLOT(close()));
thread.start();
thread.moveToThread(&thread);
prgDlg.exec();
qDebug("exiting slot...");
return 0;
}
To copy to clipboard, switch view to plain text mode
I have not written full code, because it's very big code, i have just written that section of code where i am getting problem. When i am using that for-loop for some time then it's working fine properly, mean processing dialog appear and after finishing that thread, processing dialog will disappear too . But when i am using the API, processing dialog appears, thread started to run too, but when it's reaching the API, it's not coming out of that API and so processing dialog too running infinite.
But this is okay with c-code.
If you are getting my problem by this .... can you suggest me where i am doing wrong. Thank you in advance !
Bookmarks