Is it good/safe/reliable to do so, and why?
int main()
{
MYSQL *mysqldbconn = mysql_init(NULL);
if(mysqldbconn != NULL)
{
if(mysql_real_connect(mysqldbconn, host,
user, password,
NULL, port, NULL, 0) != NULL)
{
pid_t pid = fork();
if(pid >= 0)
{
if(pid == 0)
{
mysql_query(mysqldbconn, "insert into `test`.`abc`(`x`, `y`) values (1, 2)");
exit(0);
}
else
{
mysql_query(mysqldbconn, "insert into `test`.`abc`(`x`, `y`) values (3, 4)");
usleep(1000000);
}
}
}
}
return 0;
}
I am wondering if I should open a new connection for every child process, or I can simply connect once at the beginning and then use it in every forked child.
Is it possible that the usage example above leads to dangerous data races? Is mysql_query
atomic?
Thank you for your advices.