Learning critical section with pthread


I've just started to learn how to implement multi-threaded programming.
It's very easy because we just need to call two APIs, such as

- pthread_create : Create a new thread
- pthread_join : Join with a terminated thread

In the following program, global variable "gNum" is incremented by several threads.
The number of threads are defined as "THREAD_NUM".
The fourth argument "loop" means the number of incrementation per one thread.
In the for-loop inside addFunc function, gNum is read and updated.
If at least two thread try to read gNum at the same time, gNum variable won't be updated correctly.
Finally, this program can't achieve that gNum is equal to ( THREAD_NUM * loop ).




コメント

このブログの人気の投稿

Callback関数を知らん人がまず理解すべきことのまとめ。

C言語でBluetoothスタックを叩きたい人のBluetooth開発入門その1

C++プログラミング入門(1) // 倉庫番プログラムの実装