/* like any C program, program's execution begins in main */
int
main(int argc, char* argv[])
{
    int        i;              /* loop counter                          */
    int        thr_id1;        /* thread ID for the first new thread    */
    int        thr_id2;        /* thread ID for the second new thread   */
    pthread_t  p_thread1;      /* first thread's structure              */
    pthread_t  p_thread2;      /* second thread's structure             */
    int        num1      = 1;  /* thread 1 employee number              */
    int        num2      = 2;  /* thread 2 employee number              */
    struct employee eotd;      /* local copy of 'employee of the day'.  */
    struct employee* worker;   /* pointer to currently checked employee */

    /* initialize employee of the day to first 1. */
    copy_employee(&employees[0], &employee_of_the_day);

    /* create a new thread that will execute 'do_loop()' with '1'       */
    thr_id1 = pthread_create(&p_thread1, NULL, do_loop, (void*)&num1);
    /* create a second thread that will execute 'do_loop()' with '2'    */
    thr_id2 = pthread_create(&p_thread2, NULL, do_loop, (void*)&num2);

    /* run a loop that verifies integrity of 'employee of the day' many */
    /* many many times.....                                             */
    for (i=0; i<60000; i++) {
        /* save contents of 'employee of the day' to local 'worker'.    */
        copy_employee(&employee_of_the_day, &eotd);
	worker = &employees[eotd.number-1];

        /* compare employees */
	if (eotd.id != worker->id) {
	    printf("mismatching 'id' , %d != %d (loop '%d')\n",
		   eotd.id, worker->id, i);
	    exit(0);
	}
	if (strcmp(eotd.first_name, worker->first_name) != 0) {
	    printf("mismatching 'first_name' , %s != %s (loop '%d')\n",
		   eotd.first_name, worker->first_name, i);
	    exit(0);
	}
	if (strcmp(eotd.last_name, worker->last_name) != 0) {
	    printf("mismatching 'last_name' , %s != %s (loop '%d')\n",
		   eotd.last_name, worker->last_name, i);
	    exit(0);
	}
	if (strcmp(eotd.department, worker->department) != 0) {
	    printf("mismatching 'department' , %s != %s (loop '%d')\n",
		   eotd.department, worker->department, i);
	    exit(0);
	}
	if (eotd.room_number != worker->room_number) {
	    printf("mismatching 'room_number' , %d != %d (loop '%d')\n",
		   eotd.room_number, worker->room_number, i);
	    exit(0);
	}
    }

    printf("Glory, employees contents was always consistent\n");
    
    return 0;
}
/* function to be executed by the variable setting threads thread */
void*
do_loop(void* data)
{
    int my_num = *((int*)data);   /* thread identifying number         */

    while (1) {
        /* set employee of the day to be the one with number 'my_num'. */
	copy_employee(&employees[my_num-1], &employee_of_the_day);
    }
}
Example #3
0
/* 设置本日雇员的函数 */
void*
do_loop(void* data)
{
    int my_num = *((int*)data);   /* 线程的雇员编号 */

    while (1) {
        /* 设置本日雇员为编号 my_num 的雇员 */
	copy_employee(&employees[my_num-1], &employee_of_the_day);
    }
}
Example #4
0
/* 主程序,或主线程 */
int
main(int argc, char* argv[])
{
    int        i;                      /* 循环计数器           */
    int        ret1;
    int        ret2;
    pthread_t  p_thread1;              /* 第一个线程结构       */
    pthread_t  p_thread2;              /* 第二个线程结构       */
    int        num1      = 1;          /* 第一个线程的雇员编号 */
    int        num2      = 2;          /* 第二个线程的雇员编号 */
    struct employee eotd;              /* 本日雇员的本地拷贝   */
    struct employee* worker;           /* 指向正在检查的雇员   */

    /* 初始化本日雇员为第一个雇员 */
    copy_employee(&employees[0], &employee_of_the_day);

    /* 创建一个新线程,以数据 1 执行 do_loop(),即设置 1 号雇员为本日雇员 */
    ret1 = pthread_create(&p_thread1, NULL, do_loop, (void*)&num1);
    /* 创建第二个新线程,以数据 2 执行 do_loop(),即设置 2 号雇员为本日雇员 */
    ret2 = pthread_create(&p_thread2, NULL, do_loop, (void*)&num2);

    /* 创建一个循环,检查本日雇员的数据是否一致,即是否属于同一位雇员 */
    for (i=0; i<60000; i++) {
        /* 保存本日雇员内容 */
        copy_employee(&employee_of_the_day, &eotd);
        /* 取出本日雇员的原始数据*/
	worker = &employees[eotd.number-1];

        /* 比较数据 */
	if (eotd.id != worker->id) {
	    printf("mismatching 'id' , %d != %d (loop '%d')\n",
		   eotd.id, worker->id, i);
	    exit(0);
	}
	if (strcmp(eotd.first_name, worker->first_name) != 0) {
	    printf("mismatching 'first_name' , %s != %s (loop '%d')\n",
		   eotd.first_name, worker->first_name, i);
	    exit(0);
	}
	if (strcmp(eotd.last_name, worker->last_name) != 0) {
	    printf("mismatching 'last_name' , %s != %s (loop '%d')\n",
		   eotd.last_name, worker->last_name, i);
	    exit(0);
	}
	if (strcmp(eotd.department, worker->department) != 0) {
	    printf("mismatching 'department' , %s != %s (loop '%d')\n",
		   eotd.department, worker->department, i);
	    exit(0);
	}
	if (eotd.room_number != worker->room_number) {
	    printf("mismatching 'room_number' , %d != %d (loop '%d')\n",
		   eotd.room_number, worker->room_number, i);
	    exit(0);
	}
    }

    /* 全部数据都一致 */
    printf("Glory, employees contents was always consistent\n");
    
    return 0;
}