i have the following program in vc++2008 and boost 1.45.0. i'm new to threading so be nice 
in my call to selltickets() from main i pass the variable numTickets by reference. when the threads run, even though i'm using a mutex, the value of numTickets is still not decrementing properly. what am i doing wrong?
#include<boost/thread.hpp>
#include<boost/bind.hpp>
#include<iostream>
using namespace std;
using namespace boost;
mutex mymutex;
void sellTickets(int agentID, int &numTickets)
{
while (true)
{
mymutex.lock();
if (numTickets <= 0)
{
mymutex.unlock();
break;
}
numTickets--;
printf("Agent:%d sold ticket. Tickets left:%d\n",agentID, numTickets);
mymutex.unlock();
}
printf("Agent:%d finished\n",agentID);
}
//***********************************************
int main(void)
{
int numAgents = 3;
int numTickets = 15;
thread_group thrd;
printf("Agents, START selling (%d) tickets\n",numTickets);
for (int agent = 1; agent <= numAgents; agent++)
{
thrd.create_thread(bind(&sellTickets, agent, numTickets));
}
thrd.join_all();
return 0;
}

in my call to selltickets() from main i pass the variable numTickets by reference. when the threads run, even though i'm using a mutex, the value of numTickets is still not decrementing properly. what am i doing wrong?
#include<boost/thread.hpp>
#include<boost/bind.hpp>
#include<iostream>
using namespace std;
using namespace boost;
mutex mymutex;
void sellTickets(int agentID, int &numTickets)
{
while (true)
{
mymutex.lock();
if (numTickets <= 0)
{
mymutex.unlock();
break;
}
numTickets--;
printf("Agent:%d sold ticket. Tickets left:%d\n",agentID, numTickets);
mymutex.unlock();
}
printf("Agent:%d finished\n",agentID);
}
//***********************************************
int main(void)
{
int numAgents = 3;
int numTickets = 15;
thread_group thrd;
printf("Agents, START selling (%d) tickets\n",numTickets);
for (int agent = 1; agent <= numAgents; agent++)
{
thrd.create_thread(bind(&sellTickets, agent, numTickets));
}
thrd.join_all();
return 0;
}