BUG #7841: Multithreading in libpq is not working

From: biernatj(at)interia(dot)pl
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #7841: Multithreading in libpq is not working
Date: 2013-01-31 09:49:29
Message-ID: E1U0qlt-0005um-0d@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 7841
Logged by: Joanna Biernat-Gerono
Email address: biernatj(at)interia(dot)pl
PostgreSQL version: 9.2.2
Operating system: Windows 7
Description:

Sample code that provides to access violation (I am using the latest libpq
that is compiled with multithread support, connection was made to two
different dbs). The code is below:

#define WINNT_OS
#include <Windows.h>
#include <iostream>
#include "libpq-fe.h"
#include <signal.h>

#define STD__NULL NULL
#define STD__SUCCESS 0
#define STD__FAILED -1
#define STD__FAIL -1

#define TIMEOUT 100
using namespace std;

volatile bool m_bLicenseCheckedOut = false;

enum status
{
idle,
running,
stop,
done
} m_running = idle;

int m_renewalTime = 0;
HANDLE m_hThread = NULL;
HANDLE m_hEvent = NULL;

DWORD WINAPI threadProc(LPVOID lpvoid)
{

int step = 0;
while(m_running == running) {

PGconn *conn;

try {

const char *keys[]={"dbname", "user", "password", "host", "port",
"sslcert", "sslkey", "sslrootcert", "sslmode", NULL};
const char *values[]={"put_value_here", "put_value_here" ,
"put_value_here" , "put_value_here" , "put_value_here" , "put_value_here",
"put_value_here", "put_value_here" ,"verify-full", NULL};

// conn = PQconnectdbParams(keys,values,0);

//conn = PQsetdbLogin("put_value_here", "put_value_here", NULL,
NULL, "dbname=put_value_here sslcert=put_value_here sslkey=put_value_here
sslrootcert=put_value_here sslmode=verify-full", "put_value_here" ,
"put_value_here");
conn = PQsetdbLogin("put_value_here", "put_value_here", NULL, NULL,
"put_value_here", "put_value_here" , "put_value_here");
long status = PQstatus(conn);

if (status == CONNECTION_OK) {

cout << "\nSuccessfully connected to DB:"<<step++ <<" ";
Sleep(1);
} else {

cout << "\nNot connected to ATR_DB";
cout << PQerrorMessage(conn);

}

} catch (...) {
cout << "Exception caught";
}

if(STD__NULL != conn)
{

PQfinish(conn);
conn = STD__NULL;
}

//wait before renewing again
// WaitForSingleObject(m_hEvent, TIMEOUT);

}
m_running = done;
return STD__SUCCESS;

}

/*
void handler(int sig) {

printf("Signal %d\n",sig);

}
*/

int DoSelect(int step)
{

PGconn *conn;

char conninfo[] = "dbname=put_value_here host=put_value_here
port=put_value_here user=put_value_here password=put_value_here
sslmode=verify-full sslcert=put_value_here sslkey=put_value_here
sslrootcert=put_value_here" ;

// conn = PQsetdbLogin("put_value_here", "put_value_here", NULL, NULL,
"dbname=put_value_here sslmode=verify-full sslcert=put_value_here
sslkey=put_value_here sslrootcert=put_value_here", "put_value_here",
"put_value_here");
conn = PQsetdbLogin("put_value_here", "put_value_here", NULL, NULL,
"put_value_here", "put_value_here", "put_value_here");
// conn = PQconnectdb(conninfo);
// long status = 1;
long status = PQstatus(conn);
int r = PQisthreadsafe();

if (status == CONNECTION_OK)
{

cout << "\nSuccesfully connected to MOTOLM DB: "<< step <<" \n";

} else {

cout << "\nNot connected to MOTOLM DB\n";
cout << PQerrorMessage(conn);

}

if(STD__NULL != conn)
{

PQfinish(conn);
conn = STD__NULL;
}
return 0;
}

int main (int argc,char** args)
{

// int ts = PQisthreadsafe();

// cout << "PQisthreadsafe = " << ts;
//typedef void (*SignalHandlerPointer)(int);
//SignalHandlerPointer previousHandler;
//previousHandler = signal(SIGSEGV, handler); // install our handler

cout << "PROGRAM STARTED"<< endl;

m_hEvent = CreateEvent(0,false,0,0);
if (NULL == m_hEvent)
{
cout << "CreateEvent FAILED" << endl;
return STD__FAILED;
}

m_running = running;
m_hThread = CreateThread(NULL, 0, threadProc, 0,0, NULL);
if (NULL == m_hThread)
{
m_running = done;
cout << "CreateThread FAILED" << endl;
return STD__FAILED;
}

if( FALSE == SetThreadPriority(m_hThread,
THREAD_PRIORITY_TIME_CRITICAL))
{
cout << "SetThreadPriority FAILED" << endl;
return STD__FAILED;
}

int i = 0;

while (i < 5000) {

DoSelect(i++);
Sleep(1);

}

m_running = stop;

cout << "done." << endl;

return STD__SUCCESS;

}

Browse pgsql-bugs by date

  From Date Subject
Next Message lopuszanski 2013-01-31 15:01:55 BUG #7842: pg_dump scripts view to table
Previous Message digoal 2013-01-31 09:31:33 BUG #7840: PostgreSQL 9.3 devel select for no key share lock bug?