一个小例子:
/***************************************************************************************************** * File name: ~vs2CAF.cpp * Create time: 2011/06/26 * Module: View * Author: zengqh * Blog: http://blog.csdn.net/aimyton * --------------------------------------------------------------------------------------------------- * Memo: * *****************************************************************************************************/ #include <stdlib.h> #include <stdio.h> #include "apue.h" #include <unistd.h> #include <errno.h> #include <pthread.h> /***************************************************************************************************** * Macro and Struct definition *****************************************************************************************************/ /***************************************************************************************************** * Global variables *****************************************************************************************************/ static volatile sig_atomic_t value = 0; static pthread_mutex_t mutex_handle; /***************************************************************************************************** * Global function declare *****************************************************************************************************/ /***************************************************************************************************** * Global function definition *****************************************************************************************************/ void* read_value(void* param) { for(;;) { pthread_mutex_lock(&mutex_handle); printf("read value: %d/n", value); usleep(20); pthread_mutex_unlock(&mutex_handle); } return NULL; } void* write_value(void* param) { for(;;) { pthread_mutex_lock(&mutex_handle); value++; printf("write value: %d/n", value); usleep(40); pthread_mutex_unlock(&mutex_handle); } return NULL; } int main() { int ret; pthread_t thread_id_1; pthread_t thread_id_2; ret = pthread_mutex_init(&mutex_handle, NULL); if(0 != ret) { printf("pthread mutex init failed./n"); return -1; } ret = pthread_create(&thread_id_1, NULL, read_value, NULL); if(0 != ret) { printf("pthead create failed for 1./n"); return -1; } ret = pthread_create(&thread_id_2, NULL, write_value, NULL); if(0 != ret) { printf("pthead create failed for 2./n"); return -1; } pthread_join(thread_id_1, NULL); pthread_join(thread_id_2, NULL); pthread_mutex_destroy(&mutex_handle); return 0; } /***************************************************************************************************** * Class declare ******************************************************************************************************/ /***************************************************************************************************** * Class definition *****************************************************************************************************/ /***********************************End of File*******************************************************/
posted on 2011-06-26 11:05 zengqh 阅读(218) 评论(0) 编辑 收藏 举报