在mergesort中打印开始和结束线程

时间:2015-02-12 07:10:26

标签: c++ multithreading mergesort

我有一个通用的mergesort代码,可以在C ++中对8个整数进行排序。分拣部分工作正常。但是,我需要实现线程。具体来说,我试图做以下事情: - 在线程开始和结束时打印出来 - 在线程完成的情况下,我需要打印出已排序的数字 所需输出的示例如下:

Thread 1 started
Thread 2 started
Thread 3 started
Thread 4 started
Thread 6 started
Thread 7 started
Thread 9 started
Thread 9 finished: 7,
Thread 8 started
Thread 8 finished: 3,
Thread 11 started
Thread 11 finished: 1,
........
........
........
Thread 1 finished: 1, 2, 3, 4, 5, 6, 7

我需要帮助实现这一点。我将我的主题定义如下:

thread * t1 = new thread;
thread * t2 = new thread;
*t1 = thread(&mergesort,array,low,mid);
*t2 = thread(&mergesort,array,mid+1,high);
t1->join();
t2->join();

我使用以下内容打印线程:

cout<<"Thread "<<this_thread::get_id()<<" started"<<endl;

在int main()中,我放置了代码来打印有序数字。我的问题是我从来没有得到所需的输出。我相信我的主要错误是将线程的cout函数放在错误的位置。任何帮助将不胜感激。

0 个答案:

没有答案