Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As previously I get help in Database class from U

now I want to perform following task...

1. I have One class with name "pthread" which is creating thread.the threads are simply display its number

2. I have another class with name DBconnecter it simply get connected to the database.It is used to insert data in database

NOW HERE IS MY PROBLEM START

...I want to use DBconnecter class's object in pthread class
when my pthread class perform task it must to do this work

....my pthread class is creating thread.
the thread which creating by pthread class.
they must to enter some hard coded data into database.


how can I do that?

[edit]copied from misplaced solution:[/edit]

this is my DBconnector class

C++
/*
* DBconnector.cpp
*
*   Created on: Mar 1, 2012
*         Author: root
*/
 
#include "DBconnector.h"
#include <mysql.h>
#include <stdio.h>
#include <iostream>
 
MYSQL mysql,*connection;
MYSQL_RES *result;
MYSQL_ROW row;
 
char * ip = (char*)"192.168.49.131";
char * usr = (char*)"root";
char * pass = (char*)"admin";
char * db = (char*)"vcs";
 
unsigned fields;
using namespace std;
 
void DBconnector ()
{
     mysql_init(&mysql);
 
     connection = mysql_real_connect(&mysql, ip, usr, pass, db, 0, NULL, 0);
 
     if (connection==NULL)
     {
          std::cout << mysql_error(&mysql)<< std::endl;
     }
 
     else
     {
          if(mysql_query(&mysql, "INSERT INTO `vcs`.`users`   (`id`, `fname`, `sname`) VALUES (5, 'Rana', 'sameer')"));
 
          else
          {
               result = mysql_store_result(&mysql);
               fields = mysql_num_fields(result);
 
               while((row = mysql_fetch_row (result)))
               {
                    unsigned long *length;
                    length = mysql_fetch_lengths(result);
 
                    for(int i=0; i<fields; i++)
                    {
                         cout <<   length[i], row[i] , row[i]   ;
                         cout << "NULL";
                    }
                    cout << endl;
               }
          }
     }
}
 

namespace rumeel
{
 
     DBconnector::DBconnector()
     {
          // TODO Auto-generated constructor stub
 
     }
 
     DBconnector::~DBconnector()
     {
          // TODO Auto-generated destructor stub
     }
 
} /* namespace rumeel */




and this is my pthread class



C++
/*
* pThread.cpp
*
*   Created on: Mar 1, 2012
*         Author: root
*/
 
#include "pThread.h"
#include <iostream>
#include <pthread.h>
#include <stdio.h>
 
using namespace std;
 
     void * threadfunction (void *arg)
     {
          cout << "Thread Id = " <<   int(arg)<<endl;
 
          return (void *) 0;
     }
 
     int main()
     {
          DBconnector -> DBconnector();
          int j;
 

          int value;
          cout << "Enter The Number Of Threads :";
          cin >> value;
 
     for(int i=1 ; i <= value ;i++)
          {
               pthread_t thread;
 
               pthread_create( &thread, NULL, threadfunction, (void*) i);
          }
 
          cin>>j;
          return 0;
 
     }
 

 

 

namespace rumeel
{
 
     pThread::pThread()
     {
          // TODO Auto-generated constructor stub
 
     }
 
     pThread::~pThread()
     {
          // TODO Auto-generated destructor stub
     }
 
} /* namespace rumeel */


sorry for poor English
Posted
Updated 1-Mar-12 1:37am
v3
Comments
Stefan_Lang 1-Mar-12 7:38am    
moved the code to your question and unchecked 'treat as plain text'

1 solution

You may simply pass the object (the pointer to the object) you need to the thread function.
Beware, whenever you use threads you must use synchronization, that is you must protect your data from concurrent access (see, for instance, "POSIX thread (pthread) libraries"[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900