嵌入式 互斥鎖和讀寫鎖區別
睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接
- /*
- * 線程同步——互斥量
- * 創建兩個線程,使用互斥量使任一時刻只有一個線程對全局變量進行
- 操作
- * Lzy 2011-6-19
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- pthread_mutex_t mutex; /* 定義
- 互斥量 */
- int x;
- /* 定義全局變量 */
- void thread1(void) /* 定義線程1運
- 行的函數,其功能是對全局變量x進行逐減操作 */
- {
- while(x>0)
- {
- pthread_mutex_lock(&mutex); /* 對互斥量進行
- 加鎖操作 */
- printf("Thread 1 is running : x=%d \n",x);
- x--;
- pthread_mutex_unlock(&mutex); /* 對互斥量進行
- 解鎖操作 */
- sleep(1);
- }
- pthread_exit(NULL);
- }
- void thread2(void) /* 定義線程2運
- 行的函數,功能與thread2相同 */
- {
- while(x>0)
- {
- pthread_mutex_lock(&mutex); /* 對互斥量進行
- 加鎖操作 */
- printf("Thread 2 is running : x=%d \n",x);
- x--;
- pthread_mutex_unlock(&mutex); /* 對互斥量進行
- 解鎖操作 */
- sleep(1);
- }
- pthread_exit(NULL);
- }
- int main(void)
- {
- pthread_t id1,id2;
- /* 定義線程的標識符 */
- int ret;
- ret = pthread_mutex_init(&mutex,NULL); /* 對互斥量進行
- 初始化,這里使用默認的屬性 */
- if(ret != 0)
- {
- printf ("Mutex initialization failed.\n"); /* 如果
- 初始化失敗,打印錯誤信息 */
- exit (1);
- }
- x=10;
- /* 對全局變量賦初值 */
- ret = pthread_create(&id1, NULL, (void *)&thread1, NULL);
- /* 創建線程1 */
- if(ret != 0)
- {
- printf ("Thread1 creation failed.\n");
- exit (1);
- }
- ret = pthread_create(&id2, NULL, (void *)&thread2, NULL);
- /* 創建線程2 */
- if(ret != 0)
- {
- printf ("Thread2 creation failed.\n");
- exit (1);
- }
- pthread_join(id1, NULL); /*線程
- 合并 */
- pthread_join(id2, NULL);
- return (0);
- }
- /*
- * 線程同步
- * ——讀寫鎖
- * 只要沒有進程持有某個給定的讀寫鎖用于寫,那么任意數目的
- 線程都可持有該讀寫鎖用于讀
- * 僅當沒有線程持有某個給定的讀寫鎖用于讀或寫,才能分配該
- 讀寫鎖用于寫。
- * Lzy 2011-6-19
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- int product = 0; //定義全局變量
- pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; //靜態
- 初始化讀寫鎖
- void * threadRead(void * arg) //線程函數讀
- {
- int cnt = 0;
- while(cnt++ < 100)
- {
- pthread_rwlock_rdlock(&rwlock); //讀鎖
- printf("Read: product = %d\n", product);
- pthread_rwlock_unlock(&rwlock); //解鎖
- sleep(1);
- }
- }
- void * tidProduce(void * arg) //線程函數寫 加1
- {
- int cnt = 0;
- while(cnt++ < 100)
- {
- pthread_rwlock_wrlock(&rwlock); //寫鎖
- product++;
- printf("Produce: product = %d\n", product);
- pthread_rwlock_unlock(&rwlock); //解鎖
- sleep(1);
- }
- }
- void * threadConsume(void * arg) //線程函數寫 減1
- {
- int cnt = 0;
- while(cnt++ < 100)
- {
- pthread_rwlock_wrlock(&rwlock); //寫鎖
- product--;
- printf("Consume: product = %d\n", product);
- pthread_rwlock_unlock(&rwlock); //解鎖
- sleep(2);
- }
- }
- int main(void)
- {
- int i;
- pthread_t tid[10], tidconsume, tidproduce;
- for(i = 0; i < 2; i++)
- {
- if(pthread_create(&tid[i], NULL, threadRead,
- NULL))
- {
- printf("pthread_create error\n");
- exit(0);
- }
- }
- if(pthread_create(&tidproduce, NULL, tidProduce, NULL))
- {
- printf("pthread_create error\n");
- exit(0);
- }
- if(pthread_create(&tidconsume, NULL, threadConsume,
- NULL))
- {
- printf("pthread_create error\n");
- exit(0);
- }
- pthread_exit(NULL); //等待所有線程結束
- return 0;
- }