ด้าย#
การแนะนำ#
เธรดช่วยให้หุ่นยนต์สามารถทำงานหลายอย่างพร้อมกันภายในโปรแกรมเดียวกันได้ ช่วยให้หุ่นยนต์สามารถทำงานหลายอย่างพร้อมกันได้ โดยสามารถดำเนินการต่างๆ ที่เป็นอิสระต่อกันได้ในเวลาเดียวกัน
ตัวสร้างคลาส#
1 — สร้างและเริ่มต้นเธรดใหม่พร้อมฟังก์ชัน
thread( void (* callback)(void) );
2 — สร้างและเริ่มต้นเธรดใหม่ด้วยฟังก์ชันที่รับอาร์กิวเมนต์
thread( int (* callback)(void *), void *arg );
ตัวทำลายคลาส#
Destroys the thread object and releases associated resources.
~thread();
พารามิเตอร์#
พารามิเตอร์ |
พิมพ์ |
คำอธิบาย |
|---|---|---|
|
|
ฟังก์ชันที่กำหนดไว้ก่อนหน้านี้ซึ่งจะทำงานในเธรดใหม่ |
|
|
ฟังก์ชันที่กำหนดไว้ก่อนหน้านี้ซึ่งรับอาร์กิวเมนต์เป็นตัวชี้แบบ void และจะทำงานในเธรดใหม่ |
|
|
พอยเตอร์ว่างเปล่าที่ถูกส่งไปยังฟังก์ชันเรียกกลับ (callback function) |
หมายเหตุ#
ฟังก์ชันจะต้องถูกกำหนดนิยามก่อนเสมอจึงจะถูกเรียกใช้
เมื่อคุณสร้างเธรด คุณสามารถตั้งชื่อเธรดนั้นเพื่อจัดการเธรดนั้นได้อย่างอิสระในโปรเจ็กต์ของคุณ
เมื่อเธรดหยุดทำงานโดยใช้
interrupt()จะไม่สามารถเริ่มต้นเธรดนั้นใหม่ได้อีก หากต้องการเรียกใช้งานอีกครั้ง คุณต้องสร้างเธรดใหม่
ตัวอย่าง#
กำหนดฟังก์ชัน (นอก
int main())// Drive forward while blinking screen void blinkScreen() { while (true) { Brain.Screen.clearScreen(red); wait(0.5, seconds); Brain.Screen.clearScreen(); wait(0.5, seconds); } }ลงทะเบียนเธรดภายใน
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code thread blinkScreenThread = thread(blinkScreen); Drivetrain.drive(forward); }
กำหนดฟังก์ชัน (นอก
int main())// Run multiple threads simultaneously // Turn right, blink screen at once void blinkScreen() { while (true) { Brain.Screen.clearScreen(red); wait(0.5, seconds); Brain.Screen.clearScreen(); wait(0.5, seconds); } } void turning() { Drivetrain.turn(right); }ลงทะเบียนเธรดภายใน
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code thread blinkScreenThread = thread(blinkScreen); thread turningThread = thread(turning); }
ฟังก์ชันสมาชิก#
คลาสเธรดประกอบด้วยฟังก์ชันสมาชิกต่อไปนี้:
interrupt– หยุดเธรดด้วยตนเอง มีประโยชน์สำหรับการหยุดพฤติกรรมพื้นหลัง
interrupt#
หยุดการทำงานของเธรดด้วยตนเอง ซึ่งมีประโยชน์เมื่อไม่ต้องการใช้งานงานนั้นอีกต่อไป หรือเมื่อโปรแกรมจำเป็นต้องรีเซ็ตเธรด
Available Functionsvoid interrupt();
ฟังก์ชันนี้ไม่มีพารามิเตอร์
Return Valuesฟังก์ชันนี้ไม่ส่งค่ากลับ
Notesเมื่อเธรดหยุดทำงานแล้ว จะไม่สามารถเริ่มต้นใหม่ได้อีก หากต้องการเรียกใช้งานอีกครั้ง คุณต้องสร้างเธรดใหม่โดยใช้เมธอด
threadวิธีนี้มีประโยชน์เมื่อไม่ต้องการใช้งานงานนั้นอีกต่อไป หรือเมื่อโปรแกรมจำเป็นต้องรีเซ็ตหรือกำหนดเธรดใหม่
กำหนดฟังก์ชัน (นอก
int main())// Loop through red and green void colorLoop() { while (true) { Brain.Screen.drawRectangle(0, 120, 480, 120, red); wait(0.5, seconds); Brain.Screen.drawRectangle(0, 120, 480, 120, green); wait(0.5, seconds); } } // Display the current heading void displayHeading() { Brain.Screen.setFont(prop60); while (true) { Brain.Screen.clearLine(1); Brain.Screen.setCursor(1, 1); Brain.Screen.print( "Heading: %.1f", DrivetrainInertial.heading(degrees) ); wait(50, msec); } }ลงทะเบียนเธรดภายใน
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Turn right, change screen color, and display heading at once thread colorThread = thread(colorLoop); thread displayHeadingThread = thread(displayHeading); Drivetrain.turn(right); wait(2, seconds); // Stop the flashing colors colorThread.interrupt(); }