使用 pthreads 时奇怪的 std::string 行为
Bizzare std::string behavior when using pthreads
我遇到了一个奇怪的 std::string 问题。它与通过线程在结构内传递字符串有关。本质上,我通过连接一个字符串向量来构建一个字符串,并将这个构建的字符串用作结构中的字符串。然后我将该结构作为参数传递给线程,访问字符串成员变量,并尝试打印它。发生令人难以置信的行为:
如果我在创建线程之前先打印字符串,那么在线程运行时打印字符串。但是,如果我在线程创建之前不打印字符串,那么在线程运行时字符串不会打印并且大小为 0。谁能帮我解决这个问题?
#include <iostream>
#include <pthread.h>
#include <string>
#include <vector>
struct data
{
std::string msg;
int id;
};
std::string GetString(std::vector<std::string> &args);
void *F(void *vargp);
int main()
{
using std::cout;
using std::endl;
std::vector<std::string> args;
args.push_back(std::string("skip"));
args.push_back(std::string("one"));
args.push_back(std::string("two"));
args.push_back(std::string("three"));
args.push_back(std::string("four"));
args.push_back(std::string("five"));
data d;
d.id = 123;
d.msg = GetString(args);
/* Removing the below commented line escapes the error.
* With the line commented the string has size 0 in F.
* However when it is compiled and run, the string in F
* has its actual size and the correct output appear.
* WHAT IS GOING ON?
*/
// cout << "before: " << d.msg << endl;
pthread_t tid;
pthread_create(&tid, NULL, F, &d);
pthread_exit(0);
}
void *F(void *vargp)
{
using std::cout;
using std::endl;
pthread_detach(pthread_self());
data d = *(data *)vargp;
cout << d.msg << " " << d.id << endl;
}
std::string GetString(std::vector<std::string> &args)
{
std::string str(args[1]);
for (int i=2; i!=args.size(); i++)
str += " " + args[i];
return str;
}
我通过
编译了函数
g++ test.cpp -pthread -o test
其中 test.cpp 是上述源文件的名称。
这是因为 未定义的行为 ,因为变量 d
超出范围,当您在 main
中 pthread_exit
时被破坏功能。一旦原始变量超出范围,线程函数中的指针将不再有效。
对于中间解决方案,动态分配 d
。对于长期解决方案,请考虑改用 std::thread
。
我遇到了一个奇怪的 std::string 问题。它与通过线程在结构内传递字符串有关。本质上,我通过连接一个字符串向量来构建一个字符串,并将这个构建的字符串用作结构中的字符串。然后我将该结构作为参数传递给线程,访问字符串成员变量,并尝试打印它。发生令人难以置信的行为:
如果我在创建线程之前先打印字符串,那么在线程运行时打印字符串。但是,如果我在线程创建之前不打印字符串,那么在线程运行时字符串不会打印并且大小为 0。谁能帮我解决这个问题?
#include <iostream>
#include <pthread.h>
#include <string>
#include <vector>
struct data
{
std::string msg;
int id;
};
std::string GetString(std::vector<std::string> &args);
void *F(void *vargp);
int main()
{
using std::cout;
using std::endl;
std::vector<std::string> args;
args.push_back(std::string("skip"));
args.push_back(std::string("one"));
args.push_back(std::string("two"));
args.push_back(std::string("three"));
args.push_back(std::string("four"));
args.push_back(std::string("five"));
data d;
d.id = 123;
d.msg = GetString(args);
/* Removing the below commented line escapes the error.
* With the line commented the string has size 0 in F.
* However when it is compiled and run, the string in F
* has its actual size and the correct output appear.
* WHAT IS GOING ON?
*/
// cout << "before: " << d.msg << endl;
pthread_t tid;
pthread_create(&tid, NULL, F, &d);
pthread_exit(0);
}
void *F(void *vargp)
{
using std::cout;
using std::endl;
pthread_detach(pthread_self());
data d = *(data *)vargp;
cout << d.msg << " " << d.id << endl;
}
std::string GetString(std::vector<std::string> &args)
{
std::string str(args[1]);
for (int i=2; i!=args.size(); i++)
str += " " + args[i];
return str;
}
我通过
编译了函数g++ test.cpp -pthread -o test
其中 test.cpp 是上述源文件的名称。
这是因为 未定义的行为 ,因为变量 d
超出范围,当您在 main
中 pthread_exit
时被破坏功能。一旦原始变量超出范围,线程函数中的指针将不再有效。
对于中间解决方案,动态分配 d
。对于长期解决方案,请考虑改用 std::thread
。