小编Cik*_*Cik的帖子

在多线程环境中使用MySQL C++ Connector

这是关于我提出的有关MySQL C++ Connector的问题以及我在多线程环境中使用的问题.

我有一个产生100个线程的应用程序.是的,我产生了100个,因为除了数据库更新之外,他们中的大多数都会忙于I/O或其他任务.

  1. 我有一个全局sql::Driver * driver变量,整个应用程序都可以看到它.我driver = get_driver_instance();在应用程序的一开始就打电话.
  2. 我有一个sql::Connection指针数组,connArray又是全局的,在产生任何线程之前,我为第i个线程打开ith连接(使用driver),然后调用con->setSchema.我将此连接存储在connArray数组的第i个位置.

现在,这大致是我在我的应用程序中执行的操作(粗略代码):

bool InsertFunction(int threadId)
{
    driver->threadInit(); //Driver is global
    if((connArray[threadId] == NULL) || (connArray[threadId] == NULL && connArray[threadId]->isClosed())
    {
        //Re-Create the connection and store in the connArray
    }
    else
    {
         Create Prepared Statement for Insert
         Set Values
         Execute Update
    }
    driver->threadEnd();
}

bool UpdateFunction(int threadId)
{
    driver->threadInit(); //Driver is global
    if((connArray[threadId] == NULL) || (connArray[threadId] == NULL && …
Run Code Online (Sandbox Code Playgroud)

c++ mysql boost

5
推荐指数
1
解决办法
3013
查看次数

c_str()在C++中的线程安全性

我创建了一个类SettingsClass,它包含静态字符串,用于保存MySQL C++连接器库使用的db连接字符串(例如hostname,dbname,username,password).

只要函数需要连接到数据库,它就会在这些静态字符串上调用.c_str()函数.例如:

Class SettingsClass
{
    public:
    static string hostname;
    ...
}SettingsClass;
string SettingsClass::hostname;

//A function that needs to connect to the DB uses:
driver = get_griver_instance();
driver->connect(SettingsClass.hostname.c_str(),...);
Run Code Online (Sandbox Code Playgroud)

静态字符串在进程生命周期中填充一次.其值从配置文件中读取.

我的应用程序是多线程的.我是否以安全的方式使用c_str()?

c++ multithreading stl

4
推荐指数
1
解决办法
1591
查看次数

标签 统计

c++ ×2

boost ×1

multithreading ×1

mysql ×1

stl ×1