我有2个文件(.c),一个用于测试(test.c),在test.c中我放了程序的main函数,我还有另一个文件包含在test.c中,我创建了线程,但是当我运行test.c线程时,不要运行。但是如果我在main函数中定义线程它是正确的。现在我想知道我们可以在其他头文件中使用线程吗?
这是我的主要代码:
include stdlib.h
include stdio.h
include time.h
include math.h
include "msort_pth.h"
//Dynamically allocate an array of size N in heap
void fillArray ( int** values, unsigned int N )
{
srand(time(0));
unsigned int i;
for (i=0; i<N; i++)
(*values)[i] = rand() - (RAND_MAX/2);
}
//Check to see if the sorted array is really sorted!
void checkArray ( const int* values, unsigned int N, const int* sorted )
{
unsigned int i;
for (i=0; i <= N-2; i++)
if (sorted[i]>sorted[i+1])
{
printf("ERROR i=%d %d %d", i, sorted[i], sorted[i+1]);
return;
}
printf("OK\n");
}
int main ( int argc, char* argv[] )
{
if ( argc != 3 ){
printf("Please enter: \n ./a.out M K\n");
exit( 1 );
}
//input array
unsigned int K;
unsigned int N;
//Fill in the input array with random data
K = atoi(argv[2]);
N = pow( 2, atoi(argv[1]) );
int* values = (int*) malloc ( sizeof(int) * N );
fillArray ( &values, N );
//Sorted array
int* sorted = (int*) malloc ( sizeof(int) * N );
//Sort the input array and report its runtime
clock_t t1 = clock();
mergeSortParallel ( values, N, K, sorted );
clock_t t2 = clock();
printf("Runtime = %g ms \n", (t2-t1)/1000.0);
//Check for correctness
checkArray ( values, N, sorted );
return 0;
}
这是我的帖子文件: (删除了一些不必要的功能)
include "msort_pth.h"
include pthread.h
include stdio.h
include stdlib.h
define BLOCKSIZE 1
int* globalValues;
int* globalSorted;
int* globalTemp;
int globalN;
int globalK;
int thread_count;
void merge(int A[], int B[], int N, int* sorted);
void My_BubbleSort(const int* values, int Size, int* sorted);
void My_Sort(const int* values, unsigned int N, int* sorted, int BLOCK_SIZE);
void mergeSort(const int* values, unsigned int N, int* sorted);
void* threadFunc(void* rank);
void mergeSortParallel (const int* values, unsigned int N, unsigned int K, int* sorted)
{
globalK=K;
globalN=N;
globalValues=(int*)malloc(sizeof(int)*N);
globalSorted=(int*)malloc(sizeof(int)*N);
globalTemp=(int*)malloc(sizeof(int)*N);
int i=0;
for(i=0;i<N;i++)
{
globalValues[i]=values[i];
}
pthread_t* thread_handles;
long thread=0;
thread_handles=malloc(thread_count*sizeof(pthread_t));
for(thread=0;thread<thread_count;thread++)
{
pthread_create(&thread_handles[thread],NULL,threadFunc,(void*)thread);
}
for(thread=0;thread<thread_count;thread++)
{
pthread_join(thread_handles[thread],NULL);
}
free(thread_handles);
printf("GLOBAL VALUES:\n");
for(i=0;i<N;i++)
{
printf("%d,",values[i]);
}
printf("\n");
printf("\n");
printf("GLOBAL Sorted:\n");
for(i=0;i<N;i++)
{
printf("%d,",globalSorted[i]);
}
/*
int j=0;
int length=0;
for(j=0;j<globalK;j++)
{
int p=globalK-j;
int level=pow(2,p);
length=globalN/level;
for(i=0;i<level;i++)
{
merge(globalSorted+i*length,globalSorted+(i+1)*length,length,globalTemp);
}
for(i=0;i<2*length;i++)
{
}
}
*/
}
void* threadFunc(void* rank)
{
long myrank=(long)rank;
printf("\n MY rank is: %ld \n",myrank);
int start=myrank*(globalN/thread_count);
int length=globalN/thread_count;
mergeSort(globalValues+start,length,globalSorted+start);
return NULL;
}
答案 0 :(得分:0)
如果我正确理解了你的问题,你想在其他文件(test.c)中定义一个线程并在主文件(main.c)中运行它。在这种情况下,你想做&#34; test.h&#34;在main.c和test.c中包含test.h。
#include "test.h"
之后,在test.c中定义函数,并在test.h中声明它们。
test.h应该有#ifndef。
并将其包含在Makefile中:
debug:clean
$(CC) $(CFLAGS) $(CDEBUGFLAGS) -g -o main main.c test.c