1
這是我的代碼的縮寫版本。我應該澄清,「initializeGrid()」的行爲是正確的,所以爲了簡潔起見,我沒有包括它。我試圖讓「threadFunction()」在args中打印值,但它不起作用。請幫忙!爲什麼我的線程不能正常產卵? C
#include <pthread.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int gridSize = 12;
typedef struct threadParameter {
float * grid;
int top;
int bot;
}threadParameter;
void* threadFunction(void* args) {
threadParameter *param = (threadParameter*) args;
printf("%d, %d", param->bot, param->top);
return 0;
}
int main(int argc, char *argv[]) {
int numProcs = 4;
pthread_t * threadsArr = malloc(numProcs * sizeof(pthread_t));
float mainGrid[gridSize][gridSize];
memset(mainGrid, 0, gridSize*gridSize * sizeof(float));
initializeGrid(1.0, 2.0, 3.0, 4.0, mainGrid);
threadParameter t = {&mainGrid[0][0], 0, gridSize};
int ret = pthread_create(&threadsArr[0], 0, threadFunction, (void*)&t);
}
非常感謝你,@Andrew漢勒!這正是問題所在。感謝您的快速和詳細的回覆! – SupposedlySleeping
解決該問題的另一種方法是執行'pthread_exit'而不是'main'的隱式返回。這使進程保持活動狀態,並讓所有其他線程按其選擇的方式終止。 –