Hi, everyone.
I CREATE TABLE foo VALUE(name chare(20), email char(30)) type=InnoDB
and I inserted some records.

And then I made simple program using just two threads except main thread.
Let's call the first A thread and the second B thread.
Main thread just runs these threads.

Thread A inserts just one record, sleep(10) and commit. ( Use transaction )

Thread B just select * from foo.

So simple.

When executes this sample, there is segmentation fault.

What's wrong with that?

For reference I add my source.


============================================================================
===========
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "mysql.h"

#define MAXQUERYLEN 50

void *thread_func1(void *arg)
{
        char query[MAXQUERYLEN];
        MYSQL *mysql = (MYSQL*)arg;

        snprintf(query, MAXQUERYLEN, "begin");
        mysql_query(mysql, query);

        snprintf(query, MAXQUERYLEN, "insert into foo values(\'thread\',
\'thr\')");
        mysql_query(mysql, query);

        sleep(10);

        snprintf(query, MAXQUERYLEN, "commit");
        mysql_query(mysql, query);
}

void *thread_func2(void *arg)
{
        char query[MAXQUERYLEN];
        MYSQL *mysql = (MYSQL*)arg;
        MYSQL_RES *res;
        MYSQL_ROW row;
        int num;

        snprintf(query, MAXQUERYLEN, "SELECT COUNT(*) FROM foo");
        mysql_query(mysql, query);
        res = mysql_use_result(mysql);
        row = mysql_fetch_row(res);
        num = atoi(row[0]);
        printf("SELECT COUNT(*) FROM foo : %d\n", num);
}

void main(void)
{
        MYSQL mysql;
        pthread_t thread1, thread2;

        mysql_init(&mysql);

        if(!mysql_real_connect(&mysql, "localhost", NULL, NULL, NULL, 0, 
        
"/tmp/mysql_test.sock", 0))
        {
                printf("connect error\n");
                exit(1);
        }

        if(mysql_select_db(&mysql, "test"))
        {
                printf("selection failed\n");
                exit(1);
        }

        pthread_create(&thread1, NULL, thread_func1, &mysql);
        pthread_create(&thread2, NULL, thread_func2, &mysql);

        pthread_exit(NULL);

        mysql_close(&mysql);

        return 0;
}

============================================================================
===========


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to