473,770 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about STL containers in multithreaded environment

Hi there,

I have some trouble using STL containers.
I'm working on a multithreaded (pthread) application making heavy usage
of STL containers. All accesses to containers are made in locked
sections (using mutexes). But sometimes it happens that the application
crashes on container operations. The 2 last crashes are :
* calling push_back on a stl::list object (the backtrace show the last
call was in stl::list::inse rt);
* accessing an iterator in a list.

I'm absolutely sure that accesses (read and write) are made in locked
sections. That is to say, I'm sure that only one thread is working on a
container at each moment. I can not figure out what's is happening. Is
it possible that allocations failed ? How can iterators be messed up ?
Can anyone give me a hint on what's happening ? Maybe have I
misunderstood something about usage of containers in multithreaded
environment ...

Any help is welcome

Many thanks

Nicolas -

Mar 6 '06 #1
9 4548
> Hi there,

I have some trouble using STL containers.
I'm working on a multithreaded (pthread) application making heavy usage
of STL containers. All accesses to containers are made in locked
sections (using mutexes). But sometimes it happens that the application
crashes on container operations. The 2 last crashes are :
* calling push_back on a stl::list object (the backtrace show the last
call was in stl::list::inse rt);
* accessing an iterator in a list.

I'm absolutely sure that accesses (read and write) are made in locked
sections. That is to say, I'm sure that only one thread is working on a
container at each moment. I can not figure out what's is happening. Is
it possible that allocations failed ? How can iterators be messed up ?
Can anyone give me a hint on what's happening ? Maybe have I
misunderstood something about usage of containers in multithreaded
environment ...

Any help is welcome

Many thanks

Nicolas -

Maybe some operations with list in one thread invalidate another
thread's iterators, so you get error while trying to access (insert
element) through these iterators.

Mar 6 '06 #2
TB
ni************* ****@gmail.com skrev:
Hi there,

I have some trouble using STL containers.
I'm working on a multithreaded (pthread) application making heavy usage
of STL containers. All accesses to containers are made in locked
sections (using mutexes). But sometimes it happens that the application
crashes on container operations. The 2 last crashes are :
* calling push_back on a stl::list object (the backtrace show the last
call was in stl::list::inse rt);
* accessing an iterator in a list.

I'm absolutely sure that accesses (read and write) are made in locked
sections. That is to say, I'm sure that only one thread is working on a
container at each moment. I can not figure out what's is happening. Is
it possible that allocations failed ? How can iterators be messed up ?
Can anyone give me a hint on what's happening ? Maybe have I
misunderstood something about usage of containers in multithreaded
environment ...

Any help is welcome

Many thanks

Nicolas -


Perhaps you should try to aquire an implementation of the standard
library that is multithread safe.

--
TB @ SWEDEN
Mar 6 '06 #3
The idea is good but :
Each container is owned by a instance of a class Class. All accesses to
the container are made by calling APIs of Class. No iterator are stored
outside of Class. The containers are not shared between threads,
neither through direct pointer, nor by by iterator.

Mar 6 '06 #4
It seems that your code invalidates iterator. Inavalidation can be even
not related with multithreading. E.g. function foo stores iterator,
calls bar, bar deletes element on which iterator is pointing, bar
returns, foo tries to use stored iterator - crash.

Mar 6 '06 #5
All operations made on iterator are local, and done in "leaf" function
call. There is no risk of iterator beeing invalidated.
What is strange is the crash when calling list::push_back (). I'm sure
that no other thread was using the container ! To be sure, I look at
the backtrace, and all threads were sleeping.
I'm really lost ...

Mar 6 '06 #6
<ni************ *****@gmail.com > wrote in message
news:11******** *************@i 39g2000cwa.goog legroups.com...
All operations made on iterator are local, and done in "leaf" function
call. There is no risk of iterator beeing invalidated.
What is strange is the crash when calling list::push_back (). I'm sure
that no other thread was using the container ! To be sure, I look at
the backtrace, and all threads were sleeping.
I'm really lost ...


containers and multi-threading can be a pain. I know I had all kinds of
trouble when I was working on making a thread safe queue class. Perhaps the
compiler is optimizing the code and not rechecking the size of the container
when it should for a push_back.

If you ask in comp.programmin g.threads I'm sure they'll have lots of ideas
as to what can be going wrong.
Mar 6 '06 #7
ni************* ****@gmail.com wrote:
Hi there,

I have some trouble using STL containers.
I'm working on a multithreaded (pthread) application making heavy usage
of STL containers. All accesses to containers are made in locked
sections (using mutexes). But sometimes it happens that the application
crashes on container operations. The 2 last crashes are :
* calling push_back on a stl::list object (the backtrace show the last
call was in stl::list::inse rt);
* accessing an iterator in a list.

I'm absolutely sure that accesses (read and write) are made in locked
sections. That is to say, I'm sure that only one thread is working on a
container at each moment. I can not figure out what's is happening. Is
it possible that allocations failed ?
Yes.
How can iterators be messed up ?
Can anyone give me a hint on what's happening ? Maybe have I
misunderstood something about usage of containers in multithreaded
environment ...


How shall somebody help you without seeing any code...

Mathias

Mar 6 '06 #8
I will post my question on comp.programmin g.threads news group.
Many thanks.

Mar 6 '06 #9
ni************* ****@gmail.com wrote:
I will post my question on comp.programmin g.threads news group.


Well, maybe. But the problem might not have anything to do with threads.
Maybe you just screwed up in the use of the container, and the same
problem would occur in a single-threaded version of the code.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5319
by: Anthony | last post by:
Hi, How does (Can?) the stream mechanism work in multithreaded programs? I want to implement cout in my multithreaded program.
4
1709
by: Jürgen Devlieghere | last post by:
We encounter a crash every now and then at a client, and now I'm starting to doubt the fundamentals of life :-) We have a list of structs. struct SContactProperty { public: SContactProperty(void) : m_strName(_T("")), m_strValue(_T("")), m_bHidden(false) {};
11
4270
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
18
1842
by: Serve Laurijssen | last post by:
What's the difference between char buf; strcpy(buf, tmpnam(NULL)); and char buf; tmpnam(buf); According to my docs both calls write to an internal static buffer. What's
28
2089
by: jakk | last post by:
Hello All, I have a question about how to handle exceptions. Iam working on an ASP.NET application which has a presentation layer, Business Layer and DataAccess Layer. Iam confused about where to handle exceptions. Say forexample I have a requirement where certain value has to be between 30-72 only and Iam doing this validation in the the BL. if the value is not between 30-72 then Iam throwing an exception back to the UI layer. Is this...
6
3479
by: Stephen Carson | last post by:
I'm trying to build a Web Service that will kick off threads as logging requests come in. These threads will then log to the database. I have been able to make a simple Web Service. I have been able to get a simple multi-threaded example working. I have been used the SqlDataAdapter and DataSet classes extensively to access databases in a deployed Windows Service. But I'm getting confused how to put the pieces together. For example, I...
0
1204
by: shorti | last post by:
DB1 V8.2 DB2 Info Center has information on the subject that states in a multi function environment to include the EXEC SQL INCLUDE SQLCA statement in only one function and the remaining source files should use extern struct sqlca sqlca. I am running in a multithread environment and was looking for information on how to set this up. It would seem that using the extern struct would be for a single-thread environment as it would not be
20
5855
by: Dilip | last post by:
I understand the C++ standard does not talk about threading. My question here is directed more towards what happens when a STL container is used in a certain way. I'd appreciate any thoughts. I re-iterate I don't want to probe into what C++ standard says when I trample some data from multiple threads, I simply want to know if I have understood this right. I have a "std::map<somedatatype, someotherdatatype> myMap", where stuff gets...
2
1638
by: g | last post by:
Hello, Wouldn't be better shared_ptr constructor to have an argument <true/falsefor thread-safe reference counting?? The default should be true and in case you dont want thread-safe RC create a shared_ptr with false in constructor. The reason for this is to avoid the cost of the memmory barriers where you don't need them. At the moment if your application is multithreaded boost will choose to use thread-safe RC but in many cases(...
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.