Latest web development tutorials
×

C++ หลักสูตร

C++ หลักสูตร C++ แนะนำโดยย่อ C++ การตั้งค่าสภาพแวดล้อม C++ ไวยากรณ์พื้นฐาน C++ หมายเหตุ C++ ชนิดข้อมูล C++ ประเภทตัวแปร C++ ขอบเขตตัวแปร C++ คงที่ C++ ประเภทการปรับเปลี่ยน C++ ประเภทการจัดเก็บ C++ ผู้ประกอบการ C++ การไหลเวียน C++ ผู้พิพากษา C++ ฟังก์ชัน C++ ดิจิตอล C++ แถว C++ เชือก C++ ชี้ C++ อ้างอิง C++ วันที่และเวลา C++ เอาท์พุทหรือ Basic Input C++ โครงสร้างข้อมูล

C++ เชิงวัตถุ

C++ คลาสและวัตถุ C++ สืบทอด C++ ผู้ประกอบการมากเกินไปและการทำงานมากเกินไป C++ polymorphism C++ ข้อมูลที่เป็นนามธรรม C++ ข้อมูล encapsulation C++ อินเตอร์เฟซ(ระดับนามธรรม)

C++ การสอนที่ทันสมัย

C++ แฟ้มและสตรีม C++ การจัดการข้อยกเว้น C++ หน่วยความจำแบบไดนามิก C++ namespaces C++ แบบ C++ preprocessor C++ การประมวลผลสัญญาณ C++ multithreading C++ Web โครงการ

C++ กรุ

C++ STL หลักสูตร C++ ห้องสมุดมาตรฐาน C++ ทรัพยากรที่มีประโยชน์

c ++ Multithreading

Multithreading เป็นรูปแบบเฉพาะของแบบ multi-tasking, มัลติทาสกิ้งช่วยให้คอมพิวเตอร์ทำงานสองคนหรือมากกว่าโปรแกรม โดยทั่วไปทั้งสองประเภทของการทำงานแบบ multitasking: Process-based และด้ายที่ใช้

  • กระบวนการตามโปรแกรมมัลติทาสกิ้งจะถูกดำเนินการควบคู่กันไป
  • การประมวลผลด้ายตามมัลติทาสกิ้งคือการดำเนินการพร้อมกันส่วนโปรแกรมเดียวกัน

โปรแกรมแบบมัลติเธรดมีสองคนหรือมากกว่าส่วนที่สามารถทำงานพร้อมกัน โปรแกรมสำหรับแต่ละส่วนดังกล่าวเรียกว่าด้ายแต่ละหัวข้อกำหนดเส้นทางที่แยกต่างหากจากการดำเนินการ

C ++ ไม่รวมถึงการสนับสนุนในตัวสำหรับการใช้งานแบบมัลติเธรด แต่มันเป็นทั้งหมดขึ้นอยู่กับระบบปฏิบัติการเพื่อให้การทำงานนี้

กวดวิชานี้อนุมานว่าคุณกำลังใช้ระบบปฏิบัติการลินุกซ์, เราต้องการใช้ POSIX เขียนแบบมัลติเธรดโปรแกรม c ++ API POSIX กระทู้หรือ Pthreads สามารถให้บริการในหลายประเภทของระบบ Unix POSIX ใช้ได้เช่น FreeBSD, NetBSD, GNU / Linux, Mac OS X และ Solaris

สร้างหัวข้อ

โปรแกรมต่อไปนี้เราสามารถใช้มันเพื่อสร้างหัวข้อ POSIX:

#include <pthread.h>
pthread_create (thread, attr, start_routine, arg) 

นี่ pthread_create สร้างหัวข้อใหม่และทำให้มันปฏิบัติการต่อไปนี้เป็นคำอธิบายของพารามิเตอร์ A:

参数 描述
thread 指向线程标识符指针。
attr 一个不透明的属性对象,可以被用来设置线程属性。您可以指定线程属性对象,也可以使用默认值 NULL。
start_routine 线程运行函数起始地址,一旦线程被创建就会执行。
arg 运行函数的参数。它必须通过把引用作为指针强制转换为 void 类型进行传递。如果没有传递参数,则使用 NULL。

เมื่อด้ายถูกสร้างขึ้นประสบความสำเร็จในการทำงานกลับ 0 ถ้าค่าตอบแทนของ 0 แสดงว่าล้มเหลวในการสร้างหัวข้อ

ยุติด้าย

ใช้ขั้นตอนต่อไปนี้เราสามารถใช้มันเพื่อยุติหัวข้อ POSIX:

#include <pthread.h>
pthread_exit (status) 

นี่ pthread_exit ใช้อย่างชัดเจนออกจากด้ายภายใต้สถานการณ์ปกติ pthread_exit ฟังก์ชัน () จะถูกเรียกเมื่องานทำโดยไม่ต้องยังคงอยู่หลังจากด้าย

ถ้า main () ก่อนที่จะสิ้นสุดของด้ายที่ว่ามันถูกสร้างขึ้นและออกผ่าน pthread_exit () แล้วหัวข้ออื่น ๆ จะยังคงดำเนินการ มิฉะนั้นพวกเขาจะอยู่ในหลัก () ในตอนท้ายจะถูกยกเลิกโดยอัตโนมัติ

ตัวอย่าง

รหัสตัวอย่างง่ายๆต่อไปนี้ใช้ pthread_create () ฟังก์ชันสร้างห้าหัวข้อแต่ละเอาท์พุท "สวัสดี w3big!":

#include <iostream>
// 必须的头文件是
#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

// 线程的运行函数
void* say_hello(void* args)
{
    cout << "Hello w3big!" << endl;
}

int main()
{
    // 定义线程的 id 变量,多个变量使用数组
    pthread_t tids[NUM_THREADS];
    for(int i = 0; i < NUM_THREADS; ++i)
    {
        //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
        int ret = pthread_create(&tids[i], NULL, say_hello, NULL);
        if (ret != 0)
        {
           cout << "pthread_create error: error_code=" << ret << endl;
        }
    }
    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
    pthread_exit(NULL);
}

ใช้ห้องสมุด -lpthread รวบรวมโปรแกรมต่อไปนี้:

$ g++ test.cpp -lpthread -o test.o

ขณะนี้การดำเนินงานของโปรแกรมจะให้ผลลัพธ์ที่ต่อไปนี้:

$ ./test.o
Hello w3big!
Hello w3big!
Hello w3big!
Hello w3big!
Hello w3big!

รหัสตัวอย่างง่ายๆต่อไปนี้ใช้ pthread_create () ฟังก์ชันสร้างห้าหัวข้อและได้รับการพารามิเตอร์ที่เข้ามา แต่ละหัวข้อพิมพ์ "Hello w3big!" ข้อความพารามิเตอร์และผลที่ได้รับแล้วโทร pthread_exit () ยุติด้าย

//文件名:test.cpp

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

#define NUM_THREADS     5

void *PrintHello(void *threadid)
{  
   // 对传入的参数进行强制类型转换,由无类型指针变为整形数指针,然后再读取
   int tid = *((int*)threadid);
   cout << "Hello w3big! 线程 ID, " << tid << endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];
   int indexes[NUM_THREADS];// 用数组来保存i的值
   int rc;
   int i;
   for( i=0; i < NUM_THREADS; i++ ){      
      cout << "main() : 创建线程, " << i << endl;
      indexes[i] = i; //先保存i的值
      // 传入的时候必须强制转换为void* 类型,即无类型指针        
      rc = pthread_create(&threads[i], NULL, 
                          PrintHello, (void *)&(indexes[i]));
      if (rc){
         cout << "Error:无法创建线程," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

ตอนนี้รวบรวมและรันโปรแกรมจะให้ผลลัพธ์ที่ต่อไปนี้:

$ g++ test.cpp -lpthread -o test.o
$ ./test.o
main() : 创建线程, 0
main() : 创建线程, 1
main() : 创建线程, 2
main() : 创建线程, 3
main() : 创建线程, 4
Hello w3big! 线程 ID, 4
Hello w3big! 线程 ID, 3
Hello w3big! 线程 ID, 2
Hello w3big! 线程 ID, 1
Hello w3big! 线程 ID, 0

ผ่านพารามิเตอร์ด้าย

ตัวอย่างนี้แสดงให้เห็นถึงวิธีการที่จะผ่านโครงสร้างหลายพารามิเตอร์ คุณสามารถส่งผ่านประเภทของข้อมูลใด ๆ ในการเรียกกลับด้ายเพราะมันชี้ไปที่เป็นโมฆะตามที่แสดงในตัวอย่างต่อไปนี้:

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

#define NUM_THREADS     5

struct thread_data{
   int  thread_id;
   char *message;
};

void *PrintHello(void *threadarg)
{
   struct thread_data *my_data;

   my_data = (struct thread_data *) threadarg;

   cout << "Thread ID : " << my_data->thread_id ;
   cout << " Message : " << my_data->message << endl;

   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];
   struct thread_data td[NUM_THREADS];
   int rc;
   int i;

   for( i=0; i < NUM_THREADS; i++ ){
      cout <<"main() : creating thread, " << i << endl;
      td[i].thread_id = i;
      td[i].message = "This is message";
      rc = pthread_create(&threads[i], NULL,
                          PrintHello, (void *)&td[i]);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

เมื่อโค้ดข้างต้นจะรวบรวมและดำเนินการก็จะก่อให้เกิดผลลัพธ์ต่อไปนี้:

$ g++ -Wno-write-strings test.cpp -lpthread -o test.o
$ ./test.o
main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Thread ID : 3 Message : This is message
Thread ID : 2 Message : This is message
Thread ID : 0 Message : This is message
Thread ID : 1 Message : This is message
Thread ID : 4 Message : This is message

การเชื่อมต่อและแยกด้าย

เราสามารถใช้ต่อไปนี้สองฟังก์ชั่นการเชื่อมต่อหรือหัวข้อเฉพาะกิจการ:

pthread_join (threadid, status) 
pthread_detach (threadid) 

pthread_join () subroutine ขัดขวางโปรแกรมที่เรียกจนสิ้นสุดด้าย threadid ระบุ เมื่อคุณสร้างด้ายก็เป็นคุณลักษณะที่กำหนดไม่ว่าจะมีการเชื่อมต่อ (joinable) หรือถอดออกได้ (แฝด) กำหนดเพียงสร้างหัวข้อที่สามารถเชื่อมต่อสามารถเชื่อมต่อ ถ้าด้ายจะถูกสร้างขึ้นถูกกำหนดให้เป็นแยกกันไม่ออกก็ไม่สามารถที่จะเชื่อมต่อ

ตัวอย่างนี้แสดงให้เห็นถึงวิธีการใช้ pthread_join () ฟังก์ชันที่จะรอให้เสร็จสิ้นด้าย

#include <iostream>
#include <cstdlib>
#include <pthread.h>
#include <unistd.h>

using namespace std;

#define NUM_THREADS     5

void *wait(void *t)
{
   int i;
   long tid;

   tid = (long)t;

   sleep(1);
   cout << "Sleeping in thread " << endl;
   cout << "Thread with id : " << tid << "  ...exiting " << endl;
   pthread_exit(NULL);
}

int main ()
{
   int rc;
   int i;
   pthread_t threads[NUM_THREADS];
   pthread_attr_t attr;
   void *status;

   // 初始化并设置线程为可连接的(joinable)
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   for( i=0; i < NUM_THREADS; i++ ){
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, wait, (void *)i );
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }

   // 删除属性,并等待其他线程
   pthread_attr_destroy(&attr);
   for( i=0; i < NUM_THREADS; i++ ){
      rc = pthread_join(threads[i], &status);
      if (rc){
         cout << "Error:unable to join," << rc << endl;
         exit(-1);
      }
      cout << "Main: completed thread id :" << i ;
      cout << "  exiting with status :" << status << endl;
   }

   cout << "Main: program exiting." << endl;
   pthread_exit(NULL);
}

เมื่อโค้ดข้างต้นจะรวบรวมและดำเนินการก็จะก่อให้เกิดผลลัพธ์ต่อไปนี้:

main() : creating thread, 0
main() : creating thread, 1
main() : creating thread, 2
main() : creating thread, 3
main() : creating thread, 4
Sleeping in thread 
Thread with id : 4  ...exiting 
Sleeping in thread 
Thread with id : 3  ...exiting 
Sleeping in thread 
Thread with id : 2  ...exiting 
Sleeping in thread 
Thread with id : 1  ...exiting 
Sleeping in thread 
Thread with id : 0  ...exiting 
Main: completed thread id :0  exiting with status :0
Main: completed thread id :1  exiting with status :0
Main: completed thread id :2  exiting with status :0
Main: completed thread id :3  exiting with status :0
Main: completed thread id :4  exiting with status :0
Main: program exiting.