std::basic_string<> and threads

Ryszard Kabatek rumcajs@gmx.net
Sat Apr 1 00:00:00 GMT 2000


A possibility to have a threadsave basic_string<> 
is to disable the reference counting for multitheard programs.

We can do it in the current implementation of basic_string<>
in the following way:

Currently the value of _Rep::_M_state (it determines the reference
counting) is initially set to 0.
If we would set it to -1, the string couldn't have reference copies.

So we can define an enumeration in the body of basic_string<>:

enum {INITIAL_VAL=0}; // or -1

and use it

_M_rep()->_M_state = static_cast<int>(INITIAL_VAL);

instead of the current ... = 0;

Now we can add preprocessor guards, like

#ifdef  STRING_REFCOUNT_ON
enum {INITIALVAL = 0};
else
enum {INITIALVAL = -1};
# endif

In this case we can compile the basic_string<> as reference counted or
not.
I think it is much better than the usage of reference counting & mutexes 
in multithread programs.

-- 
Ryszard Kabatek

Sent through Global Message Exchange - http://www.gmx.net



More information about the Libstdc++ mailing list