473,803 Members | 4,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL & Multithreading

Don't shoot me down guys, I know the Standard says nothing of threads and
all....

As i understand it the STL does not support multithreading, however I want
to use the STL in a multithreaded environment.

Is it sufficient to declare a bool variable 'Locked' which process set when
they are using a particular object or do I need to consult some more low
level platform-kernel documentation??
Regards
Mike
Jul 22 '05 #1
8 8066
Michael wrote:
Don't shoot me down guys, I know the Standard says nothing of threads and
all....

As i understand it the STL does not support multithreading, however I want
to use the STL in a multithreaded environment.

Is it sufficient to declare a bool variable 'Locked' which process set when
they are using a particular object or do I need to consult some more low
level platform-kernel documentation??


I think you need to ask this in a newsgroup that deals with
multithreading. Sufficiency of declaring a variable and setting
of that variable cannot be judged from C++ point of view. You
said yourself, C++ says nothing about threads.

A newsgroup for your OS is probably the best for that.

V
Jul 22 '05 #2
>
As i understand it the STL does not support multithreading, however I want
to use the STL in a multithreaded environment.
That depends on the implementation - some actually do support threading.
Or more precisely said methods of these implementations are thread-safe.
But that alone won't make your software multithread safe, as all data
constructed with STL classes must be shared in a consistent state across
threads.

You should first get a general idea how multithreading works and what is
required, before mixing STL and threads. The STL is quite difficult to
handle in such an environment esspecially regarding iterators...
Is it sufficient to declare a bool variable 'Locked' which process set when
they are using a particular object or do I need to consult some more low
level platform-kernel documentation??


A bool variable is not enough - look for mutexes and semaphores. On a
POSIX compliant system this would be pthread_mutex_l ock and friends...
Jul 22 '05 #3
"Michael" <sl***********@ hotmail.com> wrote in message news:<cl******* ***@titan.btint ernet.com>...
Don't shoot me down guys, I know the Standard says nothing of threads and
all....

As i understand it the STL does not support multithreading, however I want
to use the STL in a multithreaded environment.

Is it sufficient to declare a bool variable 'Locked' which process set when
they are using a particular object or do I need to consult some more low
level platform-kernel documentation??
Regards
Mike

Bool is not enough ( ++ operator is not atomic etc...), there is no
way you can garantie something to be locked using only language
facilities - you should use mechanisms provided by the os.
As an example, your bool variable before being changed/read may sit in
a cpu register or cash memory. For multiprocessor machines you never
know what happens with this bools as they may simultaneously sit in
another processor's register/memory (processor that executes a
concurrent thread)
Jul 22 '05 #4
GTO
Which operating system? Which STL implementation? You may want to read more
about SGI's STL implementation at
http://www.sgi.com/tech/stl/thread_safety.html

Generally, it can be done by using mutex locks inside a wrapper class
(Pthread mutexes or Win32 critical sections).

Gregor
Jul 22 '05 #5

"Michael" <sl***********@ hotmail.com> skrev i en meddelelse
news:cl******** **@titan.btinte rnet.com...
Don't shoot me down guys, I know the Standard says nothing of threads and
all....

As i understand it the STL does not support multithreading, however I want
to use the STL in a multithreaded environment.

Is it sufficient to declare a bool variable 'Locked' which process set
when
they are using a particular object or do I need to consult some more low
level platform-kernel documentation??
Regards
Mike

I am not shooting you down, but as this is outside the standard you really
ought to check with the compilers documentation. My guess is that for all
compilers that support multithreading, you can use the stl containers as you
would use any other variable in a multithreaded environment. This is the
case for e.g. Microsofts compilers.

/Peter
Jul 22 '05 #6

"GTO" <gr******@NOSPA Myahoo.com> wrote in message
news:s%******** *********@newss vr14.news.prodi gy.com...
Which operating system? Which STL implementation? You may want to read
more about SGI's STL implementation at
http://www.sgi.com/tech/stl/thread_safety.html

Generally, it can be done by using mutex locks inside a wrapper class
(Pthread mutexes or Win32 critical sections).

Gregor


Or you can use boost thread library to have platform & compiler
independency.

Catalin
Jul 22 '05 #7
Michael wrote:
Don't shoot me down guys, I know the Standard says nothing of threads and
all....

As i understand it the STL does not support multithreading, however I want
to use the STL in a multithreaded environment.

Is it sufficient to declare a bool variable 'Locked' which process set when
they are using a particular object or do I need to consult some more low
level platform-kernel documentation??
Regards


Some implementations of the STL are not thread safe meaning that using
distinct objects in the different threads may cause undefined befaviour.
This was at least true for GCC's 2.9* string implementation (which has
been fixed properly in gcc 3.*). Make sure the STL implementation you
do use is at least re-entrant. (Recent GCC STL, STLPORT and VC++7.1 seem
to be OK).

Others posts have good suggestions.
Jul 22 '05 #8
__PPS__ wrote:
Bool is not enough ( ++ operator is not atomic etc...),

What do you mean with the above.
there is no
way you can garantie something to be locked using only language
facilities - you should use mechanisms provided by the os.
As an example, your bool variable before being changed/read may sit in
a cpu register or cash memory. For multiprocessor machines you never
know what happens with this bools as they may simultaneously sit in
another processor's register/memory (processor that executes a
concurrent thread)

Now you have placed me in thoughts. Under .NET, in a producer/consumer
relationship a common practice is to have a boolean value indicating
whether a new value has been produced or not.

For example, assuming

bool consumed=true;
A producer thread checks if this boolean is true and if yes, it assigns
a new value to the corresponding location and makes consumed=false; ,
otherwise it enters the WaitSleepJoin state via a call to Monitor::Wait() .
A consumer thread checks if this value is false, and if it is, it reads
the corresponding location and makes consumed=true; otherwise it enters
the WaitSleepJoin state.
This is the same variable (the data member of an object). How can it be
in the registers of two separate processors with different values each?

It doesn't sound possible.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9

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

Similar topics

47
3759
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
5
1428
by: Per Rollvang | last post by:
Hi All! I have been struggling with where what & when to set the mousepointer to an hourglass when using async. multithreading. Nothing seems to work... : ( Anybody that can help me out with that one? TIA Per Rollvang
6
2512
by: a | last post by:
Hello, I am doing some multithreading in an MDI app, and I can't seem to get the cursor to stay as an Hourglass. I call: Cursor.Current = cursors.wait at the beginning of my routing, and set it back to cursors.default when the thread ends (using a callback)
0
1620
by: Victor | last post by:
I have a XML webservice on IIS 5.1 with self-issued certificate. My ActiveX tryes to invoke a webservice method using class CSecureEvtSyncSocket from msdn sample http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/vcsamsecuresoapsample.asp Invoking webmethod over regular HTTP it works fine, but over HTTPS i receive error 10054 (Connection reset by peer) during response body read. IIS logs say that request is OK...
19
2827
by: jupiter | last post by:
Hi guys!!! Just one quick question... Which database module should I use when I want to use multi threading as my application requires lots of data from internet ???? I also want this database module to be fast, simple n efficient, in any case multi threading capabilities are # 1 requirement.
7
4493
darlene
by: darlene | last post by:
Hi, I need some help in creating an application in Visual C++ which should make use of MFC and multithreading. The application is supposed to consist in a number of threads representing factories. Each factory, implemented as a thread, can create only one type of the objects below: wheels, windows, doors, chassis or engines. From the GUI an user should be able to add a new factory or delete an existing one by specifying its type (eg. engine...
0
888
by: joop renes | last post by:
hi, i hope this is the right list for the following question of a c++ hacker,python newbie. i have a library in c++ to which i want to add a python GUI and other python stuff.The library has multithreading components, while python uses a reference counted memory model. Usually mixing reference counting with multithreading is frowned upon, at least in the c++ world. what am i to expect in the python world, if i bring multithreading c++...
0
918
by: Gabriel Genellina | last post by:
En Sun, 04 May 2008 11:56:14 -0300, joop renes <jj.renes@hccnet.nlescribió: Python objects are reference counted, *and* you can have many threads running. This is not a problem in itself; Python has a Global Interpreter Lock (GIL) that ensures that addref/releases are properly handled. Only one thread at a time can execute Python code; but your C++ library can use as many threads as you want - as long as they don't call Python code again...
0
9703
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
10548
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
10316
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
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
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
5500
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
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.