Skip to content

Instantly share code, notes, and snippets.

@bitdust
Created May 25, 2015 16:54
Show Gist options
  • Save bitdust/c3fc1060bd1c147234ab to your computer and use it in GitHub Desktop.
Save bitdust/c3fc1060bd1c147234ab to your computer and use it in GitHub Desktop.
关于线程的使用示例
/*
main.c
关于线程的使用示例
参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms684841.aspx
http://blog.csdn.net/morewindows/article/details/7421759
*/
#include<stdlib.h>
#include<stdio.h>
#include<windows.h>
static int File_exsist = 0;
static Stop_flag = 0;
int check();
int loop();
DWORD WINAPI run_loop(LPVOID lpParam);
void main()
{
printf("hello\n");
HANDLE handle = CreateThread(NULL, 0, run_loop, NULL, 0, NULL); //创建线程循环检查,线程入口函数为run_loop()
Sleep(5000); //先跑5秒
File_exsist = 1; //放置文件
Sleep(5000); //再跑5秒
Stop_flag = 1;
Sleep(1000);
return;
}
int check()
{
if (File_exsist == 0)
{
printf("文件不存在!\n");
return 0;
}
else
{
printf("文件存在!\n");
return 1;
}
}
loop()
{
do
{
if (check() == 1)
{
;// do something else
}
Sleep(1000); // 大写 S
} while (Stop_flag == 0);
return;
}
DWORD WINAPI run_loop(LPVOID lpParam)
{
printf("进入循环!!\n");
loop();
printf("退出循环!\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment