pthread_spin_lockは、スピンロックをかけるライブラリ関数・・・。
pthread_spin_lock(3): lock spin lock object - Linux man page
ミューテックスロックされた場合には、他スレッドはサスペンド状態で待つことになるが、スピンロックされた場合には、他スレッドはビジ―ウェイト状態で待つことになる・・・。
pthreadについて(同期) (2/4):CodeZine(コードジン)
なので、CPU使用率が上がるのが特徴・・・。
とメンバにpthread_spinlock_t型spinポインタ・・・。
spinを初期化してスレッド間で共有・・・。
spinは他プロセス間でも共有できるが、今回はPTHREAD_PROCESS_PRIVATEで自プロセスのみ・・・。
スピンロックして、出力して、スピンアンロック・・・。
ミューテックスと同じ使い方・・・。
$ ./pthread_spin_lock i = 1, no = 3 i = 2, no = 3 i = 3, no = 3 i = 4, no = 3 i = 5, no = 3 i = 1, no = 1 i = 2, no = 1 i = 3, no = 1 i = 4, no = 1 i = 5, no = 1 i = 1, no = 2 i = 2, no = 2 i = 3, no = 2 i = 4, no = 2 i = 5, no = 2 i = 1, no = 4 i = 2, no = 4 i = 3, no = 4 i = 4, no = 4 i = 5, no = 4 i = 1, no = 0 i = 2, no = 0 i = 3, no = 0 i = 4, no = 0 i = 5, no = 0 $
結果も変わらない・・・。
psコマンドで、
psコマンドでスレッドを表示させたり、スレッドごとのCPU使用率を確認する - 元RX-7乗りの適当な日々
前回のpthread_mutex_lockとCPU使用率を比べると、
$ ps auxww -L | grep -e PID -e mutex USER PID LWP %CPU NLWP %MEM VSZ RSS TTY STAT START TIME COMMAND bg1 27517 27517 0.0 5 0.0 0 0 pts/0 Zl+ 11:00 0:00 [pthread_mutex_l] <defunct> bg1 27517 27518 0.0 5 0.0 59764 728 pts/0 Sl+ 11:00 0:00 [pthread_mutex_l] <defunct> bg1 27517 27519 0.0 5 0.0 59764 728 pts/0 Sl+ 11:00 0:00 [pthread_mutex_l] <defunct> bg1 27517 27520 0.0 5 0.0 59764 728 pts/0 Sl+ 11:00 0:00 [pthread_mutex_l] <defunct> bg1 27517 27522 0.0 5 0.0 59764 728 pts/0 Sl+ 11:00 0:00 [pthread_mutex_l] <defunct> bg1 27528 27528 0.0 1 0.0 112608 1016 pts/1 S+ 11:00 0:00 grep --color=auto -e PID -e mutex $ ps auxww -L | grep -e PID -e spin USER PID LWP %CPU NLWP %MEM VSZ RSS TTY STAT START TIME COMMAND bg1 27535 27535 0.0 6 0.0 0 0 pts/0 Zl+ 11:01 0:00 [pthread_spin_lo] <defunct> bg1 27535 27536 9.7 6 0.0 59764 720 pts/0 Rl+ 11:01 0:00 [pthread_spin_lo] <defunct> bg1 27535 27537 9.7 6 0.0 59764 720 pts/0 Rl+ 11:01 0:00 [pthread_spin_lo] <defunct> bg1 27535 27538 10.0 6 0.0 59764 720 pts/0 Rl+ 11:01 0:00 [pthread_spin_lo] <defunct> bg1 27535 27539 0.0 6 0.0 59764 720 pts/0 Sl+ 11:01 0:00 [pthread_spin_lo] <defunct> bg1 27535 27540 10.0 6 0.0 59764 720 pts/0 Rl+ 11:01 0:00 [pthread_spin_lo] <defunct> bg1 27543 27543 0.0 1 0.1 112612 1116 pts/1 S+ 11:01 0:00 grep --color=auto -e PID -e spin $
mutexは0.0に対して、spinは9.7~10.0とCPU使用率が上がっている・・・。
Sample/pthread_spin_lock.c at master · bg1bgst333/Sample · GitHub