6

I am trying to make a windows-version of a program written for Linux, in C++. For the program to be thread-safe, I use pthread_cond_t and pthread_cond_wait in the Linux version. These functions use a mutex to help make sure that the waiting thread is actually waiting.

I found that CONDITION_VARIABLE may do the trick in Windows, however I can't figure out why it wont compile. I get the error "error: 'CONDITION_VARIABLE' does not name a type" even though all relevant headers are included, as far as I can tell. I tried copy-pasting the code on http://msdn.microsoft.com/en-us/library/ms686903%28v=VS.85%29.aspx , which wont compile either. I am using GCC.

Any ideas on how to compile this? or any alternate approaches, which doesn't involve condition_variables?

Ajay
  • 18,086
  • 12
  • 59
  • 105
pjaall
  • 103
  • 1
  • 5
  • What compiler are you using and how old is your Windows SDK? [Notice](http://msdn.microsoft.com/en-us/library/ms683469(v=VS.85).aspx) that you need at least Vista SDK. – wilx Jul 29 '11 at 11:36
  • maybe windows.h used by gcc/cygwin(?) is too old? I suppose it would be better to use Visual Studio Express for Windows, or to steal windows.h and other system headers from it installation or to upgrade windows headers and libraries in some another way. – user396672 Jul 29 '11 at 11:42

3 Answers3

4

Did you define _WIN32_WINNT and WINVER before #include <windows.h>?

This is necessary to include definitions for things only added in later versions of Windows. For condition variables you need to set these to at least 0x0600 as condition variables were new in V6 (ie. Vista/2008).

See http://msdn.microsoft.com/en-us/library/aa383745%28VS.85%29.aspx

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Yea.. that sounds like something he might be missing. – Vite Falcon Jul 29 '11 at 11:04
  • 1
    Good idea, but it still wont compile. My program looks like #define _WIN32_WINNT 0x0600 #define WINVER 0x0600 #include #include #include #include int main(void) { CONDITION_VARIABLE go; return 0; } – pjaall Jul 29 '11 at 11:29
  • @pjaall What version of the Windows SDK/Visual Studio/C++ are you using? – Richard Jul 29 '11 at 14:47
2

Check that you have latest Windows headers, and WinBase.h has the following line:

typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;

And of course, you have _WIN32_WINNT #defined to at least 0x600.

Ajay
  • 18,086
  • 12
  • 59
  • 105
0

You mentioned that you're using pthread library for threading. Have you looked into this article about using mutex for windows, when using pthread.

Vite Falcon
  • 6,575
  • 3
  • 30
  • 48