线程间通信

线程间通信–互斥量

1 /******************************************************************************* 2 * 版权所有: 3 * 模 块 名: 4 * 文 件 名:pthread4_addnum.c 5 * 实现功能: 6 * 作 者:XYZ 7 * 版 本:V1.0 8 * 日 期:2013.07.26 9 * 其他说明:无#include<stdio.h>13 #include<unistd.h>14 #include<stdlib.h>15 #include<pthread.h>*thread_function(void *arg);18 pthread_mutex_t num_mutex;19 #define PTHREAD_NUM220 #define NUM3021 int cnt = 0; main()24 {25int res;26 pthread_t thread_id[PTHREAD_NUM];27void *thread_result;28res = pthread_mutex_init(&num_mutex, NULL);29if (res != 0)30 {);32 exit(EXIT_FAILURE);33 } i ;(i=0; i<PTHREAD_NUM; ++i)38 {39pthread_create(&thread_id[i], NULL, thread_function, (void*)thread_id[i]);40 }143for (i=0; i<NUM; ++i)44 {45pthread_mutex_lock(&num_mutex);,__FUNCTION__,pthread_self(), cnt);47cnt += 1;, cnt);49pthread_mutex_unlock(&num_mutex);50 sched_yield();51usleep(1);52 }(i=0; i<PTHREAD_NUM; ++i)56 {57 pthread_join(thread_id[i], NULL);58 }59pthread_mutex_destroy(&num_mutex);60 61pthread_exit(0);62 }*thread_function(void *arg)65 {66int i;67for (i=0; i<NUM; ++i)68 {69pthread_mutex_lock(&num_mutex);fprintf(stderr, ,__FUNCTION__,pthread_self(), cnt);72cnt += 1;, cnt);74pthread_mutex_unlock(&num_mutex);75 sched_yield();76usleep(1);77 }78 pthread_exit(NULL);79 }

1.pthread_mutex_init(&work_mutex, NULL); //init the mutex pthread_mutex_destroy(&work_mutex);pthread_mutex_destroy, pthread_mutex_init – destroy and initialize a mutex#include <pthread.h>int pthread_mutex_destroy(pthread_mutex_t *mutex);int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; The pthread_mutex_init() function shall initialize the mutex referenced by mutex with attributes specified by attr. If attr is NULL, the default mutex attributes are used; Only mutex itself may be used for performing synchronization. The result of referring to copies of mutex in calls to pthread_mutex_lock(), pthread_mutex_trylock(), pthread_mutex_unlock(), and pthread_mutex_destroy() is undefined.Attempting to initialize an already initialized mutex results in undefined behavior.In cases where default mutex attributes are appropriate, the macro PTHREAD_MUTEX_INITIALIZER can be used to initialize mutexes that are statically allocated. The effect shall be equivalent to dynamic initialization by a call to pthread_mutex_init() with parameter attr specified as NULL, except that no error checks are performed.If successful, the pthread_mutex_destroy() and pthread_mutex_init() functions shall return zero; otherwise, an error number shall be returned to indicate the error.2.pthread_mutex_lock(&work_mutex); pthread_mutex_unlock(&work_mutex);pthread_mutex_lock, pthread_mutex_trylock, pthread_mutex_unlock – lock and unlock a mutex#include <pthread.h>int pthread_mutex_lock(pthread_mutex_t *mutex);int pthread_mutex_trylock(pthread_mutex_t *mutex);int pthread_mutex_unlock(pthread_mutex_t *mutex);The mutex object referenced by mutex shall be locked by calling pthread_mutex_lock(). If the mutex is already locked, the calling thread shall block until the mutex becomes available. This operation shall return with the mutex object referenced by mutex in the locked state with the calling thread as its owner.If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not be provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results.The pthread_mutex_trylock() function shall be equivalent to pthread_mutex_lock(), except that if the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call shall return immediately.The pthread_mutex_unlock() function shall release the mutex object referenced by mutex. The manner in which a mutex is released is dependent upon the mutex’s type attribute.If successful,the pthread_mutex_lock() and pthread_mutex_unlock() functions shall return zero; otherwise, an error number shall be returned to indicate the error.The pthread_mutex_trylock() function shall return zero if a lock on the mutex object referenced by mutex is acquired. Otherwise, an error number is returned to indicate the error

posted on

,香港服务器,网站空间,香港虚拟主机曾经一直想让别人知道自己的心情,那些沉重,

线程间通信

相关文章:

你感兴趣的文章:

标签云: