With this code:
#include <iostream>
#include <vector>
#include <stdio.h>
struct x
{
    int a;
    const char* t;
};
int main()
{
    std::vector<x> instances;
    
    while(true)
    {
        printf("wait for key1\n");
        getchar();
        getchar();
        
        {
            for(int i = 0; i < 100000; i++)
            {
                x n;
                n.a = i;
                n.t = "x instance";
                instances.push_back(n);
            }
            //x instance deleted right?
        }
        
        {
            x x1, x2, x3;
            x1 = instances[0];
            x2 = instances[1];
            x3 = instances[2];
            
            std::cout << x1.t << std::endl;
            std::cout << x2.t << std::endl;
            std::cout << x3.t << std::endl;
            
            instances.clear();
        }
        
        printf("wait for key2\n");
        getchar();
        getchar();
    }
    
    return 0;
}
I'm getting this output:
wait for key2
wait for key1
x instance
x instance
x instance
That's cute but I think I should get a output like this:
wait for key2
wait for key1
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½
>>£#$@@#£#$½£#$½
Because x struct instances must be deleted. Am I wrong? And the true implemention should be like this:
#include <iostream>
#include <vector>
#include <stdio.h>
struct x
{
    int a;
    const char* t;
};
int main()
{
    std::vector<x*> instances;
    
    while(true)
    {
        printf("wait for key1\n");
        getchar();
        getchar();
        
        {
            for(int i = 0; i < 100000; i++)
            {
                x* n = new x();
                n->a = i;
                n->t = "x instance";
                instances.push_back(n);
            }
        }
        
        {
            x* x1 = 0;
            x* x2 = 0;
            x* x3 = 0;
            x1 = instances[0];
            x2 = instances[1];
            x3 = instances[2];
            
            std::cout << x1->t << std::endl;
            std::cout << x2->t << std::endl;
            std::cout << x3->t << std::endl;
            
            instances.clear(); /* delete x instances one-by-one like 'delete instances[i];' */
         }
        
        printf("wait for key2\n");
        getchar();
        getchar();
    }
    
    return 0;
}
I'm not clear about memory management. Why I can still get (non-new'd) 'x instances' after cleanup? Examples?
I've looked in link>> and I think x instances in the for loop must be deleted?
Update
Here is my example implementation for other people (beginners like me). I'll use a sync'd queue for socket io packets and I don't care about thread.join() just because my threads are only workers, not managers! (what a real-life simulation!)
#include <iostream>
#include <thread>
#include <chrono>
bool b1 = true;
bool b2 = true;
//Of course you can create only 1 boolean for all threads (isAlive should be a good name for it)
//but this way provides more detailed thread aliveness control.
void process(bool* ref, int id)
{
    bool my = *ref;
    while (my)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        std::cout << "thread#" << id << std::endl;
        my = *ref;
    }
    std::cout << "thread#" << id << " end." << std::endl;
}
int main()
{
    std::thread(process, &b1, 0).detach();
    std::thread(process, &b2, 1).detach();
    std::cin.get();
    b1 = false;
    
    std::cin.get();
    b2 = false;
    
    //MS-DOS :(
    std::cin.get();
    return 0;
}
Posibble output:
thread#thread#10
thread#0
thread#1
thread#1
thread#0
//Hit the enter!
thread#1
thread#0
thread#0 end.
thread#1 thread#1 //Hit the enter! thread#1 thread#1 end. //Hit the enter!
 
     
     
     
     
     
    