- 论坛徽章:
- 1
|
//谁休眠 谁拿锁???? 这是我整个疑问
程序不短 但是几乎都是重复的代码、语法简单、 希望您能耐心的看下去。
代码中 在有疑问的地方、我注了注释- #include "test_mutex.h"
- static unityos_mutex_t * test_mutex = NULL;
- void mutex_in()
- {
- pthread_t tid6, tid7;
- int rc6 = 0, rc7 = 0;
- int i;
- test_mutex = unityos_mutex_create();
-
- rc6 = pthread_create(&tid6, NULL, mutex_write_one, NULL);
- if ( rc6 != NULL)
- {
- printf("thread_tran_msg create\n");
- }
- rc7 = pthread_create(&tid7, NULL, mutex_write_two, NULL);
- if ( rc7 != NULL)
- {
- printf("thread_tran_msg create\n");
- }
- pthread_join(tid6, NULL);
- pthread_join(tid7, NULL);
- unityos_mutex_destroy(test_mutex);
-
- }
- /*****************************************************************************
- 函 数 名 : mutex_write_one
- 功能描述 : TASK_3 用于互斥读结构体
- 输入参数 : void* args
- 输出参数 : 无
- 返 回 值 : void
- 主调函数 :
- 被调函数 :
-
- 修改历史 :
- 1.日 期 : 2011年4月28日
- 作 者 : Eli.Liu
- 修改内容 : 新生成函数
- *****************************************************************************/
- void *mutex_write_one(void* args)
- {
-
- int i, count;
- count = 1;
- while(count++ < 5)
- {
-
- unityos_mutex_lock(test_mutex);
- printf("******************one\n");
- for(i = 0; i < 3; i++)
- {
- strcpy(per_inf[i].birthday, "1999-01-01");
- strcpy(per_inf[i].name, "lisi");
- }
- mutex_print();
- unityos_mutex_unlock(test_mutex);
- sleep(1); //如果休眠 write_one就可以持续那锁、一直等到 while循环结束write_two才能拿到
-
-
- }
- return 0;
-
- }
- /*****************************************************************************
- 函 数 名 : mutex_write_two
- 功能描述 : TASK_3 用于互斥读结构体
- 输入参数 : void* args
- 输出参数 : 无
- 返 回 值 : void
- 主调函数 :
- 被调函数 :
-
- 修改历史 :
- 1.日 期 : 2011年4月28日
- 作 者 : Eli.Liu
- 修改内容 : 新生成函数
- *****************************************************************************/
- void * mutex_write_two(void* args)
- {
- int i, count;
- count = 1;
- while(count++ < 5)
- {
-
- unityos_mutex_lock(test_mutex);
- printf("******************two\n");
- for(i = 0; i < 3; i++)
- {
- strcpy(per_inf[i].birthday, "2012-12-22");
- strcpy(per_inf[i].name, "xiaoliu");
- }
- mutex_print();
- unityos_mutex_unlock(test_mutex);
-
- sleep(1); //如果休眠 write_two就可以持续那锁、一直等到 while循环结束write_one才能拿到
- //2、如果两个线程都休眠 则交替拿锁?
- //3、如果两个线程都不休眠 则 write_one持续拿锁 、直到其while循环结束
-
-
- }
- return 0;
- }
- void mutex_print()
- {
- int i, count;
- count = 1;
- while(count++ < 5)
- {
- printf("name\t\tbirthday\n");
- for(i = 0; i < 3; i++)
- {
- printf("%s\t", per_inf[i].name);
- printf("%s\n", per_inf[i].birthday);
- }
- sleep(1);
- }
-
- }
复制代码 |
|