2017-06-02 72 views
-1

我的C++非常基础,这是我第一次尝试多线程代码。 所以我的数据集相当大我认为我可以通过分离一些功能来缩短时间。在伪代码中描述的是我想要的一般概念。在C++中运行两个进程并等待它们完成

int main(){ 

    process1(); 
    process2(); 

} 

process1(){ 
    base value for recursion 
    Builds a forward array 
    Once complete - run function combination(); 
    Recursive call 
} 

process2(){ 
    base value for recursion 
    Builds backwards array 
    Once complete - run function combination(); 
    Recursive call 
} 

combination(){ 
    when both functions are complete. 
    if functions return null then the array is complete 
    else add results into a new array. 
} 

递归调用是我可能在这里遇到的难题。是否有一种简单的方法来实现两个流程必须完成第一次迭代才能运行组合并再次通过它的地方?

+0

哦,这个代码希望SIMD这么难.... – iehrlich

回答

4

这是不运行两个processes但运行两个线程

一个过程几乎根据定义,它自己的virtual address space。因此,如果没有特定步骤,两个不同的进程不能共享内存(在Linux上,请参阅mmap(2) & shm_overview(7))。一个进程可以有几个线程共享相同的虚拟地址空间,每个线程在该虚拟地址空间中都有自己的call stack

pthread tutorial讲授P-线程有趣的概念(你可以很容易地适应C++11 threads

你可能想了解更多关于condition variablesmutexes,因为你应该关心synchronization

+0

是的,我现在正在阅读,比我想象的要复杂得多。感谢您指点我正确的方向 –

3

有没有简单的方法来实现两个进程必须完成他们的第一次迭代运行组合,然后再次通过它?

一个信号。你想要一个线程等待一个信号。也叫condition variables。 (Posix

有很多多线程资源和它的技术,你应该肯定会学习(多核心标准这些天)。

Strategized Locking, Thread-safe Interface, and scoped Locking

Half-Sync/Half-ASync(描述并发队列)

Atomicity Policies using Design Patterns(描述生产者/消费者,读/写策略)。

相关问题