22

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

Could you please give me a simple code example?

Jiminion
  • 5,080
  • 1
  • 31
  • 54
szaman
  • 6,666
  • 13
  • 53
  • 81
  • 3
    While this may be a simple RTFM question, it isn't not a real question. After all, there are several real answers already. – i_am_jorf Dec 30 '09 at 18:02

3 Answers3

40

Here is the MSDN sample on how to use CreateThread() on Windows.

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.

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.
  }
}

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.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • 3
    Keep in mind, however, that if you're going to use the CRT in the new thread you may need to be extremely careful. In MSVC, for example, you should use _beginthread/_beginthreadex and _endthread instead of the relative APIs, to let the CRT correctly allocate/deallocate its internal per-thread structures. I think that also in other CRTs it should go somehow like that. – Matteo Italia Dec 31 '09 at 02:20
  • Additionally you can use SHCreateThread() if you want it to initialize COM for you on the new thread, among other things. – i_am_jorf Mar 06 '13 at 18:10
  • This seems to be a C++ example (link). Would this work for C as well? – Jiminion Jan 13 '16 at 15:37
  • Win32 is a C api, so, yes, it should work. – i_am_jorf Jan 13 '16 at 17:27
  • @MatteoItalia how do we join or detach a thread in c `windows`? is there a good tutorial on it? – Ahmed Can Unbay Oct 07 '17 at 14:55
  • 1
    @turmuka: handling a thread is slightly different on Win32 than with pthreads; the `HANDLE` you get is similar to the pthread id, once you are no longer interested in waiting for it or getting its exit code you have to call `CloseHandle` on it, so you may say that `CloseHandle` is similar to `pthread_detach`. `pthread_join` job is split between several calls - use `WaitForSingleObject` to wait for the thread end, `GetExitCodeThread` to get its exit code (if you care about it) and, as always, `CloseHandle` once you finished using the handle. – Matteo Italia Oct 07 '17 at 15:02
  • if you are interested, I have another question. thank you so much by the way. I always ended up finding stuff about *C++*. Please check this link. https://codeshare.io/21Dkdq I asked a question with a comment there – Ahmed Can Unbay Oct 07 '17 at 15:14
  • That's not a good forum for asking a question. Please ask the question on StackOverflow and paste the code into your question. And also look up "infinite loop" while you're at it. – i_am_jorf Oct 12 '17 at 15:54
3

You would use the CreateThread function.

You mentioned semaphores as well. For that you would use CreateSemaphore.

1

Atomic operations and mutexes are good. I use CreateThread etc, not pthreads.

martinr
  • 3,794
  • 2
  • 17
  • 15