【发布时间】:2015-08-11 09:26:04
【问题描述】:
我的 c 经验有限,几乎没有使用线程的经验。如果有一种简单的方法可以将“永远”函数包装到线程中并允许 main() 继续运行,请告诉我如何完成。这个例子当然是完全不切实际的,但如果我能让它工作,我认为这是一个很好的起点。也许它甚至不可能?但是很想知道。谢谢!
//
#include <Windows.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include "stdafx.h"
void foreverFunction(); // function prototype
int myNumber=1;
void main()
{
foreverFunction(); // call forever (looping function), would like get it running as a separate thread so that the code below can continue.
while (myNumber != 0 ){
printf("Enter a number (0 to exit): ");
scanf("%d",&myNumber);
printf("Entered: %d\n",myNumber);
}
}
// function below should report the current value of myNumber every second
void foreverFunction(){
while (myNumber>0){
printf ("\nThis will run until the user enters 0, Last value entered was %d\n",myNumber);
Sleep(1000);
}
}
【问题讨论】:
-
请选择一种语言。
-
你用的是c++11吗?
-
Windows、Linux、MacOS ??
-
the documentation 能走多远?如果遇到困难,请尝试返回。
-
@Tommy 您可以在项目的属性中禁用预编译的 stdafx 标头。另外,不要害怕编辑您的问题。只要确保这样做时您不会使任何答案无效(cmets 可以)。
标签: c multithreading winapi