-
September 19th, 2013, 04:09 AM
#1
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;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|