Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
this mutex class

C#
#include"mutex.h"
#include<time.h>
Mutex::Mutex()
{
 // pthread_mutexattr_init(&attr);
  //pthread_mutexattr_setpshared(&attr,PTHREAD_PROCESS_PRIVAITE);
 // pthread_mutex_init(&lock,NULL);

 lock = PTHREAD_MUTEX_INITIALIZER;
}

void Mutex::Lock()
{
/*struct timespec delta;
delta.tv_sec = 5;
delta.tv_nsec = 0;
pthread_mutex_timedlock_np(&lock,&delta);*/

   pthread_mutex_lock(&lock);

}

void Mutex::Unlock()
{
  pthread_mutex_unlock(&lock);
 pthread_mutex_destroy(&lock);

}
Mutex::~Mutex()
{

}



and i define mutex it in LTSC manager class that use dbConnection to connect
C#
class LTSC_Manager
{
public:
 LTSC_Manager();
 virtual ~LTSC_Manager();
 
 IDriver * CreateDriver(char * technology);
 int RequestNewIdentifier();
 int DBConnect();  
 CDatabaseModule  dbConnection;
 
 //pthread_mutex_t lock ;
 
 Mutex c_section; // here 

};

and i call like this in threading function
C#
   m_Manager->c_section.Lock();
     int res=m_Manager->m_DB.BookFreeTestHead("AXE",&h);
     m_Manager->c_section.Unlock();
printf(" res is %i ",res);

and the cursor stop in res is ..
but if i press ctrl-c the data return
Posted
Comments
Sergey Alexandrovich Kryukov 28-Sep-14 15:25pm    
What do you mean by that: "lock from threading system"? Unclear what is the problem.
—SA
Mahmoud_Gamal 29-Sep-14 5:31am    
I MEAN THAT multi-thread application every new thread have access to database
and this code

m_Manager->c_section.Lock();
int res=m_Manager->m_DB.BookFreeTestHead("AXE",&h);
m_Manager->c_section.Unlock();
printf(" res is %i ",res);

in thread and another threads also BookFreeTestHead but i did't want two thread have the same free test head
Sergey Alexandrovich Kryukov 29-Sep-14 9:56am    
Okay, the lock prevents two or more thread executing some code fragment at the same time. I don't understand what do you mean by "have the same free test head".
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900