Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在多线程中编辑字符串 我对C++是更新的,我知道C++中的多线程支持很差。 但是现在我必须在多线程中编辑字符串val,我使用线程互斥来保护val。问题是程序在运行时总是核心转储_C++_String - Fatal编程技术网

在多线程中编辑字符串 我对C++是更新的,我知道C++中的多线程支持很差。 但是现在我必须在多线程中编辑字符串val,我使用线程互斥来保护val。问题是程序在运行时总是核心转储

在多线程中编辑字符串 我对C++是更新的,我知道C++中的多线程支持很差。 但是现在我必须在多线程中编辑字符串val,我使用线程互斥来保护val。问题是程序在运行时总是核心转储,c++,string,C++,String,虽然经过重新设计,我找到了一个更好的解决方案,但我不明白为什么它是核心。有人能告诉我发生了什么吗? 代码如下所示 using namespace std; const static int kThreadSize = 48; map<string, string> gMap; string gStr = ""; void * func(void * data) { while(true) { printf("%s\n", gStr.c_str

虽然经过重新设计,我找到了一个更好的解决方案,但我不明白为什么它是核心。有人能告诉我发生了什么吗? 代码如下所示

using namespace std;
const static int kThreadSize = 48; 
map<string, string> gMap;
string gStr = ""; 
void * func(void * data)
{
    while(true)
    {   
        printf("%s\n", gStr.c_str());
        pthread_mutex_t m;
        pthread_mutex_init(&m, NULL);
        pthread_mutex_lock(&m);

        gStr = gStr + "a";//core in this line
        printf("%x\n", &gStr);//print the address

        pthread_mutex_unlock(&m);
        pthread_mutex_destroy(&m);

        printf("%s\n", gStr.c_str());
    }   
}

int main()
{
    pthread_t threads[kThreadSize]; 
    for(int i = 0; i < kThreadSize; i ++) 
    {   
        pthread_create(&threads[i], NULL, &func, &gMap);
    }   
    for(int i = 0; i < kThreadSize; i ++) 
    {   
        pthread_join(threads[i], NULL);
    }   
    return 0;
}
使用名称空间std;
const static int kThreadSize=48;
地图gMap;
字符串gStr=“”;
void*func(void*data)
{
while(true)
{   
printf(“%s\n”,gStr.c_str());
pthread_mutex_t m;
pthread_mutex_init(&m,NULL);
pthread_mutex_lock(&m);
gStr=gStr+“a”;//此行中的核心
printf(“%x\n”,&gStr);//打印地址
pthread_mutex_unlock(&m);
pthread_mutex_destroy(&m);
printf(“%s\n”,gStr.c_str());
}   
}
int main()
{
pthread_t threads[kThreadSize];
对于(int i=0;i

编辑: 使用全局互斥将解决Mike指出的问题。这里我不粘贴新的源代码


我的新问题是,我也不明白为什么在多线程中编辑字符串时它会成为核心。因为COW或引用计数?

正如我所看到的,您的互斥锁有一个问题,因为它是在方法
func()
中创建的,并且它使
func()
的每个线程调用创建一个新的互斥锁,调用此方法的每个威胁都会发生这种情况

正如MikeB所说,我建议您回顾线程理论,但同时尝试使用唯一的互斥来同步对
gMap
的访问,如示例所示:

using namespace std;

const static int kThreadSize = 48; 
map<string, string> gMap;
string gStr = ""; 

pthread_mutex_t m;

void * func(void * data)
{
    while(true)
    {   
        printf("%s\n", gStr.c_str());
        pthread_mutex_lock(&m);

        gStr = gStr + "a";//core in this line
        printf("%x\n", &gStr);//print the address

        pthread_mutex_unlock(&m);

        printf("%s\n", gStr.c_str());
    }   
}

int main()
{
    pthread_mutex_init(&m, NULL);

    pthread_t threads[kThreadSize]; 
    for(int i = 0; i < kThreadSize; i ++) 
    {   
        pthread_create(&threads[i], NULL, &func, &gMap);
    }   
    for(int i = 0; i < kThreadSize; i ++) 
    {   
        pthread_join(threads[i], NULL);
    }   
    return 0;
}
使用名称空间std;
const static int kThreadSize=48;
地图gMap;
字符串gStr=“”;
pthread_mutex_t m;
void*func(void*data)
{
while(true)
{   
printf(“%s\n”,gStr.c_str());
pthread_mutex_lock(&m);
gStr=gStr+“a”;//此行中的核心
printf(“%x\n”,&gStr);//打印地址
pthread_mutex_unlock(&m);
printf(“%s\n”,gStr.c_str());
}   
}
int main()
{
pthread_mutex_init(&m,NULL);
pthread_t threads[kThreadSize];
对于(int i=0;i<>代码>如果你只是学习C++,那么也许你最好远离手工多线程。我看到您的程序的主要问题是,所有三个广告都需要使用相同的互斥锁,而不是一个互斥锁。但即使您纠正了这一点,您的程序运行速度也很可能不会比单线程版本.OMG快。这是我的错。当我使用全局互斥来编辑程序时,它似乎运行正常。谢谢。是的,我已经像你一样重写了我的代码,它不会再核心化了。