Multi-threading example

การเขียนโปรแกรมแบบ single process (ไม่มี thread)

เป็นการเขียนโปรแกรมแบบดั้งเดิมโดยให้โปรแกรมทำงานตั้งแต่บนลงล่าง โดยการใช้หน่วยประมวลผลหน่วยเดียวในการแบ่งทรัพยากรณ์การคำนวณทั้งหมด

#include <iostream>
#include <vector>
#include <thread>
using namespace std;
int main(){
    while(1)
        cout << "+" ;
    while(1)
        cout << "-" ;
    return 0;
}

เมื่อทำการเขียนโปรแกรมข้างต้นสำเร็จ ให้ทดลองรันโปรแกรมที่เขียนมา และทำการสังเกตุผลที่เกิดขึ้น

จากผลการรันจะเห็นได้ว่า โปรแกรมที่เราเขียนนั้นจะทำการแสดงผลเพียงแค่เครื่องหมาย “+” เท่านั้น เพราะเงื่อนไข loop while ในโปรแกรมนั้นมีค่าเป็นจริงตลอด ทำให้โปรแกรมที่เราอยากให้รันได้ใน loop ที่สองนั้นไม่ทำงาน

การเขียนโปรแกรมแบบ single thread

จะมีความใกล้เคียงกับการเขียนโปรแกรมแบบ single process แต่จะเป็นการสร้าง thread ขึ้นมา thread เดียวเพื่อแยกการคำนวณออกจาก main thread

#include <iostream>
#include <thread>
using namespace std;
void threadFn(){
    cout << " I am inside a thread function " << endl;
    for(int i = 0; i < 500; i++)
        cout << "+" ;
}
int main(){
    thread thread1(threadFn); //create thread and made it run function
    thread1.join(); //wait for thread to finish
    cout << "Output main " << endl;
    return 0;
}

จากโปรแกรมด้านบน จะเห็นได้ว่าเราสร้าง thread ขึ้นมาเพื่อไปเรียกฟังก์ชั่น threadFn

จากผลการรันจะเห็นได้ว่าโปรแกรมรันเหมือนโปรแกรมทั่วไปที่เราเขียน แต่ถ้าเราลองสลับบรรทัดที่ 11 กับ 12 เราจะเห็นได้ว่า คำว่า “Output main” จะไม่ได้แสดงในตำแหน่งเดิม ให้ผู้ทดลอง ลองสลับและทำการรันขึ้นมาอีกครั้ง สังเกตุการเปลี่ยนแปลง

การเขียนโปรแกรมแบบ single thread แบบรับ argument

การเขียนโปรแกรมแบบนี้จะมีความใกล้เคียงกับการเขียนโปรแกรมแบบ single thread แต่จะเป็นการสร้าง thread ขึ้นมาโดยที่ตัว thread นั้นจะรับค่าที่ส่งจาก main thread ไปประมวลผลต่อใน thread

#include <iostream>
#include <thread>
using namespace std;
void threadFn(int & value){  // past by reference
    cout << " I am inside a thread function " << endl;
    cout << "my Value = " << value<< endl;
    value += 10;
}
int main(){
    int localvalue = 100;
    thread thread1{threadFn, ref(localvalue)};
    thread1.join();
    cout<< "value in main thread = " << localvalue <<endl;
    return 0;
}

จากโปรแกรมด้านบน จะเป็นการสร้าง thread และทำการส่งค่าไปให้ thread แระมวลผลโดยค่าที่ส่งไป จะเป็นในลักาณะ pointer เนื่องจากค่าที่อยู่ใน thread เปลี่ยนแปลงค่าที่อยู่ใน main thread ก็เปลี่ยนแลงเช่นกันแม้จะไม่มีการส่งค่ากลับ

การเขียนโปรแกรมแบบ Multi thread

การเขียนแบบนี้จะเป็นการสร้าง thread ขึ้นมามากกว่า 1 thread ในการช่วบประมวลผลยิ่งจำนวน thread เยอะการประมวลผลก็ใช้เวลาน้อยลงด้วยเช่นกัน แต่อย่างไรก็ตามเมื่อ thread มีจำนวนมากขึ้นแต่จำนวน memory ที่มีไม่ได้มีมากขึ้นตามอาจทำให้การคำนวณเกิดการผิดพลาดได้ถ้ามี thread ใด thread หนึ่งไปใช้ memory ที่เดียวกัน

#include <iostream>
#include <vector>
#include <thread>
using namespace std;
void function1(){
    while(1)
        cout << "+" ;
}
void function2(){
    while(1)
        cout << "-" ;
}
int main(){
    thread worker1(function1); //create thread1
    thread worker2(function2); //create thread2
    worker1.join(); //wait thread1 to end
    worker2.join(); //wait thread2 to end
    return 0;
}

จากโปรแกรมด้านบนจะเป็นการสร้าง thread ขึ้นมา 2 thread แต่ละเส้นทำงานต่างกัน เมื่อทำการรันโปรแกรมจะได้ดังรูป

จะเห็นได้ว่าเมื่อเราสั่งโปรแกรมให้รันใน thread ต่างกันโปรแกรมเราจะแสดงทั้งลูปที่แสดงผลค่า “+” และค่า “-” แม้ทั้งสอง loop จะไม่มี loop ไหนจบลงเลยก็ตาม

Last updated

Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University