Boost: thread brief analysis (3): local thread storage and other

Source: Internet
Author: User

There is also an important concept in multi-threaded programming: Thread Local store (TLS, Thread Local Storage). In boost, TLS is also called TSS, thread specific storage.
Boost::

The thread Library provides us with a simple TLS object-oriented encapsulation interface. The following is the interface definition of the TSS class:
Class
Tss
{


Public
:


Tss(

Boost::

Function1<

Void
,

Void
*> *

Pcleanup);


Void
*

Get()

Const
;


Void
Set(

Void
*

Value);


Void
Cleanup(

Void
*

P);
};


These functions are used to obtain, set, and clear local storage variables of threads. These functions encapsulate API operations such as tlsalloc, tlsgetvalue, and tlssetvalue internally and encapsulate them into oo forms.
However, boost encapsulates this type of information in the detail namespace, which is not recommended. When we need to use TSS, we should use another more convenient class: thread_specific_ptr, this is a smart pointer class, and its interface is as follows:
Class
Thread_specific_ptr:

Private
Boost::

Noncopyable// Exposition only


{


Public
:


// Construct/copy/destruct


Thread_specific_ptr();


Thread_specific_ptr(

Void
(*

Cleanup)(

Void
*));
~

Thread_specific_ptr();

// Modifier Functions


T*

Release();


Void
Reset(

T* =

0
);

// Observer functions


T*

Get()

Const
;


T*

Operator
-> ()

Const
;


T&

Operator
*()()

Const
;
};


Get, reset, and release operations are supported.
Thread_specific_ptr
Class implementation is very simple, just to "modify" the TSS class into a smart pointer, the class will automatically create a TSS object in its constructor, the default parameter
The reset function causes internally encapsulated TSS objects to be destructed, so as to automatically manage memory allocation and release.

The following is an example of using thread_specific_ptr to implement TSS:
# Include <boost/thread. HPP>
# Include <boost/thread/mutex. HPP>
# Include <boost/thread/TSS. HPP>
# Include <iostream>


Boost::

Mutex io_mutex;


Boost::

Thread_specific_ptr<

Int
>

PTR;

// Use this method to tell that this Member will not shared by all threads



Struct
Count
{


Count(

Int
ID):

ID(

ID){}

Void
Operator
()()
{


If
(

PTR.

Get() =

0
)

// If PTR is not initialized, initialize it


PTR.

Reset(

New
Int
(

0
));

// Attention, we pass a pointer to reset (actually set PTR)



For
(

Int
I=

0
;

I<

10
; ++

I)
{
(*

PTR) ++;


Boost::

Mutex::

Scoped_lock lock(

Io_mutex);


STD::

Cout<

ID<

":"
<*

PTR<

STD::

Endl;
}
}

Int
ID;
};

Int
Main
(

Int
Argc,

Char
*

Argv[])
{


Boost::

Thread thrd1(

Count(

1
));


Boost::

Thread thrd2(

Count(

2
));


Thrd1.

Join();


Thrd2.

Join();

Return
0
;
}

In addition, the thread Library also provides an interesting function called call_once in TSS.::

This function is used in the implementation of init.
The declaration of this function is as follows:
Void
Call_once(

Void
(*

Func)(),

Once_flag&

Flag);


The Windows implementation of this function creates a mutex so that all threads are in the waiting state when trying to execute this function until one thread has executed the func function, the second parameter of the function indicates whether the function func has been executed. This parameter is often initialized to boost_once_init (that is, 0 ).
), If you initialize this parameter to 1
, Then the function func will not be called. At this time, call_once is equivalent to nothing, which may be required in some cases. For example, you can decide whether to need a function called call_once based on the Program processing result.
After the function func is executed, call_once changes the flag to 1.
This will cause future call_once threads (including threads waiting at mutex and threads just entering call_once) to skip the code for executing func.

Note that this function is not a template function, but a common function. Its first parameter 1
Is a function pointer and its type is void.
(*)()

Instead of using the function template like the boost library in many other places, it does not matter. With boost::

BIND is a super weapon. You can bind parameters as needed. According to the boost documentation, the input function cannot throw an exception, but it does not seem like this in the implementation code.

The following is a typical example of using call_once to implement one-time initialization:
# Include <boost/thread. HPP>
# Include <boost/thread/once. HPP>
# Include <iostream>


Int
I=

0
;


Int
J=

0
;


Boost::

Once_flag flag=

Boost_once_init;

Void
Init()
{
++

I;
}

Void
Thread()
{


Boost::

Call_once(&

Init,

Flag);
++

J;
}

Int
Main
(

Int
Argc,

Char
*

Argv[])
{


Boost::

Thread thrd1(&

Thread);


Boost::

Thread thrd2(&

Thread);


Thrd1.

Join();


Thrd2.

Join();

STD::

Cout<

I<

STD::

Endl;


STD::

Cout<

J<

STD::

Endl;

Return
0
;
}


The result shows that the global variable I is executed only once.++

And variable J is executed in both threads.++

Operation.

Others
Boost::

Thread
Currently, it is not perfect. The main problems include: no support for thread priority or cancellation of threads, the current implementation mechanism does not seem easy to meet this requirement through simple modifications.
A major adjustment will be made to the implementation of a specific version, but the currently supported interfaces should be relatively stable, and the currently supported features will continue to work.

 

Post from: http://blog.csdn.net/billdavid/archive/2005/05/27/381918.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.