> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/c-c++-for-embedded-programming/high-performance-programing/multi-task-and-multi-thread/multi-threading-example.md).

# Multi-threading example

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

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

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

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

<figure><img src="https://paper-attachments.dropbox.com/s_C76828DECC2A10B4AD7FC83CF4CD9E66C63F4032166BD478AEB40FE30C836762_1662973364763_image.png" alt=""><figcaption><p>ภาพผลการรันโปรแกรม</p></figcaption></figure>

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

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

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

```css
#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  &#x20;

<figure><img src="https://paper-attachments.dropbox.com/s_C76828DECC2A10B4AD7FC83CF4CD9E66C63F4032166BD478AEB40FE30C836762_1662974352581_image.png" alt=""><figcaption><p>ผลการรันโปรแกรม</p></figcaption></figure>

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

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

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

```css
#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 ก็เปลี่ยนแลงเช่นกันแม้จะไม่มีการส่งค่ากลับ

<figure><img src="https://paper-attachments.dropbox.com/s_C76828DECC2A10B4AD7FC83CF4CD9E66C63F4032166BD478AEB40FE30C836762_1662976659876_image.png" alt=""><figcaption><p>ผลการรันโปรแกรม</p></figcaption></figure>

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

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

```css
#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 แต่ละเส้นทำงานต่างกัน เมื่อทำการรันโปรแกรมจะได้ดังรูป

<figure><img src="https://paper-attachments.dropbox.com/s_C76828DECC2A10B4AD7FC83CF4CD9E66C63F4032166BD478AEB40FE30C836762_1662977937993_image.png" alt=""><figcaption><p>ผลการรันโปรแกรม</p></figcaption></figure>

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.aic-eec.com/c-c++-for-embedded-programming/high-performance-programing/multi-task-and-multi-thread/multi-threading-example.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
