Kenalan Karo Thread

Equan P.
Pemrograman
Published in
4 min readSep 15, 2016

Sakjane lek dijelasno langsung opo iku thread yo iso sampeyan woco dewe nang wikipedia tapi tulisan iki nggawe pendekatan cara-cara praktis ben luwih cetha iso nggambarno opo sakjane thread iku.

Multi-Threading Hardware

Lek di delok tekok hardware, prosesor Intel Pentium misale (koyok gambar ndek nduwur) mesti duwe seng jenenge kemampuan maksimal thread ndek tiap prosesor core, istilae di jenengno hardware thread. Lek ndek prosesor Intel teknologi iki biasane di jenengi Hyper-Threading utowo Multi-Threading . Contoe Intel Pentium B940 iku iso nglakokno 2 thread tiap core, nah total core ndek prosesor iki ono 2 dadi total thread sing iso dilakokno barengan iku jumlahe ono 4.

Terus yakopo carane prosesor iki nglakokno thread? Prosesor Intel nggawe cara sing jenenge context switching, yo iku cara njupuk lan ngeksekusi perintah-perintah komputer tapi gentenan. Nah saking cepete switching iki (tergantung cepete clock prosesor) koyok-koyok task komputer sing dilakokno iku keroso barengan. Lek koyok Intel Pentium B490 iku maksimal thread per-core yo iku ono 2.

$ lstopo

PU P#0 artie Processing Unit Processor #0 utowo CPU nomer 0. Mangkane masano Pentium B940 iku duwe 2 core tapi sak core iku mek duwe siji CPU lan maksimal iso ngelakokno 2 thread.

Multitasking Operating System

Ono maneh sing jenenge scheduler sing mesti onok ndek Operating System (OS) modern. Scheduler iki fungsie ngatur thread supoyo iso gantian lek nggawe prosesor. Dadi koyok koyoke OS iki iso ngelakokno task barengan utowo paralel tapi aslie mek iso ngelakokno task sitok-sitok kecuali lek misale prosesor iku duwe fitur multi-threading koyok sing dijelasno ndek nduwur iku maeng, biasane OS modern wes mesti ngusahakno ngelakokno thread iku secara hardware pisan soale efeke nang komputer yo iku mlakune iso luwih cepet dadi hemat wektu lan terkadang hemat energi pisan.

Carane ngecek prosesor ndek komputer iku duwe fitur multi-threading, misale lek sampeyan gawe Ubuntu Linux 16.04 iso nggawe perintah

$ sudo dmidecode

Flag HTT (Multi-threading) lan Status: Populated, Enabled iku nandakno lek fitur multi-threading wes aktif.

Process

Lek ndek OS Linux sing jenenge process yo iku maeng task utowo tugas-tugas sing kudu dilakokno oleh komputer, salah siji cirine process yo iku mesti duwe sing jenenge PID (Process ID).

$ ps -e | grep node
14030 pts/5 00:00:00 node

Biasanae process iku mesti kumpulan tekok threads. Lek PID = 14030, thread opo ae sing ndek njero process iku iso didelok pisan lan thread iku yo duwe jeneng unik sing diarani TID (Thread ID).

$ ps -p 14030 -L -o pid,tid,cpuid
PID TID CPUID
14030 14030 0
14030 14031 1
14030 14032 1
14030 14033 1
14030 14034 1
14030 14035 1

lek nggawe perintah ps iso didelok pisan CPU terakhir sing digawe ngelakokno thread iku maeng, lek ndek conto nduwur iku iso sampeyan delok ndek kolom CPUID. Data liyane iso didelok pisan liwat perintah top koyok ndek isor iki

$ top -H -p 14030

Node.js Thread

Masano platform Node.js iku gak duwe multi-thread tapi sampeyan iso ngguna’ake modul npm sing jenenge WebWorker Threads. Fungsi utama modul iki yoiku nguncalno task sing akeh mangan cycle CPU supaya dadi thread dadi gak nganggu event loop ndek Node.js, task ngene iki biasanae tandae yoiku butuh wektu sing suwe ben task iku iso mari, supoyo iso luwih jelas sampeyan iso ndelok ndek ngisor iki

--

--