473,378 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 8032
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_lock and friends...
Jul 22 '05 #3
"Michael" <sl***********@hotmail.com> wrote in message news:<cl**********@titan.btinternet.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.btinternet.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******@NOSPAMyahoo.com> wrote in message
news:s%*****************@newssvr14.news.prodigy.co m...
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
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
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...
6
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...
0
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...
19
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...
7
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....
0
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...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.