I read, somewhere, that for MS Visual Studio c++ you have to set in the project's option a special stuff to tell the compiler that the apllication it is going to build is a multithread apllication, to avoid problem in the execution.
I would like to know if is there anything similar to set in the Dev C++ Editor or compiler option.
I searched but I did't found anythig that could fit.
So if anybody there may help me it would be great. thanks...
Rain
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I don't know of any such "setting" in Dev-C++. I was able to compile a Petzold example (BIGJOB1.C -- Multithreading Demo) by simply locating an "undefined reference" I saw on a first compile attempt (search done using Windows Explorer), linking that lib, and adding a compiler option to get rid of the console window. Like this:
Compiler: -mwindows
Linker: -lgdi32
-- Jim.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As far as I know, unlikly MS VC++, MinGW (and hence Dev-C++) does not require any special options to be set for multithreading. Below please find a simple example that compiles under DevC++.
And I have a task for you. (Now you know I'm a teacher. Yes, only practice makes perfect!)
Run the program,
see what and *how* it does. Then uncomment the contents of the 'while' loop in main(), recompile, rerun,
draw conclusions. Then correct the loop so that
different threads (after all main() can be regarded as thread nr 0) do not interfere.
Play with the program. Comment out some of its
parts and see what happens. Read MSDN, Petzold or whatever. Have fun!
So, here it comes,
-- Zbyszek
--------------------
#include <windows.h>
#include <process.h> //!! that's where threads are
#include <iostream>
#include <conio.h>
CRITICAL_SECTION cs; //!! This is the most important thing in the program!
int n[2] = {0,0}; // Globals are accessible by all threads and functions
enum Who {e_one=0, e_two=1}; // Gadget type; just not to use ints
void write(Who who)
{
const char s[2][8] = {"1st", "2nd"};
EnterCriticalSection(&cs); //!! system call
n[who]++;
std::cout << "This is the " << s[who] << " thread. The "
<< s[1-who] << " has written to console \t" << n[1-who]
<< " time(s)" << std::endl;
LeaveCriticalSection(&cs); //!! system call
}
void first_thread(void *args)
{
while(1)
{
write(e_one);
Sleep(3000); // Sleep for 3 seconds. From <windows.h>
}
}
void second_thread(void *args)
{
while(1)
{
write(e_two);
Sleep(2000); // Sleep for 2 seconds. From <windows.h>
}
}
int main(int argc, char* argv[])
{
std::cout << "2 simple synchronized threads\n"
"that do not get into each ether's way thanks to synchronization\n"
"Access to a resource (console) has been limited to one function (write)\n"
"Press a kesy to finish.\n"
"=======================================================================";
std::cout << std::endl;
InitializeCriticalSection(&cs); //!! system function
_beginthread(first_thread, 0, NULL); //!! system function, WIN32 specific
_beginthread(second_thread, 0, NULL); //!! system function, WIN32 specific
while (!kbhit()) // wait for a key. From <conio.h>
{
// std::cout << "Hello from main! Didn't I iterrupt?\n";
// Sleep(5000); // sleep for 5 seconds. From <windows.h>
}
// Upon completion of main() all threads will be destroyed automatically
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Is this relevant? (I don't fully understand all the issues...) -- Jim.
From: "Using the GNU Compiler Collection (GCC)"
-mthreads
Support thread-safe exception handling on Mingw32. Code that
relies on thread-safe exception handling must compile and link
all code with the -mthreads option. When compiling, -mthreads
defines -D_MT; when linking, it links in a special thread helper
library -lmingwthrd which cleans up per thread exception handling
data.
----
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
(Old guy talking to self, actually remembering seeing Bill Gates [himself!] presenting a beta version of NT with its *amazing* "multithreading capabilities". The demo crashed... )
Plugged a try-throw-catch routine into the (very helpful -- thanks!) code from Zbyszek, and sure enough, -mthreads was needed. Looked further into the (pure) Win API approach, and get the (vague) impression that -- if you write your code to stay entirely within the Win API "context" -- there might be no need for any special MinGW compile/link options.
Still exploring this topic (and don't mean to "hijack" the thread), appreciate the posts so far, and hope it draws some more input....
Also played with pthreads (in Cygwin). Don't yet know what to make of that....
Sorry there's no specific question. Just wondering what experiences people have had in this area....
-- Jim.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good site, with some excellent links. I agree it's an interesting topic. For now, I will probably stick with the Cygwin implementation of pthreads, for experimentation. From what I have read, things might not be as portable as one might hope. I still have a suspicion that, for Windows, the Win API might be the best way to go. Thanks for posting!
-- Jim.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I read, somewhere, that for MS Visual Studio c++ you have to set in the project's option a special stuff to tell the compiler that the apllication it is going to build is a multithread apllication, to avoid problem in the execution.
I would like to know if is there anything similar to set in the Dev C++ Editor or compiler option.
I searched but I did't found anythig that could fit.
So if anybody there may help me it would be great. thanks...
Rain
nobody knos anything about threads in Dev c++?
:.(
Rain --
I don't know of any such "setting" in Dev-C++. I was able to compile a Petzold example (BIGJOB1.C -- Multithreading Demo) by simply locating an "undefined reference" I saw on a first compile attempt (search done using Windows Explorer), linking that lib, and adding a compiler option to get rid of the console window. Like this:
Compiler: -mwindows
Linker: -lgdi32
-- Jim.
For reference, here's a recent "thread" started by someone exploring the general topic of threading, in case it might be useful:
http://sourceforge.net/forum/message.php?msg_id=2358589
-- Jim.
Rain --
As far as I know, unlikly MS VC++, MinGW (and hence Dev-C++) does not require any special options to be set for multithreading. Below please find a simple example that compiles under DevC++.
And I have a task for you. (Now you know I'm a teacher. Yes, only practice makes perfect!)
Run the program,
see what and *how* it does. Then uncomment the contents of the 'while' loop in main(), recompile, rerun,
draw conclusions. Then correct the loop so that
different threads (after all main() can be regarded as thread nr 0) do not interfere.
Play with the program. Comment out some of its
parts and see what happens. Read MSDN, Petzold or whatever. Have fun!
So, here it comes,
-- Zbyszek
--------------------
#include <windows.h>
#include <process.h> //!! that's where threads are
#include <iostream>
#include <conio.h>
CRITICAL_SECTION cs; //!! This is the most important thing in the program!
int n[2] = {0,0}; // Globals are accessible by all threads and functions
enum Who {e_one=0, e_two=1}; // Gadget type; just not to use ints
void write(Who who)
{
const char s[2][8] = {"1st", "2nd"};
EnterCriticalSection(&cs); //!! system call
n[who]++;
std::cout << "This is the " << s[who] << " thread. The "
<< s[1-who] << " has written to console \t" << n[1-who]
<< " time(s)" << std::endl;
LeaveCriticalSection(&cs); //!! system call
}
void first_thread(void *args)
{
while(1)
{
write(e_one);
Sleep(3000); // Sleep for 3 seconds. From <windows.h>
}
}
void second_thread(void *args)
{
while(1)
{
write(e_two);
Sleep(2000); // Sleep for 2 seconds. From <windows.h>
}
}
int main(int argc, char* argv[])
{
std::cout << "2 simple synchronized threads\n"
"that do not get into each ether's way thanks to synchronization\n"
"Access to a resource (console) has been limited to one function (write)\n"
"Press a kesy to finish.\n"
"=======================================================================";
std::cout << std::endl;
InitializeCriticalSection(&cs); //!! system function
_beginthread(first_thread, 0, NULL); //!! system function, WIN32 specific
_beginthread(second_thread, 0, NULL); //!! system function, WIN32 specific
while (!kbhit()) // wait for a key. From <conio.h>
{
// std::cout << "Hello from main! Didn't I iterrupt?\n";
// Sleep(5000); // sleep for 5 seconds. From <windows.h>
}
// Upon completion of main() all threads will be destroyed automatically
return 0;
}
Zbyszek,
Why don't you put this in the "Read This Before Asking A Question" thread.
Thanks, when I get a chance, I want to do your lesson!
Wayne
Essentially, the standard C runtime library is not thread safe. So you pick multi-threaded to link with a safe one.
See here for more detailed answer:
http://www.microsoft.com/msj/0799/Win32/Win320799.aspx
http://www.microsoft.com/msj/1099/win32/win321099.aspx
rr
Is this relevant? (I don't fully understand all the issues...) -- Jim.
From: "Using the GNU Compiler Collection (GCC)"
-mthreads
Support thread-safe exception handling on Mingw32. Code that
relies on thread-safe exception handling must compile and link
all code with the -mthreads option. When compiling, -mthreads
defines -D_MT; when linking, it links in a special thread helper
library -lmingwthrd which cleans up per thread exception handling
data.
----
(Old guy talking to self, actually remembering seeing Bill Gates [himself!] presenting a beta version of NT with its *amazing* "multithreading capabilities". The demo crashed... )
Plugged a try-throw-catch routine into the (very helpful -- thanks!) code from Zbyszek, and sure enough, -mthreads was needed. Looked further into the (pure) Win API approach, and get the (vague) impression that -- if you write your code to stay entirely within the Win API "context" -- there might be no need for any special MinGW compile/link options.
Still exploring this topic (and don't mean to "hijack" the thread), appreciate the posts so far, and hope it draws some more input....
Also played with pthreads (in Cygwin). Don't yet know what to make of that....
Sorry there's no specific question. Just wondering what experiences people have had in this area....
-- Jim.
Hi everyone,
This is an interesting topic. I'm wondering if any of you tried pthreads for win32.
http://sources.redhat.com/pthreads-win32/
Ahmed
Ahmed --
Good site, with some excellent links. I agree it's an interesting topic. For now, I will probably stick with the Cygwin implementation of pthreads, for experimentation. From what I have read, things might not be as portable as one might hope. I still have a suspicion that, for Windows, the Win API might be the best way to go. Thanks for posting!
-- Jim.