CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2013
    Posts
    4

    Lightbulb 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;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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 '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Sep 2013
    Posts
    4

    Red face 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:cci::Environment::createEnvironment(oracle:cci::Environment::OBJECT);

    and the solution is
    change the above statement to

    env = oracle:cci::Environment::createEnvironment(oracle:cci::Environment::THREADED_MUTEXED);

    because for occi to support threading we need to declare like this..
    Thanks

  4. #4
    Join Date
    Sep 2013
    Posts
    4

    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..

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  6. #6
    Join Date
    Oct 2013
    Posts
    1

    Re: Program is ending before the thread is executed.

    helpful thread! thanks

    www.mypartyproduction.com

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
  •  





Click Here to Expand Forum to Full Width

Featured