I'm creating a thread managing class, to which i can post tasks (functions + parameters), and the class will take care of threading them. 

the problem is not so much related to this whole class (i think), but more a general problem with locking shared resources when use multiple threads.
here is the code:

main test file:

```
#include <iostream>
#include <thread>
#include <vector>
#include "Ttask_1.h"
#include <mutex>

using namespace std;

bool quit=false;

void* square(void* n);
void* print(void* n);
mutex cout_mutex;
mutex res_mutex;

bool cout_locked=false;

vector<Ttask_1<void*,void*>*> tasks1;
vector<Ttask_1<void*,void*>*> tasks2;

void check_for_work(vector<void*> &vec,vector<Ttask_1<void*,void*>*> &tasks)
{
    while(!quit)
    {
        if(tasks.size()!=0)
        {
            (*tasks[0]).run(vec);
            tasks.erase(tasks.begin());
        }
        else
            quit=true;
    }
    while(cout_locked){}
    cout_locked=true;
    cout<<"thread done"<<endl;
    cout_locked=false;

}

int main(int argc, const char * argv[])
{
    vector<void*> vec;
    int n=5;
    //int *n_p=&n;
    //Ttask_1<void*,void*> task(&n,square);
    tasks1.push_back(new Ttask_1<void*,void*>(&n,square,true));
    tasks1.push_back(new Ttask_1<void*,void*>(&n,print,false));

    tasks2.push_back(new Ttask_1<void*,void*>(&n,square,true));
    tasks2.push_back(new Ttask_1<void*,void*>(&n,print,false));

    thread Thread1(check_for_work,ref(vec),ref(tasks2));

    thread Thread2(check_for_work,ref(vec),ref(tasks1));
    //(&Ttask_1<int,int>::run,&task,ref(vec));
   // task.run(vec,Thread);
    Thread1.join();
    Thread2.join();

    int a;
    cin>>a;
    return 0;
}

void* print(void* n)
{
    for(int i=0;i<*(int*)(n);i++)
    {
        while(cout_locked){}
        cout_locked=true;
        cout<<i<<endl;
        cout_locked=false;
    }
    void* a;
    return a;
}

void* square(void* n)
{

    int res=(*(int*)n)*(*(int*)n);

    while(cout_locked){}
    cout_locked=true;
    cout<<res<<endl;
    cout_locked=false;

    int *res_p=new int;
    res_p=&res;
    return res_p;
}

```

the ttask\_1 class:

```
#ifndef task_Test_Ttask_1_h
#define task_Test_Ttask_1_h
#include <vector>
#include <mutex>
using namespace std;

extern mutex res_mutex;

template <class type1, class ret>
class Ttask_1
{
public:
    Ttask_1(type1 arg_in,ret(*func_p_in)(type1),bool result)
    {
        safe_result=result;
        arg1=arg_in;
        func_p=func_p_in;
    }
    void run(vector<void*> &res_vector)
    {

        ret res=(*func_p)(arg1);

        if(safe_result)
        {
            void *res_p=&res;
            res_mutex.lock();
            res_vector.push_back(res_p);
            res_mutex.unlock();
        }
        done=true;

    }
    bool is_done(){return done;}
private:
    bool safe_result;
    bool done=false;
    type1 arg1;
    ret(*func_p)(type1);
};

#endif

```

as you can see, i implemented my own 'locking' thing on the cout, after i saw the mutex wasn't working. the behaviour is exactly the same so this is not the issue.

and that behaviour is as follows: 

I expect the numbers 0,1,2,3,4,25 and the string 'thread done' to all be printed twice before the program terminates. 

However, very often (not always, but often) i get this output:

> 
> 25 0 1 2 3 4 thread done thread done
> 
> 
> 

so a couple of numbers are missing, and i have no idea what causes this. as i said, eve if i replace my own 

```
    while(cout_locked){}
    cout_locked=true;

```

by

```
    cout_mutex.locked();

```

and

```
    cout_locked=false;

```

by

```
    cout_mutex.unlock()

```

nothing changes.

Any help would be appreciated, thanks