Synchronization of threads in the Linux operating system more

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

1 mutual exclusion lock

  Mutexes used to ensure that a period of time only one thread in the execution of a code. Necessity: obviously all threads to the same file sequential write data, the final result is disastrous.

  Look at the following code. This is a read / write procedures, which use a public buffer, and assume that only a buffer to save a piece of information. Buffer only two states: the information or no information.

  void reader_function ( void );

  void writer_function ( void );

  char buffer;

  int buffer_has_item=0;

  pthread_mutex_t mutex;

  struct timespec delay;

  void main ( void ){

  pthread_t reader;

  /* The definition of delay time*/

  delay.tv_sec = 2;

  delay.tv_nec = 0;

  /* With the default property is initialized a mutex object*/

  pthread_mutex_init (&mutex,NULL);

  pthread_create(&reader, pthread_attr_default, (void *)&reader_function), NULL);

  writer_function( );

  }

  void writer_function (void){

  while(1){

  /* Lock mutex lock*/

  pthread_mutex_lock (&mutex);

  if (buffer_has_item==0){

  buffer=make_new_item( );

  buffer_has_item=1;

  }

  /* Open the mutex lock*/

  pthread_mutex_unlock(&mutex);

  pthread_delay_np(&delay);

  }

  }

  void reader_function(void){

  while(1){

  pthread_mutex_lock(&mutex);

  if(buffer_has_item==1){

  consume_item(buffer);

  buffer_has_item=0;

  }

  pthread_mutex_unlock(&mutex);

  pthread_delay_np(&delay);

  }

  }

  Declares the mutex variables mutex, pthread_mutex_t does not open the data types, including a system to distribute the property where the object. The function pthread_mutex_init is used to generate a mutex lock. The NULL parameter indicates that the default properties. If we need to declare the specific properties of the mutex, must call the function pthread_mutexattr_init. The function pthread_mutexattr_setpshared and pthread_mutexattr_settype function is used to set the mutex attribute. Before a function to set the attributes of pshared, it has two values, PTHREAD_PROCESS_PRIVATE and PTHREAD_PROCESS_SHARED. The former is used to a different thread in the process of synchronization, the latter for the different thread synchronization process. In the example above, using default attribute PTHREAD_PROCESS_ PRIVATE. The latter is used to set the mutex type, optional types are PTHREAD_MUTEX_NORMAL, PTHREAD_MUTEX_ERRORCHECK, PTHREAD_MUTEX_RECURSIVE and PTHREAD _MUTEX_DEFAULT. They are defined by different locking, unlocking mechanism, under normal circumstances, the final selection of a default property.

  The pthread_mutex_lock statement to lock mutex locked, then call pthread_mutex_unlock code until now, were locked, only one time is called a thread of execution. When a thread executes a pthread_mutex_lock location, if the lock is now another thread, the thread is blocked, the program will wait until another thread releases the mutex lock. In the example above, the use of the pthread_delay_np function, let the thread to sleep for a period of time, in order to prevent a thread always occupy this function.

  In the process of using the mutex lock would Deadlock: two threads trying to simultaneously occupy two resources, according to the different order of the corresponding mutex lock, such as two threads need to lock the mutex mutex in 1 and 2, the a thread to lock a mutex 1, B threads to lock the mutex lock 2, then there is a deadlock. You can use the function pthread_mutex_trylock, which is a function of the non blocking version of pthread_mutex_lock, when it found that deadlock is inevitable, it will return the corresponding information, the programmer can make the appropriate treatment for deadlock. In addition to deal with different mutexes type of deadlock is not the same, but the most important is to the programmer in program design attention to this point.

  The 2 condition variables

  In the previous section we described how to use a mutex to realize the sharing and communication of data between threads, mutexes one obvious disadvantage is that only two states: locking and non locking. And condition variables through methods allow threads to block and wait for another thread to send signals to make up for the lack of a mutex, it often and mutexes used together. When in use, the condition variable is used to block a thread, when the condition is not satisfied, the corresponding thread often unlock mutex lock and wait for change. Once a thread other changed condition variables, it will notify the corresponding condition variables wakes up one or more being the condition variable thread blocking. These threads will be locked mutex and re test conditions are satisfied. Generally speaking, condition variables are used for synchronization between threads.

  Structure variable of pthread_cond_t, function pthread_cond_init () is used to initialize a variable conditions. Its prototype:

  extern int pthread_cond_init __P ((pthread_cond_t *__cond,__const pthread_condattr_t *__cond_attr));

  The cond is a pointer to a pthread_cond_t pointer, cond_attr is a pointer to the pthread_condattr_t structure pointer. Structure of pthread_condattr_t is the attribute structure condition variable and mutex, as it can be used to set the condition variable is the process within the available or inter process is available, the default value is PTHREAD_ PROCESS_PRIVATE, namely the condition variable is the threads within the same process use. Note the initialization condition variables only is not used to re initialize or were released. Function to release a condition variable for pthread_cond_ destroy(pthread_cond_t cond).

  The function pthread_cond_wait () so that the threads blocked on a condition variable. The function prototype for it:

  extern int pthread_cond_wait __P ((pthread_cond_t *__cond,

  pthread_mutex_t *__mutex));

  Thread to lock and unlock the mutex cond blocking is condition variables. A thread can be function of pthread_cond_signal and function pthread_cond_broadcast to wake up, but it should be noted that, conditional variable is blocking and wake-up threads, conditions to determine the specific need to give users, such as whether a variable is 0 and so on, from this point can be seen in the following example. The thread is awakened, it will re check to determine whether meet the conditions, if still not satisfied, in general the thread should still blocked here, is waiting to be the next wake. The process of using while statement.

  Another is used for blocking a thread function is pthread_cond_timedwait (), the prototype for the:

  extern int pthread_cond_timedwait __P ((pthread_cond_t *__cond,

  pthread_mutex_t *__mutex, __const struct timespec *__abstime));

  It is the function pthread_cond_wait () many a time parameter abstime, experience after period of time, even if the condition variable is not satisfied, congestion also be lifted.

  The function pthread_cond_signal () of the prototype:

  extern int pthread_cond_signal __P ((pthread_cond_t *__cond));

  It is used to release is a thread is blocked in variable conditions on cond. Multiple threads to block on the condition variable, which thread is awakened is decided by the thread scheduling policy institute. It is necessary to pay attention to, must be to protect the function of protection condition variables, mutexes, otherwise the conditions to meet the signal may be made between the testing conditions and call the pthread_cond_wait function, resulting in unlimited waiting. Use the function pthread_cond_wait () and pthread_cond_signal () function of a simple example.

  pthread_mutex_t count_lock;

  pthread_cond_t count_nonzero;

  unsigned count;

  decrement_count () {

  pthread_mutex_lock (&count_lock);

  while(count==0)

  pthread_cond_wait( &count_nonzero, &count_lock);

  count=count -1;

  pthread_mutex_unlock (&count_lock);

  }

  increment_count(){

  pthread_mutex_lock(&count_lock);

  if(count==0)

  pthread_cond_signal(&count_nonzero);

  count=count+1;

  pthread_mutex_unlock(&count_lock);

  }

  The count value is 0, the decrement function block in pthread_cond_wait, and open the mutex count_lock. At this time, when the call to the function increment_count, pthread_cond_signal () function to change the condition variables, told decrement_count () to stop blocking.

  The function pthread_cond_broadcast (pthread_cond_t *cond) used to wake up all blocked in the condition variable cond thread. These threads are awakened will compete again the mutex lock, so care must be taken when using this function.

  The 3 signal

  The signal is essentially a non negative integer counter, which is used to control access to public resource. When public resources increases, the function call sem_post () to increase the signal. Only when the signal value is greater than 0, to the use of public resources, use, function sem_wait () to reduce the signal. The function sem_trywait (pthread_) and function (mutex_trylock) play the same role, it is a function (sem_wait) of the non blocking version. Some functions below introduce and semaphore related, they are defined in the /usr/include/semaphore.h header.

  The amount of signal data type for the structure of sem_t, it is essentially a long integer number. The function sem_init () used to initialize a semaphore. Its prototype:

  extern int sem_init __P ((sem_t *__sem, int __pshared, unsigned int __value));

  SEM is a pointer to the signal structure; pshared is not 0 when this signal is shared between processes, or can only be shared by all threads the current process; value gives the initial value of the semaphore.

  The function sem_post (sem_t *sem) is used to increase the value of the semaphore. When a thread is blocked in the signal quantity, call this function will make a thread which is no longer blocked, the selection mechanism is also determined by the thread scheduling policy.

  The function sem_wait (sem_t *sem) was used for the current thread is blocked until the signal SEM is greater than 0, to relieve the obstruction after SEM value minus one, that public resources after the use reduction. The function sem_trywait (sem_t *sem) is a function of sem_wait () of the non blocking version, it will signal the value of SEM.

  The function sem_destroy (sem_t *sem) to release the semaphore SEM.

  Following a signal example. In this example, a total of 4 threads, two of which thread is responsible for reading the data from the file buffer into the public, the other two thread reads the data from the buffer zone for different treatment (addition and multiplication).

  /* File sem.c */

  #include

  #include

  #include

  #define MAXSTACK 100

  int stack[MAXSTACK][2];

  int size=0;

  sem_t sem;

  /* Read data from the file 1.dat, each read once, signal plus one*/

  void ReadData1(void){

  FILE *fp=fopen("1.dat","r");

  while(!feof(fp)){

  fscanf(fp,"%d %d",&stack[size][0],&stack[size][1]);

  sem_post(&sem);

  ++size;

  }

  fclose(fp);

  }

  /*Read data from the file 2.dat*/

  void ReadData2(void){

  FILE *fp=fopen("2.dat","r");

  while(!feof(fp)){

  fscanf(fp,"%d %d",&stack[size][0],&stack[size][1]);

  sem_post(&sem);

  ++size;

  }

  fclose(fp);

  }

  /*Blocked waiting buffer data, read data, after the release of space, continue to wait*/

  void HandleData1(void){

  while(1){

  sem_wait(&sem);

  printf("Plus:%d+%d=%d\n",stack[size][0],stack[size][1],

  stack[size][0]+stack[size][1]);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Kristine at December 19, 2013 - 2:56 PM