关于C语言中的sleep的疑问,新手入门

piaopiao11 2016-08-29 03:29:57
sleep方法s是大写还是小写没有区别吧
int main(void) {
puts("!!!Hello World!!!\n"); /* prints !!!Hello World!!! */
Sleep(3000);
puts("asdw\n");
return EXIT_SUCCESS;
}
我以为这段代码是先打印hello world 隔三秒后再打印asdw但是他是再延时三秒后一起打印出来,想请教下各位sleep方法的用法。以及C创建线程的方法
DWORD WINAPI ThreadProc1( LPVOID lpParam )
{
int i=0;
for(;i<5;i++)
{
printf("hello,this thread 1 ...\n");
}
}
void main()
{
int i=0;
//创建线程1
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc1, // thread function
NULL, // argument to thread function
0, // use default creation flags
NULL);
}
这样不能创建线程吗,打印出不来
...全文
705 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
piaopiao11 2016-08-29
  • 打赏
  • 举报
回复
引用 7 楼 qq423399099 的回复:
主线程结束时会释放掉和其相关的所有资源(包括子线程) 你可以用类似WaitForSingleObject这样的函数等待子线程跑完再执行之后的代码
//============================================================================ // Name : cplus.cpp // Author : deepocean // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <windows.h> using namespace std; DWORD WINAPI ThreadFun(PVOID par) { cout<<"线程方法正在执行..."<<endl; Sleep(3000); return 0; } int main() { DWORD ThreadID; HANDLE hThread=CreateThread(NULL,0,ThreadFun,(void*)0,0,NULL); while(TRUE) { //这里等待时间是设置的1秒钟,可以设置为INFINITE(就不需要外面的死循环了) DWORD r= WaitForSingleObject(hThread,1000); if(r==WAIT_TIMEOUT) { cout<<"WAIT_TIMEOUT"<<endl;//输出2-3次 continue; } else if(r==WAIT_OBJECT_0) { cout<<"WAIT_OBJECT_0"<<endl;//线程方法睡眠3秒以后输出 break; } else { cout<<"hello"<<endl; continue; } } CloseHandle(hThread); system("PAUSE"); return 0; } 非常感谢
赵4老师 2016-08-29
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
    #include <process.h>
    #define  MYVOID             void
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
    #define  MYVOID             void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr[16000];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
    Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
    usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;


    if (NULL==pszFmt||0==pszFmt[0]) return;
    if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr[ARRSIZE(logstr)-1]=0;
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
            flog=fopen(logfilename1,"a");
            if (NULL==flog) return;
        }
        fclose(flog);
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int No_Loop=0;
MYVOID testThread(void *pcn) {
    int n,i;

    n=(int)pcn;
    i=0;
    while (1) {
        sleep_ms(1000);
        Log("in testThread %d:i==%ds\n",n,++i);
        if (i>=5) No_Loop=1;
    }
}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
    pthread_t threads[1];
    int threadsN;
    int rc;
#endif
    Log("=========BEGIN==================\n");
#ifdef WIN32
    _beginthread((void(__cdecl *)(void *))testThread,0,(void *)1);
#else
    threadsN=0;
    rc=pthread_create(&(threads[threadsN++]),NULL,testThread,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif
    i=0;
    while (1) {
        sleep_ms(100);
        Log("in main:i==%d\n",++i);
        if (No_Loop==1) break;//
    }
    Log("=========END====================\n");
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//2012-06-14 16:27:21.500 =========BEGIN==================
//2012-06-14 16:27:21.609 in main:i==1
//2012-06-14 16:27:21.718 in main:i==2
//2012-06-14 16:27:21.828 in main:i==3
//2012-06-14 16:27:21.937 in main:i==4
//2012-06-14 16:27:22.046 in main:i==5
//2012-06-14 16:27:22.156 in main:i==6
//2012-06-14 16:27:22.265 in main:i==7
//2012-06-14 16:27:22.375 in main:i==8
//2012-06-14 16:27:22.484 in main:i==9
//2012-06-14 16:27:22.500 in testThread 1:i==1s
//2012-06-14 16:27:22.593 in main:i==10
//2012-06-14 16:27:22.703 in main:i==11
//2012-06-14 16:27:22.812 in main:i==12
//2012-06-14 16:27:22.921 in main:i==13
//2012-06-14 16:27:23.031 in main:i==14
//2012-06-14 16:27:23.140 in main:i==15
//2012-06-14 16:27:23.250 in main:i==16
//2012-06-14 16:27:23.359 in main:i==17
//2012-06-14 16:27:23.468 in main:i==18
//2012-06-14 16:27:23.500 in testThread 1:i==2s
//2012-06-14 16:27:23.578 in main:i==19
//2012-06-14 16:27:23.687 in main:i==20
//2012-06-14 16:27:23.796 in main:i==21
//2012-06-14 16:27:23.906 in main:i==22
//2012-06-14 16:27:24.015 in main:i==23
//2012-06-14 16:27:24.125 in main:i==24
//2012-06-14 16:27:24.234 in main:i==25
//2012-06-14 16:27:24.343 in main:i==26
//2012-06-14 16:27:24.453 in main:i==27
//2012-06-14 16:27:24.500 in testThread 1:i==3s
//2012-06-14 16:27:24.562 in main:i==28
//2012-06-14 16:27:24.671 in main:i==29
//2012-06-14 16:27:24.781 in main:i==30
//2012-06-14 16:27:24.890 in main:i==31
//2012-06-14 16:27:25.000 in main:i==32
//2012-06-14 16:27:25.109 in main:i==33
//2012-06-14 16:27:25.218 in main:i==34
//2012-06-14 16:27:25.328 in main:i==35
//2012-06-14 16:27:25.437 in main:i==36
//2012-06-14 16:27:25.500 in testThread 1:i==4s
//2012-06-14 16:27:25.546 in main:i==37
//2012-06-14 16:27:25.656 in main:i==38
//2012-06-14 16:27:25.765 in main:i==39
//2012-06-14 16:27:25.875 in main:i==40
//2012-06-14 16:27:25.984 in main:i==41
//2012-06-14 16:27:26.093 in main:i==42
//2012-06-14 16:27:26.203 in main:i==43
//2012-06-14 16:27:26.312 in main:i==44
//2012-06-14 16:27:26.421 in main:i==45
//2012-06-14 16:27:26.500 in testThread 1:i==5s
//2012-06-14 16:27:26.531 in main:i==46
//2012-06-14 16:27:26.531 =========END====================
小灸舞 版主 2016-08-29
  • 打赏
  • 举报
回复
主线程结束时会释放掉和其相关的所有资源(包括子线程)
你可以用类似WaitForSingleObject这样的函数等待子线程跑完再执行之后的代码
piaopiao11 2016-08-29
  • 打赏
  • 举报
回复
引用 4 楼 qq423399099 的回复:
1.Windows中,Sleep()里面的单位,是以毫秒为单位,所以如果想让函数滞留1秒的话,应该写成:
Sleep(1000);
2.在CreateThread后加上getchar() PS:不推荐使用eclipse来编写C/C++代码
这个我是这样写的 就是出不来 void main() { int i=0; //创建线程1 CreateThread( NULL, // default security attributes 0, // use default stack size ThreadProc1, // thread function NULL, // argument to thread function 0, // use default creation flags NULL); getchar(); }
piaopiao11 2016-08-29
  • 打赏
  • 举报
回复
这个循环为什么在停止主线程时 才能打印出来
小灸舞 版主 2016-08-29
  • 打赏
  • 举报
回复
1.Windows中,Sleep()里面的单位,是以毫秒为单位,所以如果想让函数滞留1秒的话,应该写成:
Sleep(1000);

2.在CreateThread后加上getchar()

PS:不推荐使用eclipse来编写C/C++代码
piaopiao11 2016-08-29
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
1. C语言标准库中没有sleep这个函数,它实际上是一个系统API函数。 函数功能: 执行挂起一段时间 在VC中使用带上头文件,而且全称为Sleep(),第一个字母需要大写。
#include <windows.h> //需要添加头文件
Sleep(unisgned long); //函数声明
Windows中,Sleep()里面的单位,是以毫秒为单位,所以如果想让函数滞留1秒的话,应该写成: 在gcc中,使用的头文件因gcc版本的不同而不同,一般来说,linux系统需要添加的头文件为: #include <unistd.h> 在Linux下,sleep()里面的单位是秒,而不是毫秒。 2.你打印不出来,是因为你的主函数(主线程)已经结束了,你在主函数里加个getchar();让它不要结束就行了
我不是用VC编辑 我是用eclipse,sleep应该写成什么样的,没看见,能麻烦你再写下不,线程这个我再主线程里面加了个while(1)循环 还是打印不出来
赵4老师 2016-08-29
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
小灸舞 版主 2016-08-29
  • 打赏
  • 举报
回复
1.
C语言标准库中没有sleep这个函数,它实际上是一个系统API函数。
函数功能: 执行挂起一段时间
在VC中使用带上头文件,而且全称为Sleep(),第一个字母需要大写。
#include <windows.h> //需要添加头文件
Sleep(unisgned long); //函数声明

Windows中,Sleep()里面的单位,是以毫秒为单位,所以如果想让函数滞留1秒的话,应该写成:

在gcc中,使用的头文件因gcc版本的不同而不同,一般来说,linux系统需要添加的头文件为:
#include <unistd.h>
在Linux下,sleep()里面的单位是秒,而不是毫秒。

2.你打印不出来,是因为你的主函数(主线程)已经结束了,你在主函数里加个getchar();让它不要结束就行了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧