libuv-thread-example

libuv thread example


更新記錄

item note
20160517 第一版

目錄


libuv thead

  • 目前存在两个主流的线程库, Windows 线程库实现和 pthreads,reference
  • pthreads - POSIX threads
  • A single process can contain multiple threads, all of which are executing the same program

範例說明

範例內容

1
2
3
4
5
6
7
8
9
10
11
int main() {
int tracklen = 10;
uv_thread_t hare_id;
uv_thread_t tortoise_id;
uv_thread_create(&hare_id, hare, &tracklen);
uv_thread_create(&tortoise_id, tortoise, &tracklen);

uv_thread_join(&hare_id);
uv_thread_join(&tortoise_id);
return 0;
}

範例測試

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[ubuntu](master-4113bbf)1h18m root@e8b0cd737680:[t11_thread-create]$ ./t11-main-ubuntu 
Tortoise ran another step
Hare ran another step
Hare ran another step
Tortoise ran another step
Hare ran another step
Hare ran another step
Hare ran another step
Tortoise ran another step
Hare ran another step
Hare ran another step
Hare ran another step
Tortoise ran another step
Hare ran another step
Hare ran another step

thread lock

读写锁是一种更细粒度的访问策略.两个线程可以同时访问共享内存区域, 当读线程拥有锁时, 写线程并不能获取到锁,reference

範例說明

  • uv_barrier_init
    • pthread_barrier_init
    • pthread_mutex_init(&barrier->mutex, NULL); //如果參數attr爲NULL,則使用默認的互斥鎖屬性,默認屬性爲快速互斥鎖
    • pthread_cond_init(&barrier->cond, NULL); // initialise condition variables
  • uv_rwlock_init
    • pthread_rwlock_init(rwlock,NULL)
    • initialises the read-write lock referenced by rwlock with the attributes referenced by attr. If attr is NULL, the default read-write lock attributes are used,source
  • uv_barrier_wait
    • pthread_mutex_lock(&barrier->mutex);
    • if(–barrier->count == 0 ) return..
    • Otherwise, wait for other threads until the count reaches 0
    • pthread_cond_wait //wait on a condition
    • pthread_mutex_unlock(&barrier->mutex);
  • uv_barrier_destroy
    • barrier->count = 0;
    • pthread_cond_destroy(&
      barrier->cond);
    • pthread_mutex_destroy(&barrier->mutex);

struct pthread_barrier_t

範例內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
uv_barrier_init(&blocker, 4);

shared_num = 0;
uv_rwlock_init(&numlock);

uv_thread_t threads[3];

int thread_nums[] = {1, 2, 3};
uv_thread_create(&threads[0], reader, &thread_nums[0]);
uv_thread_create(&threads[1], reader, &thread_nums[1]);

uv_thread_create(&threads[2], writer, &thread_nums[2]);

uv_barrier_wait(&blocker);
uv_barrier_destroy(&blocker);

uv_rwlock_destroy(&numlock);
return 0;
}

範例測試

  • 不了解為何一開始只有..Reader1無2&3 ?
  • 使用 uv_rwlock_rdlock(讀lock) /uv_rwlock_wrlock (寫lock)好處理,確保程式一至性(即在unlock之前是不會被中斷)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[ubuntu](master-8af3563)0h20m root@e8b0cd737680:[t12_thread-lock]$ ./t12-main-ubuntu 
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
Reader 1: acquired lock
Reader 1: shared num = 0
Reader 1: released lock
...
Writer 3: acquired lock
Writer 3: incremented shared num = 15
Writer 3: released lock
Reader 2: acquired lock
Reader 2: shared num = 15
Reader 2: released lock
Writer 3: acquired lock
Writer 3: incremented shared num = 16
Writer 3: released lock
Reader 2: acquired lock
Reader 2: shared num = 16
Reader 2: released lock
Writer 3: acquired lock
Writer 3: incremented shared num = 17
Writer 3: released lock
Writer 3: acquired lock
Writer 3: incremented shared num = 18
Writer 3: released lock
Writer 3: acquired lock
Writer 3: incremented shared num = 19
Writer 3: released lock
Writer 3: acquired lock
Writer 3: incremented shared num = 20
Writer 3: released lock

參考來源