在非多线程访问std::queue的应用中不会有异常,但是如果在多线程访问中可能会出现一个异常终止,报deque iterator not dereferencable,即使只有一个线程执行pop出列操作,另一个线程只push入列也会出现这个异常,这个是大家经常碰到的。
另一个问题,在while循环中,如果入列的是整数型,则出列显示的都是入列的数据,如果是char *型指针,出列的数据都是最后一个入列的字符串,
如下面代码,入列的是整数类型的数据
int iCurrentIndex = 0;
while (iCurrentIndex < 30)
{
intQueue.push(iCurrentIndex);
iCurrentIndex++;
}
cout << endl;
int t;
while (!intQueue.empty())
{
t = intQueue.front();
cout << "deque " << t << endl;
intQueue.pop();
}
输出
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
deque 0
deque 1
deque 2
deque 3
deque 4
deque 5
deque 6
deque 7
deque 8
deque 9
deque 10
deque 11
deque 12
deque 13
deque 14
deque 15
deque 16
deque 17
deque 18
deque 19
deque 20
deque 21
deque 22
deque 23
deque 24
deque 25
deque 26
deque 27
deque 28
deque 29
如下代码
int iCurrentIndex = 0;
char *cstr = new char[100];
while (iCurrentIndex < 30)
{
sprintf(cstr, "str%d.mp4", iCurrentIndex);
//char * ppath = (char*)path.c_str();
cout << cstr << endl;
cstrQueue.push(cstr);
iCurrentIndex++;
}
cout << endl;
char *p;
while (!cstrQueue.empty())
{
p = cstrQueue.front();
cout << "deque " << p << endl;
cstrQueue.pop();
}
输出
str0.mp4
str1.mp4
str2.mp4
str3.mp4
str4.mp4
str5.mp4
str6.mp4
str7.mp4
str8.mp4
str9.mp4
str10.mp4
str11.mp4
str12.mp4
str13.mp4
str14.mp4
str15.mp4
str16.mp4
str17.mp4
str18.mp4
str19.mp4
str20.mp4
str21.mp4
str22.mp4
str23.mp4
str24.mp4
str25.mp4
str26.mp4
str27.mp4
str28.mp4
str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
deque str29.mp4
而如果使用string类型,即queue<string>而不是queue<char *>则入列和出列的内容一致。