Windows下pthread多线程使用(2):CreateThread

声明:此系列代码参考了“Professional Group Tec. Doc.07121901 Author-万一飞”的教程。

下述cmnheader.h的内容详见这里(第二段代码),另注意sleep()函数在Windows下应当引用WIndows.h的Sleep() 注意此处的"S"为大写字母

#include "cmnheader.h"

typedef struct
{
	int threadParm1;
	char threadParm2[124];
} threadParm_t;

void *theThread(void *param)
{
	pid_t tid = getpid();
	threadParm_t *p = (threadParm_t *)param;
	printf("Thread ID %.8x, Parameters: %d is the answer to \n\"%s\"\n",	tid, p->threadParm1, p->threadParm2);
	return NULL;
}
int main(int argc, char **argv)
{
	pthread_t thread;
	int rc = 0;
	threadParm_t *threadParm;
	printf("Enter Testcase - %s\n", argv[0]);
	threadParm = (threadParm_t *)malloc(sizeof(threadParm));
	threadParm->threadParm1 = 42;
	strcpy(threadParm->threadParm2, "Life, the Universe and Everything");
	printf("Create/start a thread with parameters\n");
	rc = pthread_create(&thread, NULL, theThread, threadParm);
	checkResults("pthread_create()\n", rc);
	printf("Wait for the thread to complete\n");
	rc = pthread_join(thread, NULL);
	checkResults("pthread_join()\n", rc);
	printf("Main completed\n");

	system("pause");
	return 0;
}

Windows下pthread多线程使用(2):CreateThread_第1张图片

你可能感兴趣的:(Parallel,CUDA,etc.)