gpt4 book ai didi

C++ 多线程

转载 作者:行者123 更新时间:2023-11-30 02:11:51 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
c++ multithread

我用c++实现了一个线程类。代码如下。我初始化了两个对象,希望它会启动两个线程(我使用 pthread_self() 来查看线程 ID)。但是结果显示主线程旁边只有一个线程。我有点糊涂了……

class Thread {
public:
int mask;
pthread_t thread;

Thread( int );
void start();
static void * EntryPoint (void *);
void Run();
};

Thread::Thread( int a) {
mask =a;
}

void Thread::Run() {

cout<<"thread begin to run" <<endl;
cout <<" Thread Id is: "<< pthread_self() << endl; // the same thread Id.
}

void * Thread::EntryPoint(void * pthis) {
cout << "entry" <<endl;
Thread *pt = (Thread *) pthis;
pt->Run();
}

void Thread::start() {

pthread_create(&thread, NULL, EntryPoint, (void *)ThreadId );
pthread_join(thread, NULL);
}

int main() {
int input_array[8]={3,1,2,5,6,8,7,4};
Thread t1(1);
Thread t2(2);
t1.start();
t2.start()
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com