- 论坛徽章:
- 0
|
第一个占用180m,第二个占用108m ,怎么这么大阿,代码没问题吧
编译 gcc t.c -lmysqlclient -lpthread- #include <stdio.h>
- #include <pthread.h>
- #include <mysql/mysql.h>
- void *test1( void *arg )
- {
- char sql[1024];
- MYSQL_RES *results;
- MYSQL_ROW record;
- unsigned long id = 0;
- MYSQL *mysql;
- int mysql_timeout = 10;
-
- mysql = mysql_init( NULL );
- mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );
- if ( !mysql_real_connect( mysql, "localhost", "root", "123456","mydb", 0, NULL, 0 ) )
- {
- printf("error");
- exit(1);
- }
-
- while( 1 )
- {
- snprintf( sql, 1024, "select * from test limit %ld,10", id ++ );
-
- if ( mysql_query( mysql, sql ) != 0 )
- {
- printf( "1 mysql:%s\n", mysql_error( mysql ) );
- }
-
- results = mysql_store_result( mysql );
- if ( results == NULL )
- {
- printf( "2 mysql:%s\n", mysql_error( mysql ) );
- }
- while ( (record = mysql_fetch_row( results ) ) )
- {
- printf("%s - %s\n", record[0], record[1] );
- }
- if ( mysql_num_rows( results ) == 0 )
- {
- id = 0;
- }
-
- mysql_free_result( results );
- }
- return arg;
- }
- void *test2( void *arg )
- {
- char sql[1024], str[1024];
- int id = 0;
- MYSQL *mysql;
- int mysql_timeout = 10;
-
- mysql = mysql_init( NULL );
- mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );
- sleep( 2 );
-
- if ( !mysql_real_connect( mysql, "localhost", "root", "123456","mydb", 0, NULL, 0 ) )
- {
- printf("error");
- exit(1);
- }
-
- while ( 1 )
- {
- snprintf( str, 1024, "wangwei %d", id );
-
- snprintf( sql, 1024, "insert into test values (%ld,'%s')", id ++, str );
- if ( mysql_query( mysql, sql ) != 0 )
- {
- printf( "1 mysql:%s\n", mysql_error( mysql ) );
- }
-
- printf("+");
- }
-
- return arg;
- }
- main()
- {
- int status;
- pthread_t id1, id2;
- printf("mysql_thread_safe = %d\n",mysql_thread_safe());
-
- status = pthread_create( &id1, NULL, test1, NULL );
-
- if (status != 0)
- {
- perror("thread1");
- exit(0);
- }
- status = pthread_create( &id2, NULL, test2, NULL );
- if (status != 0)
- {
- perror("thread2");
- exit(0);
- }
- while(1);
- }
复制代码- #include <stdio.h>
- #include <pthread.h>
- #include <mysql/mysql.h>
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
- void *test1( void *arg )
- {
- char sql[1024];
- MYSQL_RES *results;
- MYSQL_ROW record;
- unsigned long id = 0;
- MYSQL *mysql;
- int mysql_timeout = 10;
-
- mysql = ( MYSQL *) arg;
- //mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );
-
- while( 1 )
- {
- pthread_mutex_lock( &mutex );
-
- snprintf( sql, 1024, "select * from test limit %ld,1", id ++ );
-
- if ( mysql_query( mysql, sql ) != 0 )
- {
- printf( "1 mysql:%s\n", mysql_error( mysql ) );
- }
-
- results = mysql_store_result( mysql );
- if ( results == NULL )
- {
- printf( "2 mysql:%s\n", mysql_error( mysql ) );
- }
- while ( (record = mysql_fetch_row( results ) ) )
- {
- printf("%s - %s\n", record[0], record[1] );
- }
- if ( mysql_num_rows( results ) == 0 )
- {
- id = 0;
- }
-
- mysql_free_result( results );
-
- pthread_mutex_unlock( &mutex );
- }
- return arg;
- }
- void *test2( void *arg )
- {
- char sql[1024], str[1024];
- int id = 0;
- MYSQL *mysql;
- int mysql_timeout = 10;
- mysql = ( MYSQL *) arg;
- //mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );
- sleep( 2 );
- while ( 1 )
- {
- pthread_mutex_lock( &mutex );
- snprintf( str, 1024, "wangwei %d", id );
-
- snprintf( sql, 1024, "insert into test values (%ld,'%s')", id ++, str );
- if ( mysql_query( mysql, sql ) != 0 )
- {
- printf( "1 mysql:%s\n", mysql_error( mysql ) );
- }
-
- printf("+");
-
- pthread_mutex_unlock( &mutex );
- }
-
- return arg;
- }
- main()
- {
- int status;
- pthread_t id1, id2;
- MYSQL *mysql;
-
- printf("mysql_thread_safe = %d\n",mysql_thread_safe());
-
- mysql = mysql_init( NULL );
- if ( !mysql_real_connect( mysql, "localhost", "root", "123456","mydb", 0, NULL, 0 ) )
- {
- printf("error");
- exit(1);
- }
-
- status = pthread_create( &id1, NULL, test1, (void*)mysql );
-
- if (status != 0)
- {
- perror("thread1");
- exit(0);
- }
- status = pthread_create( &id2, NULL, test2, (void*)mysql );
- if (status != 0)
- {
- perror("thread2");
- exit(0);
- }
- while(1);
- }
复制代码 |
|