关于mysql多线程访问串行与并行的问题
InitConnection (4); //初始化连接池,放入4个mysql句柄
for(int i=0;i<4;i++)
{
mysql = GetConnection (); //获取一个MYSQL句柄
Insert (mysql ,"insert children values(6,'yantai',40)"); //执行插入
}
我想问下这个程序到底是并行,还是串行的?
InitConnection (4);
for(int i=0; i<4;i ++)
{
mysql = GetConnection ();
for( int i = 0; i < 5; i ++)
{
pthread_create(&thread_id [i], NULL, Testinsert ,mysql);
}
for(int i = 0; i < 5; i ++)
{
pthread_join(thread_id [i], NULL);
}
}
那么这四个连接是并行还是串行?四个连接并行?四个连接并行五次?
InitConnection (4);
for(int i=0; i<1;i ++)
{
mysql = GetConnection ();
for( int i = 0; i < 1; i ++)
{
pthread_create(&thread_id [i], NULL, Testinsert ,mysql);
}
for(int i = 0; i < 1; i ++)
{
pthread_join(thread_id [i], NULL);
}
}
这应该是4个连接并行一次,这和第一个程序有区别么?
串行和并行不是太清楚,希望大家点拨一下。谢谢
[解决办法]
1.
InitConnection (4); //初始化连接池,放入4个mysql句柄
for(int i=0;i<4;i++)
{
mysql = GetConnection (); //获取一个MYSQL句柄
Insert (mysql ,"insert children values(6,'yantai',40)"); //执行插入
}
InitConnection (4);
for(int i=0; i<4;i ++)
{
mysql = GetConnection ();
for( int i = 0; i < 5; i ++)
{
pthread_create(&thread_id [i], NULL, Testinsert ,mysql);
}
for(int i = 0; i < 5; i ++)
{
pthread_join(thread_id [i], NULL);
}
}
那么这四个连接是并行还是串行?四个连接并行?四个连接并行五次?
InitConnection (4);
for(int i=0; i<1;i ++)
{
mysql = GetConnection ();
for( int i = 0; i < 1; i ++)
{
pthread_create(&thread_id [i], NULL, Testinsert ,mysql);
}
for(int i = 0; i < 1; i ++)
{
pthread_join(thread_id [i], NULL);
}
}
这应该是4个连接并行一次,这和第一个程序有区别么?
InitConnection (4);
for(int i=0; i<4;i++){
mysql = GetConnection ();
pthread_create(&thread_id[i], NULL, Testinsert ,mysql);
}
for(int i = 0; i < 4; i++){
pthread_join(thread_id[i], NULL);
}