寿命; //制作线程,制定fn pthread_t threads[numthreads]; void*状态[numthreads]; pthread_mutex_init(&mymutex,NULL); int rc; for(int i=0;i,c++,multithreading,parallel-processing,pthreads,mutex,C++,Multithreading,Parallel Processing,Pthreads,Mutex" /> 寿命; //制作线程,制定fn pthread_t threads[numthreads]; void*状态[numthreads]; pthread_mutex_init(&mymutex,NULL); int rc; for(int i=0;i,c++,multithreading,parallel-processing,pthreads,mutex,C++,Multithreading,Parallel Processing,Pthreads,Mutex" />

Pthreads-将顺序程序转换为并行程序 我用C++模拟“考平的游戏”,其中一个2D矩阵表示板,0是一个空单元,1是活单元。我最初是按顺序编写的,并试图使其与pthreads并行。然而,由于某种原因,程序不再按预期运行。虽然它通过两个循环,并且似乎在一些“count++”上拾取,但它不会拾取所有的循环,因此单元的每一轮都被评估为只有一个或零个邻居(即使不是这样)。这会导致“结果”在设定的时间段后全部为零,因为每个细胞都会死亡而无法复制。我已经为此工作了几天,改变了不同的东西,但仍然无法理解。这是我的密码: #include <iostream> #include <vector> #include <pthread.h> #include <cstdlib> #include <functional> using namespace std; pthread_mutex_t mymutex; int lifetime, numthreads = 5; vector<vector<int> > board,result,pending; void *loader(void *tid){ long thid = long(tid); int n = board.size(); result = board; int count = 0; for(long i = 0; i < n; i ++){ if(i % numthreads != thid) continue; for(long j = 0; j < n ; j++){ if(i % numthreads != thid) continue; if(i+1 < n){ if(result[i+1][j] == 1) //checking each of the neighbor count++ ; if(j+1 < n){ if(result[i+1][j+1] == 1) count++; } if(j-1 >= 0){ if(result[i+1][j-1] == 1) count++; } } if(j-1 >= 0){ if(result[i][j-1] == 1) count++; } if(j+1 < n){ if(result[i][j+1] == 1) count++; } if(i-1 >= 0){ if(result[i-1][j] == 1) count++; if(j+1 < n){ if(result[i-1][j+1] == 1) count++; } if(j-1 >= 0){ if(result[i-1][j-1] == 1) count++; } } //determining next state if(count <= 1 || count >= 4){ //this utilizes the three main rules of game pthread_mutex_lock(&mymutex); pending[i][j] = 0; pthread_mutex_unlock(&mymutex); }else if(count == 3){ pthread_mutex_lock(&mymutex); pending[i][j] = 1; pthread_mutex_unlock(&mymutex); }else{ pthread_mutex_lock(&mymutex); pending[i][j] = result[i][j]; pthread_mutex_unlock(&mymutex); } count = 0; pthread_mutex_lock(&mymutex); result = pending; pthread_mutex_unlock(&mymutex); } } pthread_exit(NULL); return NULL; } int main(){ //setting up input int n; cin >> n; board.resize(n); result.resize(n); pending.resize(n); for(int i = 0; i < board.size(); i++){ board[i].resize(n); result[i].resize(n); pending[i].resize(n); } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> board[i][j]; } } cin >> lifetime; //making threads, enacting fn pthread_t threads[numthreads]; void *status[numthreads]; pthread_mutex_init(&mymutex,NULL); int rc; for(int i = 0; i < lifetime; i++){ for(int t = 0; t < numthreads; t++){ rc = pthread_create(&threads[t],NULL,loader,(void *)t); if(rc) exit(-1); } for(int t = 0; t < numthreads; t++){ rc = pthread_join(threads[t],&status[t]); if(rc) exit(-1); } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cout << result[i][j] << " "; } cout << endl; } } #包括 #包括 #包括 #包括 #包括 使用名称空间std; pthread_mutex_t mymutex; int生存期,numthreads=5; 向量板,结果,待定; void*加载器(void*tid){ 长时间=长时间(tid); int n=board.size(); 结果=板; 整数计数=0; 用于(长i=0;in; 板。调整大小(n); 结果:调整大小(n); 待定。调整大小(n); 对于(int i=0;i>寿命; //制作线程,制定fn pthread_t threads[numthreads]; void*状态[numthreads]; pthread_mutex_init(&mymutex,NULL); int rc; for(int i=0;i

Pthreads-将顺序程序转换为并行程序 我用C++模拟“考平的游戏”,其中一个2D矩阵表示板,0是一个空单元,1是活单元。我最初是按顺序编写的,并试图使其与pthreads并行。然而,由于某种原因,程序不再按预期运行。虽然它通过两个循环,并且似乎在一些“count++”上拾取,但它不会拾取所有的循环,因此单元的每一轮都被评估为只有一个或零个邻居(即使不是这样)。这会导致“结果”在设定的时间段后全部为零,因为每个细胞都会死亡而无法复制。我已经为此工作了几天,改变了不同的东西,但仍然无法理解。这是我的密码: #include <iostream> #include <vector> #include <pthread.h> #include <cstdlib> #include <functional> using namespace std; pthread_mutex_t mymutex; int lifetime, numthreads = 5; vector<vector<int> > board,result,pending; void *loader(void *tid){ long thid = long(tid); int n = board.size(); result = board; int count = 0; for(long i = 0; i < n; i ++){ if(i % numthreads != thid) continue; for(long j = 0; j < n ; j++){ if(i % numthreads != thid) continue; if(i+1 < n){ if(result[i+1][j] == 1) //checking each of the neighbor count++ ; if(j+1 < n){ if(result[i+1][j+1] == 1) count++; } if(j-1 >= 0){ if(result[i+1][j-1] == 1) count++; } } if(j-1 >= 0){ if(result[i][j-1] == 1) count++; } if(j+1 < n){ if(result[i][j+1] == 1) count++; } if(i-1 >= 0){ if(result[i-1][j] == 1) count++; if(j+1 < n){ if(result[i-1][j+1] == 1) count++; } if(j-1 >= 0){ if(result[i-1][j-1] == 1) count++; } } //determining next state if(count <= 1 || count >= 4){ //this utilizes the three main rules of game pthread_mutex_lock(&mymutex); pending[i][j] = 0; pthread_mutex_unlock(&mymutex); }else if(count == 3){ pthread_mutex_lock(&mymutex); pending[i][j] = 1; pthread_mutex_unlock(&mymutex); }else{ pthread_mutex_lock(&mymutex); pending[i][j] = result[i][j]; pthread_mutex_unlock(&mymutex); } count = 0; pthread_mutex_lock(&mymutex); result = pending; pthread_mutex_unlock(&mymutex); } } pthread_exit(NULL); return NULL; } int main(){ //setting up input int n; cin >> n; board.resize(n); result.resize(n); pending.resize(n); for(int i = 0; i < board.size(); i++){ board[i].resize(n); result[i].resize(n); pending[i].resize(n); } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> board[i][j]; } } cin >> lifetime; //making threads, enacting fn pthread_t threads[numthreads]; void *status[numthreads]; pthread_mutex_init(&mymutex,NULL); int rc; for(int i = 0; i < lifetime; i++){ for(int t = 0; t < numthreads; t++){ rc = pthread_create(&threads[t],NULL,loader,(void *)t); if(rc) exit(-1); } for(int t = 0; t < numthreads; t++){ rc = pthread_join(threads[t],&status[t]); if(rc) exit(-1); } } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cout << result[i][j] << " "; } cout << endl; } } #包括 #包括 #包括 #包括 #包括 使用名称空间std; pthread_mutex_t mymutex; int生存期,numthreads=5; 向量板,结果,待定; void*加载器(void*tid){ 长时间=长时间(tid); int n=board.size(); 结果=板; 整数计数=0; 用于(长i=0;in; 板。调整大小(n); 结果:调整大小(n); 待定。调整大小(n); 对于(int i=0;i>寿命; //制作线程,制定fn pthread_t threads[numthreads]; void*状态[numthreads]; pthread_mutex_init(&mymutex,NULL); int rc; for(int i=0;i,c++,multithreading,parallel-processing,pthreads,mutex,C++,Multithreading,Parallel Processing,Pthreads,Mutex,我可以立即看到三个正确性问题 首先,每个线程设置result=board,没有锁定,而且您甚至不想在每个循环中都这样做。只需让主线程执行一次即可-后续迭代使用result作为输入 其次,这些嵌套循环: for(long i = 0; i < n; i ++){ if(i % numthreads != thid) continue; for(long j = 0; j < n ; j++){ if(i % numthreads != t

我可以立即看到三个正确性问题

首先,每个线程设置
result=board
,没有锁定,而且您甚至不想在每个循环中都这样做。只需让主线程执行一次即可-后续迭代使用
result
作为输入

其次,这些嵌套循环:

for(long i = 0; i < n; i ++){
    if(i % numthreads != thid)
        continue;
    for(long j = 0; j < n ; j++){
        if(i % numthreads != thid)
            continue;
        /* ... */
第三,在处理每个单元格后,每个线程都在更新
结果
——这意味着一些单元格正在根据其他单元格的部分结果计算其结果,而这甚至不会以确定的顺序发生,因此结果将不稳定

您可以通过删除
loader()
函数中更新
result
的代码,并将其放入
main()
中的
life
循环中来修复此问题,这样游戏的每一步只会发生一次


还有一个性能问题-你在游戏的每一步都要启动和停止一堆线程。这根本不会有好的表现-启动和停止线程是一个很重要的操作。一旦你让它工作起来,你可以通过让每个线程执行
生命周期
循环并一直运行来解决这个问题。你可以在每个步骤中使用
pthread\u barrier\u wait()

loader
内部进行同步(线程进程)当您在其中没有竞争条件的保护时,是否有任何代码访问或更新正在被其他线程修改或读取的单元格?此外,编译器可以假设其他线程没有修改单元格的内容,并优化重复读取/更新。主题外:您是否考虑过使用
std::thread
及其朋友而不是pthreads?
for(long i = 0; i < n; i ++){
    if(i % numthreads != thid)
        continue;
    for(long j = 0; j < n ; j++){
        /* ... */