互斥锁的使用

  [root@localhost threads]# cat mutex.c
#include
#include
#include
#include

#define  NUMS_ARRAY 30

int num=0;

int arr[NUMS_ARRAY];
void *thread_function(void *arg)
{
    int i=(int)arg;
    int j;
    for(j=0;j<NUMS_ARRAY;j++)
    {
            printf("%d:%d:%d   ",i,j,arr[j]);
            arr[j]=j;
            printf("%d:%d:%d \n",i,j,arr[j]);
    }
}


void *thread_function1(void *arg)
{
    int i=(int)arg;
    int j;
    pthread_mutex_t mymutex;
    pthread_mutex_init(&mymutex,NULL);
    pthread_mutex_lock(&mymutex);
    for(j=0;j<NUMS_ARRAY;j++)
    {
        printf("%d:%d:%d   ",i,j,arr[j]);
        arr[j]=99;
        printf("%d:%d:%d \n",i,j,arr[j]);
    }
    pthread_mutex_unlock(&mymutex);
    pthread_mutex_destroy(&mymutex);
}

int main()
{
    pthread_t mythread1;
    pthread_t mythread2;

     if(pthread_create(&mythread1, NULL, thread_function, (void*)1))
    {
       printf("error creating thread.\n");
       abort();
    }
    else
    {
       printf("create thread 1\n");
    }

    if(pthread_create(&mythread2, NULL, thread_function1, (void*)2))
    {
       printf("error creating thread.\n");
       abort();
    }
    else
    {
       printf("create thread 2\n");
    }

    if(pthread_join(mythread1, NULL))
    {
       printf("error joining thread\n");
       abort();
    }
    else
    {
        printf("Join thread 1\n");
    }

    if(pthread_join(mythread2, NULL))
    {
       printf("error joining thread\n");
       abort();
    }
    else
    {
        printf("Join thread 2\n");
    }

    printf("into main thread now, pid%ud \n",(unsigned int)getpid());
    int i=0;
    for(;i
        printf("%d:%d ",i,arr);
    printf("\n");
    return 0;
}

第X次输出: 可以看出加了锁的 thread_function1 的for内赋值未被其他线程打断
[root@localhost threads]# ./mutex
create thread 1
create thread 2
1:0:0   1:0:0
1:1:0   1:1:1
1:2:0   1:2:2
1:3:0   1:3:3
1:4:0   1:4:4
1:5:0   1:5:5
1:6:0   1:6:6
1:7:0   1:7:7
1:8:0   1:8:8
1:9:0   1:9:9
1:10:0   1:10:10
1:11:0   1:11:11
1:12:0   1:12:12
1:13:0   1:13:13
1:14:0   1:14:14
1:15:0   1:15:15
1:16:0   1:16:16
1:17:0   1:17:17
1:18:0   1:18:18
1:19:0   1:19:19
1:20:0   1:20:20
1:21:0   1:21:21
1:22:0   1:22:22
1:23:0   1:23:23
1:24:0   1:24:24
1:25:0   1:25:25
1:26:0   1:26:26
1:27:0   1:27:27
1:28:0   1:28:28
1:29:0   1:29:29
Join thread 1
2:0:0   2:0:99  (以下的赋值是从0-29所以未被其他线程打断)
2:1:1   2:1:99
2:2:2   2:2:99
2:3:3   2:3:99
2:4:4   2:4:99
2:5:5   2:5:99
2:6:6   2:6:99
2:7:7   2:7:99
2:8:8   2:8:99
2:9:9   2:9:99
2:10:10   2:10:99
2:11:11   2:11:99
2:12:12   2:12:99
2:13:13   2:13:99
2:14:14   2:14:99
2:15:15   2:15:99
2:16:16   2:16:99
2:17:17   2:17:99
2:18:18   2:18:99
2:19:19   2:19:99
2:20:20   2:20:99
2:21:21   2:21:99
2:22:22   2:22:99
2:23:23   2:23:99
2:24:24   2:24:99
2:25:25   2:25:99
2:26:26   2:26:99
2:27:27   2:27:99
2:28:28   2:28:99
2:29:29   2:29:99
Join thread 2
into main thread now, pid6677d
0:99 1:99 2:99 3:99 4:99 5:99 6:99 7:99 8:99 9:99 10:99 11:99 12:99 13:99 14:99 15:99 16:99 17:99 18:99 19:99 20:99 21:99 22:99 23:99 24:99 25:99 26:99 27:99 28:99 29:99

第N次输出: 可以看出加了锁的 thread_function1 的for内赋值未被其他线程打断
[root@localhost threads]# ./mutex
create thread 1
create thread 2
1:0:0   1:0:0
1:1:0   1:1:1
1:2:0   1:2:2
1:3:0   1:3:3
1:4:0   1:4:4
1:5:0   1:5:5
1:6:0   1:6:6
1:7:0   1:7:7
1:8:0   1:8:8
1:9:0   1:9:9
1:10:0   1:10:10
1:11:0   1:11:11
1:12:0   1:12:12
1:13:0   1:13:13
1:14:0   1:14:14
1:15:0   1:15:15
1:16:0   1:16:16
1:17:0   1:17:17
1:18:0   1:18:18
1:19:0   1:19:19
1:20:0   1:20:20
1:21:0   1:21:21
2:0:0   2:0:99
2:1:1   2:1:99
2:2:2   2:2:99
2:3:3   2:3:99
2:4:4   2:4:99
2:5:5   2:5:99
2:6:6   2:6:99
2:7:7   2:7:99
2:8:8   2:8:99
2:9:9   2:9:99
2:10:10   2:10:99
2:11:11   2:11:99
2:12:12   2:12:99
2:13:13   2:13:99
2:14:14   2:14:99
2:15:15   2:15:99
2:16:16   2:16:99
2:17:17   2:17:99
2:18:18   2:18:99
2:19:19   2:19:99
2:20:20   2:20:99
2:21:21   2:21:99
2:22:0   2:22:99
2:23:0   2:23:99
2:24:0   2:24:99
2:25:0   2:25:99
2:26:0   2:26:99
2:27:0   2:27:99
2:28:0   2:28:99
2:29:0   2:29:99
1:22:99   1:22:22
1:23:99   1:23:23
1:24:99   1:24:24
1:25:99   1:25:25
1:26:99   1:26:26
1:27:99   1:27:27
1:28:99   1:28:28
1:29:99   1:29:29
Join thread 1
Join thread 2
into main thread now, pid6680d
0:99 1:99 2:99 3:99 4:99 5:99 6:99 7:99 8:99 9:99 10:99 11:99 12:99 13:99 14:99 15:99 16:99 17:99 18:99 19:99 20:99 21:99 22:22 23:23 24:24 25:25 26:26 27:27 28:28 29:29




[ 此贴被cnscn在2007-01-11 15:19重新编辑 ]

 » 相关连接:
 » 本栏目最新帖:
 » 精华帖:

Powered by PHPWind v6.0 Code © 2003-08