在Windows上使用用C线程。简单的例子? [英] Using threads in C on Windows. Simple Example?

查看:159
本文介绍了在Windows上使用用C线程。简单的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要什么,我怎么能使用用C线程在Windows Vista?

What do I need and how can I use threads in C on Windows Vista?

您能给我一个简单的code例子吗?

Could you please give me a simple code example?

推荐答案

下面是的如何在Windows上使用的CreateThread()MSDN样本

其基本思想就是你所谓的CreateThread()和一个指针传递到您的线程函数,也就是将它一旦建立目标线程上运行。

The basic idea is you call CreateThread() and pass it a pointer to your thread function, which is what will be run on the target thread once it is created.

最简单的code来做到这一点是:

The simplest code to do it is:

#include <windows.h>

DWORD WINAPI ThreadFunc(void* data) {
  // Do stuff.  This will be the first function called on the new thread.
  // When this function returns, the thread goes away.  See MSDN for more details.
  return 0;
}

int main() {
  HANDLE thread = CreateThread(NULL, 0, ThreadFunc, NULL, 0, NULL);
  if (thread) {
    // Optionally do stuff, such as wait on the thread.
  }
}

您也可以致电 SHCreateThread的选项() -same基本的想法,但会做一些壳式初始化你,如果你问吧,比如初始化COM等。

You also have the option of calling SHCreateThread()—same basic idea but will do some shell-type initialization for you if you ask it, such as initializing COM, etc.

这篇关于在Windows上使用用C线程。简单的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆