/* on linux, you can compile with:
ecpg cn.pgc&& gcc -I/usr/local/pgsql/include -L/usr/local/pgsql/lib -lpgtypes -lecpg -lpthread cn.c

and you'll need this table:

CREATE TABLE foo (
    message character(40)
);

*/


#include <pthread.h>

int main();
void ins1();
void ins2();

int main()
{
	pthread_t thread1, thread2;
	pthread_create(&thread1, NULL, (void *) ins1, NULL);
	pthread_create(&thread2, NULL, (void *) ins2, NULL);
	pthread_join(thread1, NULL);
	pthread_join(thread2, NULL);
	printf("Done!");
	return 0;
}

void ins1()
{
	int i;
	EXEC SQL BEGIN DECLARE SECTION;
	char* cs = "test@host";
	char* bar = "one!";
	EXEC SQL END DECLARE SECTION;
	EXEC SQL WHENEVER sqlerror sqlprint;
	EXEC SQL CONNECT TO :cs AS test1;
	EXEC SQL SET AUTOCOMMIT TO ON;
	for (i = 0; i < 5; i++)
	{
		printf("thread1 inserting\n");
		EXEC SQL INSERT INTO foo VALUES(:bar);
		printf("==>thread1 insert done\n");
	}
	EXEC SQL DISCONNECT test1;
	printf("done!\n");
}


void ins2()
{
	int i;
	EXEC SQL BEGIN DECLARE SECTION;
	char* cs = "test@host";
	char* bar = "two!";
	EXEC SQL END DECLARE SECTION;
	EXEC SQL WHENEVER sqlerror sqlprint;
	EXEC SQL CONNECT TO :cs AS test2;
	EXEC SQL SET AUTOCOMMIT TO ON;
	for (i = 0; i < 5; i++)
	{
		printf("thread2 inserting\n");
		EXEC SQL INSERT INTO foo VALUES(:bar);
		printf("==>thread2 insert done\n");
	}
	EXEC SQL DISCONNECT test2;
	printf("done!\n");
}

