window多线程编程

 

======================================== 方法一=====================================================================================

#include "stdafx.h"
#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <process.h>  // 包含这个头文件就行 

 

 

========================================方法二======================================================================================

移植posix库:

移植官网:

ftp://sourceware.org/pub/pthreads-win32

 

 

#include "stdafx.h"
#include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <pthread.h>


void* Function_t(void* Param);
void* myFunction_t(void*MyParm);


int main(int argc, _TCHAR* argv[])
{
	pthread_t pid;
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	pthread_create(&pid, &attr, Function_t, NULL);
	pthread_create(&pid, &attr, myFunction_t, NULL);
	printf("====\n");
	getchar();
	pthread_attr_destroy(&attr);

	CreateThread
	return 0;
}
void* Function_t(void* Param)
{
	printf("Thread Starts.\n");
	pthread_t myid = pthread_self();

	while (1)
	{
		printf("thread_one tid is %d\n", myid.x);
		fflush(stdout);
		Sleep(1000);
	}

	return NULL;
}

void* myFunction_t(void*MyParm)
{
	printf("Thread Starts.\n");
	pthread_t myid = pthread_self();

	while (1)
	{
		printf(" pthread my  pthread is  %d\n", myid.x);
		printf("my Function_t\n");
		Sleep(1000);

	}

	return NULL;
}

  

 

posted @ 2020-04-04 14:26  卷哭你  阅读(304)  评论(0编辑  收藏  举报