C语言简单的多线程

来源:互联网 发布:淘宝网外卖怎么开店 编辑:程序博客网 时间:2024/05/16 04:54

程序功能,当从键盘

输入1时程序一直输出“Hello World !”

 当输入0时 , 停止输出'Hello World !' 

再次输入1时 ,又开始输出“Hello World !”

这样循环下去。。。。。

知道输入-1时 ,程序退出!

 

///Thread.h文件

 

 

#include <Windows.h>

#include <stdio.h>

#include <stdlib.h>

 

int input = 1 ;

DWORD WINAPI output(LPVOID lpPrameter)

{

while(1)

{

printf('Hello World ! /n') ;

Sleep(1000) ;

}

return 0 ;

}

 

//////main.c文件

 

 

#include 'Thread.h'

int main()

{

int count_0 = 0 ;

HANDLE tOutput ;

tOutput = CreateThread(NULL , 0 , output , NULL , 0 , NULL) ;

while(1)

{

scanf('%d' ,&input) ;

if(input == 0)

{

SuspendThread(tOutput) ;

count_0 ++ ;

}

else if(input == 1)

{

while(count_0 && count_0 > 0)

{

ResumeThread(tOutput) ;

count_0 -- ;

}

}

else if(input == -1)

{

CloseHandle(tOutput) ;

return 0 ;

}

}

}