Threads and functions calls

Hi,

 

i have One situation that i dont know how to solve.

 

i have two functions:

 

void function1()

{

// do something

}

void function2()

{

// do something

}

 

and i need to call both, but function2 needs that function1 to be finished first.

 

how can i call function2 only when function1 is finished ?

 

thanks,

Paulo 

 

 

WaitableEvent  event;

void function1()

{

// do something

event.signal();

}

void function2()

{

event.wait();

// do something

}

Thanks, I will test your solution.

Sounds like you're probably doing something silly to me.

Try asking your question again, but say what you're actually trying to achieve, rather than just asking about how to call functions. You'll probably get a better answer that way.

Hi Jules,

I'm working with SQLite databases and i have functions that will work with the databases.

What is happening is that when my software starts, needs to call a function that will create the database and populate with all needed tables.

My function is this way:

bool CreateDefaultDB(String DBPath) {
    char *sql;
    int rc = 0;
    sqlite3* pDb = NULL;
    sqlite3_stmt* query = NULL;
    
    File *fptr_db = new File(DBPath);
    
    if(fptr_db->existsAsFile())
    {
        
    } else {
        sqlite3_initialize( );
        rc = sqlite3_open_v2( DBPath.toRawUTF8(), &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
        if ( rc != SQLITE_OK) {
            sqlite3_close( pDb );
            exit( -1 );
        }

// ----------------------- TABLE USERS ---------------------------- //

        sql = (char*)&"CREATE TABLE IF NOT EXISTS users ('id' integer PRIMARY KEY NOT NULL, 'groupId' integer NOT NULL, 'name' VARCHAR(40) NOT NULL COLLATE NOCASE, 'password' VARCHAR(200) COLLATE NOCASE, 'address' VARCHAR(200) COLLATE NOCASE, 'postalCode' VARCHAR(20), vat VARCHAR(30), 'bi' INT, 'LastSale' DATE, 'limits' DECIMAL(12,6), 'commission' DECIMAL(12,6), 'birthDate' DATE, 'phone' VARCHAR(20), 'mobilePhone' VARCHAR(20), 'email' VARCHAR(200) COLLATE NOCASE, 'obs' VARCHAR, 'defaultSerieId' integer NOT NULL ); ";
        rc = sqlite3_prepare_v2( pDb, sql, -1, &query, NULL );
        if ( rc != SQLITE_OK) exit( -1 );
        rc = sqlite3_step( query );
        if ( rc != SQLITE_DONE ) exit ( -1 );


(....)

        sqlite3_finalize(query);

        sqlite3_close(pDb);
        sqlite3_shutdown();
    }
    
    return 0;
}

 

And after this function is called, depending on the option choosed, i need to populate the tables with demo data.

So I call second function, that is this way:

bool insertDemoData(String DBPath) {

    char *sql;
    int rc = 0;
    sqlite3* pDb = NULL;
    sqlite3_stmt* query = NULL;

    File *fptr_db = new File(DBPath);

    if (fptr_db->existsAsFile())
    {
        Logger::outputDebugString("DB not found");
    }
    else {
        sqlite3_initialize();
        rc = sqlite3_open_v2(DBPath.toRawUTF8(), &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
        if (rc != SQLITE_OK) {
            Logger::outputDebugString("Error stage 1");
            sqlite3_close(pDb);
            exit(-1);
        }

        // ----------------------- TABLE USERS ---------------------------- //

        sql = (char*)&"INSERT INTO [users] ([id], [groupId], [name], [password], [defaultSerieId]) VALUES (1, 0, 'Paulo', '1234', 0)";
        rc = sqlite3_prepare_v2(pDb, sql, -1, &query, NULL);
        if (rc != SQLITE_OK) exit(-1);
        rc = sqlite3_step(query);
        if (rc != SQLITE_DONE) exit(-1);


(....)

        sqlite3_finalize(query);

        sqlite3_close(pDb);
        sqlite3_shutdown();
    }

    return 0;
}

 

What is happening is that when I call the first function, it's created a thread, so when i call second one, the database does not exists, since the thread is not finished.

I tried to make a simple and direct question, to avoid to put code here and make it more complex.

 

Paulo

1 Like

sqlite uses threads internally? Sounds more like a sqlite question than a juce one.

(And yeesh.. that's some truly terrible code you've written! You should never create a File object with 'new' - if you don't learn to use C++ types properly, you're really going to struggle to make anything work)

Regarding coding style and 'new', only now i noticed that what i posted was old code, new one is this way:

int ret = 0;
    String DBPath = getStoragePath();
    String szSQL;
    sqlite3* pDb = NULL;
    sqlite3_stmt* query = NULL;

    UserData.name = "";

    File fptr_db(DBPath);

    if (fptr_db.existsAsFile())
    {

...

 

 

Better.. But you should never use NULL any more, use nullptr. And "fptr_db" would be a bad name even if the object actually was a pointer!

And the code you where you take the address of a string literal and c-style cast it to a char*.. that alone would get you sacked from a lot of C++ jobs!

Yes, your're right, I don't know what i had in mind to put NULL in a pointer.

regarding char*, that was also the old part, I'm doing now this way:

String szSQL;

szSQL = "SELECT * FROM users WHERE id = '" + String(userid) + "';";

ret = sqlite3_prepare_v2(pDb, szSQL.getCharPointer(), -1, &query, NULL);

Hope it's a correct way, at least I have no leaks.

Thanks for your advices.

Much better, but lose those NULLs! smiley