Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4826866
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: Mysql/postgreSQL

2010-04-12 18:01:48

#include
#include
#include
#include
#include
#include "mysql.h"
const char* host = "localhost";
const char* user = "root";
const char* password = "123456";
const char* database = "test";
static unsigned int thread_count;
static unsigned int number_of_tests=10;
static unsigned int number_of_threads=2;
static pthread_cond_t COND_thread_count;
static pthread_mutex_t LOCK_thread_count;
void* test_thread(void *arg)
{
  MYSQL *mysql;
  MYSQL_ROW row;
  int t;
  unsigned int count;
  char query[50];
  int flag = *((int *)arg);
 
  //pthread_mutex_lock(&LOCK_thread_count);
  mysql=mysql_init(NULL);
  if (!mysql_real_connect(mysql,host,user,password,database, 0, NULL, 0))
  {
    fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(mysql));
    goto end;
  }
  mysql->reconnect= 1;
 
  for (count=0 ; count < number_of_tests ; count++)
  {
    MYSQL_RES *res;
    if(flag&1)
     sprintf(query, "insert into thread values(%d)", count+1);
    else
      sprintf(query, "select * from thread where id=%d", count+1);
   
    printf("query is %s\n", query);  
    if (mysql_query(mysql,query))
    {
      fprintf(stderr,"Query failed (%s)\n",mysql_error(mysql));
      goto end;
    }
    if((flag&1)==0)
    {
     if (!(res=mysql_store_result(mysql)))
      {
       fprintf(stderr,"Couldn't get result from %s\n", mysql_error(mysql));
        goto end;
       }
      while((row = mysql_fetch_row(res))!=NULL)  
      {  
       for(t=0;t            printf("%s ",row[t]);  
       printf("\n");  
      }  
     mysql_free_result(res);
    }
  }
  //pthread_mutex_unlock(&LOCK_thread_count);
 
end:
  mysql_close(mysql);
  pthread_mutex_lock(&LOCK_thread_count);
  thread_count--;
  pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */
  pthread_mutex_unlock(&LOCK_thread_count);
  pthread_exit(0);
  return NULL;
 }
 
int main(int argc, char **argv)
{
  pthread_t tid;
  pthread_attr_t thr_attr;
  unsigned int i;
  int error;
 
  if ((error=pthread_cond_init(&COND_thread_count,NULL)))
  {
    fprintf(stderr,"Got error: %d from pthread_cond_init (errno: %d)",error,errno);
    exit(1);
  }
  pthread_mutex_init(&LOCK_thread_count,NULL);
  if ((error=pthread_attr_init(&thr_attr)))
  {
    fprintf(stderr,"Got error: %d from pthread_attr_init (errno: %d)",
     error,errno);
    exit(1);
  }
  if ((error=pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED)))
  {
    fprintf(stderr,
     "Got error: %d from pthread_attr_setdetachstate (errno: %d)",
     error,errno);
    exit(1);
  }
  printf("Init ok. Creating %d threads\n",number_of_threads);
  for (i=1 ; i <= number_of_threads ; i++)
  {
    int *param= (int *)&i;
    pthread_mutex_lock(&LOCK_thread_count);
    if ((error=pthread_create(&tid,&thr_attr,test_thread,(void*) param)))
    {
      fprintf(stderr,"\nGot error: %d from pthread_create (errno: %d) when creating thread: %i\n",
       error,errno,i);
      pthread_mutex_unlock(&LOCK_thread_count);
      exit(1);
    }
    thread_count++;
    pthread_mutex_unlock(&LOCK_thread_count);
  }
  printf("Waiting for threads to finnish\n");
  error=pthread_mutex_lock(&LOCK_thread_count);
  while (thread_count)
  {
    if ((error=pthread_cond_wait(&COND_thread_count,&LOCK_thread_count)))
      fprintf(stderr,"\nGot error: %d from pthread_cond_wait\n",error);
  }
 
  pthread_mutex_unlock(&LOCK_thread_count);
  pthread_attr_destroy(&thr_attr);
  pthread_cond_destroy(&COND_thread_count);
  pthread_mutex_destroy(&LOCK_thread_count);
  return 0;     /* Keep some compilers happy */
}
 
编译执行结果
[root@kenthy mysql]# gcc -Wall -I/usr/include/mysql -o mysql_thread mysql_thread.c -L/usr/lib/mysql/ -lmysqlclient
[root@kenthy mysql]# ./mysql_thread
Init ok. Creating 2 threads
Waiting for threads to finnish
query is insert into thread values(1)
query is insert into thread values(2)
query is insert into thread values(3)
query is insert into thread values(4)
query is insert into thread values(5)
query is insert into thread values(6)
query is insert into thread values(7)
query is insert into thread values(8)
query is insert into thread values(9)
query is insert into thread values(10)
query is select * from thread where id=1
1
query is select * from thread where id=2
2
query is select * from thread where id=3
3
query is select * from thread where id=4
4
query is select * from thread where id=5
5
query is select * from thread where id=6
6
query is select * from thread where id=7
7
query is select * from thread where id=8
8
query is select * from thread where id=9
9
query is select * from thread where id=10
10
阅读(1815) | 评论(0) | 转发(0) |
0

上一篇:三表以上查询

下一篇:MySQL连接取值

给主人留下些什么吧!~~