Question: i tried the following code i found on the net and is perfectly working.

```
template<typename T,typename T2>
std::vector<T>& operator<<(std::vector<T>& v1, T2 t1){
v1.push_back(T(t1));
return v1;
 } 

```

but for me the argument t1 must be from type T and not T2.
**so my first question is:** why when i do 

```
std::vector<T>& operator<<(std::vector<T>& v1, ***T t1***) //instead of T2 t1

```

i have the following error:
error: no match for 'operator<<' (operand types are 'std::vector >' and 'std::string {aka std::basic\_string}')

**my second question** is a ligne 4 of the code:
what does mean T(t1) ? 

thanks a lot !

**EDIT:**

thanks for all your answers!

this is the code i was using to test and try to inderstand the function.

```
using namespace std; 

template<typename T>
std::vector<T>& operator<<(std::vector<T>& v1, T t1){
v1.push_back(T(t1));
return v1;
 } 

int main()  {
vector<string> s;
string s2(" hello ");
s << s2 ;
cout<< s[0];
return 0;}

```

so the T is string and v1 is of type vector(String) and t1 is of type String
this is why i was wondering why removing typename T2 from template causes error even if the value\_type is string for both

Answer: 
Use an object and [lock it](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement)

```
class App
{
    public delegate void StopHandler();
    public event StopHandler OnStop;
    private object keepAliveLock = new object();
    private bool keepAlive = true;

....
    private void Update()
    {
        int counter = 0;

        while (true)
        {
            lock(keepAliveLock)
            {
                 if(!keepAlive) 
                      break;
            }
            counter++;

            Console.WriteLine(string.Format("[{0}] Update #{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), counter));

            Thread.Sleep(3000);
        }
    }
}

```

Please note, that every access to keepAlive needs to be locked (surround by lock statement). Take care of deadlock situations.
As far as the specific wording of your question and your example code (in regards to `keepAlive`) simply use `volatile` (for simple *Read Access* to `bool`). As to if there are other ways to better your code, this is another story. 

```
private volatile bool keepAlive = true;

```

For *Simple Types* and access such as `bool`, then `volatile` is sufficient - this ensures that threads don't cache the value.

Read and writes to `bool` are *Atomic* as found in [C# Language Spec](http://msdn.microsoft.com/en-us/library/aa691278(VS.71).aspx).

> 
> Reads and writes of the following data types are atomic: bool, char,
>  byte, sbyte, short, ushort, uint, int, float, and reference types.
> 
> 
> 

**Furthermore**

> 
> Partition I, Section 12.6.6 of the CLI spec states: "A conforming CLI
>  shall guarantee that read and write access to properly aligned memory
>  locations no larger than the native word size is atomic when all the
>  write accesses to a location are the same size.
> 
> 
> 

Its also worth looking up [volatile (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/volatile)

> 
> The volatile keyword indicates that a field might be modified by
>  multiple threads that are executing at the same time. Fields that are
>  declared volatile are not subject to compiler optimizations that
>  assume access by a single thread. This ensures that the most
>  up-to-date value is present in the field at all times.
> 
> 
>