Program is ending before the thread is executed.
Hi,
I have written a multi threading application, in this the program is ending before I am getting the results of the threads, not all the times but some times and some times it is crashing and some times I am getting the expected output. I struck here, Can anyone help me on this please.
Thanks in advance,..:)
and my code is.
#include"stdafx.h"
#include<iostream>
#include <process.h>
using namespace std;
#include <Windows.h>
#include <math.h>
#include"thread_testing.h"
typedef struct db_results S;
const char tag[4]="con";
DBClass* DBClass::obj = NULL;
string DBClass::username;
string DBClass::passWord;
bool DBClass::InstanceFlag=false;
DBClass* DBClass::GetInstance(){
if(!InstanceFlag){
obj = new DBClass();
InstanceFlag= true;
return obj;
}
else {
return DBClass::obj;
}
}
DBClass::DBClass(){
env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::OBJECT);
string poolUserName = "edi";
string poolPassword = "edi123";
string connectString = "//10.161.13.110:1521/EDIDB";
DBClass::username = "edi";
DBClass::passWord = "edi123";
unsigned int maxConn = 2;
unsigned int minConn = 1;
unsigned int incrConn = 1;
connPool = env->createStatelessConnectionPool(poolUserName, poolPassword, connectString, maxConn, minConn, incrConn, StatelessConnectionPool::HETEROGENEOUS);
try{ // Try Block starts.
if (connPool){
cout<<"Stateless Connection Pool created successfully"<<endl;
}
else
cout << "fail";
/*stmt = con->createStatement();*/
} //End of try Block.
catch(SQLException ex)
{
cout<<"Exception thrown for createConnectionPool"<<endl;
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
exit(3);
} // End of catch Block.
catch(...)
{
cout<<"Exception thrown for createConnectionPool"<<endl;
cout<<"Error number: " << endl;
exit(3);
} // End of catch B
}
void DBClass::isEven()
{
try{
Statement *stmt;
Connection *con;
con = connPool->getConnection(DBClass::username, DBClass::passWord, tag);
if (!con)
cout <<"connection failed"<< endl;
cout<<endl<<"Executing ";
}
catch (SQLException ex){
cout<<"Exception";
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
}
catch(...)
{
cout<<"Exception thrown for createConnectionPool"<<endl;
cout<<"Error number: " << endl;
}
cout<<endl<<"Session Done";
}
void DBClass::isPrime()
{
try{
Statement *stmt1;
Connection *con1;
con1 = connPool->getConnection(DBClass::username, DBClass::passWord, tag);
if (!con1)
cout <<"connection failed"<< endl;
cout<<endl<<"Executing";
// i have to write some logic
}
catch (SQLException ex){
cout<<"Exception";
cout<<"Error number: "<< ex.getErrorCode() << endl;
cout<<ex.getMessage() << endl;
}
catch(...)
{
cout<<"Exception thrown for createConnectionPool"<<endl;
cout<<"Error number: " << endl;
}
cout<<endl<<"Lot Done";
}
DBClass* demo=DBClass::GetInstance();
unsigned int __stdcall mythreadB(void*)
{
demo->isEven();
return 0;
}
unsigned int __stdcall mythreadC(void*)
{
demo->isPrime();
return 0;
}
int main(int argc, char* argv[])
{
HANDLE /*myhandleA,*/ myhandleB, myhandleC;
myhandleB = (HANDLE)_beginthreadex(0, 0, &mythreadB, (void*)0, 0, 0);
myhandleC = (HANDLE)_beginthreadex(0, 0, &mythreadC, (void*)0, 0, 0);
CloseHandle(myhandleB);
CloseHandle(myhandleC);
getchar();
return 0;
}
Re: Program is ending before the thread is executed.
Please format the code and use code tags when posting code. Your code as posted is just about unreadable. Go Advanced, select the code and then click '#'.
Re: Program is ending before the thread is executed.
Hey... Thanks for your reply.. I have solved the problem..
The problem is with
env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::OBJECT);
and the solution is
change the above statement to
env = oracle::occi::Environment::createEnvironment(oracle::occi::Environment::THREADED_MUTEXED);
because for occi to support threading we need to declare like this..
Thanks
Re: Program is ending before the thread is executed.
Hi,
Thanks for your reply, I will make my code readable from next time onwards..:)
I have a doubt in multi-threading.. We can able to run multiple threads based on single connection then what is the need of connection pooling and what will be the advantage of connection pooling in case of multi threading.
Please help me on this..:)
Thanks..:)
Re: Program is ending before the thread is executed.
if your main thread exits (typically when your main() returns, all other threads in the process will be forcefully terminated.
If you need results, you need to explicitely provide synchronisation code to wait for those results from the worker threads.
Re: Program is ending before the thread is executed.
helpful thread! thanks :)
www.mypartyproduction.com