Computing.Net > Forums > Programming > Threads in C++

Threads in C++

Reply to Message Icon

Original Message
Name: Leo the 28C (by Sulfurik)
Date: September 26, 2005 at 13:17:56 Pacific
Subject: Threads in C++
OS: Windows XP Home SP2
CPU/Ram: 2.8 GHz/448 MB
Comment:

Hello everyone! :-D
OK, I'm making a game, and I need the enemies to move on a separate thread. I know how to do threads in C#, but not on C++... I came across some thread classes but all of them require Visual Studio... Does anybody know of one? I need one that doesn't need my program to be object-oriented. Thanks! ;-)

http://www.xthost.info/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


Report Offensive Message For Removal

Response Number 1
Name: sen (by santanusen_82)
Date: September 26, 2005 at 21:50:09 Pacific
Subject: Threads in C++
Reply: (edit)

Hi Sulf,

Posix threads work fine with C. Hence do not require your program to be object oriented. But it is meant for UNIX based systems like Linux. Not sure it'll work on Windows. It is available with gcc and the corresponding library is "lpthread.a".
Check out for its Windows counterpart.

Santanu Sen
National Institute of Technology
Durgapur
India


Report Offensive Follow Up For Removal

Response Number 2
Name: Leo the 28C (by Sulfurik)
Date: September 27, 2005 at 07:14:54 Pacific
Subject: Threads in C++
Reply: (edit)

Hmm... is this the correct way to do it?:

-------------------------
#include <pthread.h>

pthread_t * monsThread;
pthread_attr_t * attrib;
pthread_create(monsThread, attrib, MoveMonsters); // MoveMonsters being a function
-------------------------

That gives me a bunch of error messages... I read the documentation but they don't give an example... do you know how to do it? Thanks! ;-)

http://www.xthost.info/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


Report Offensive Follow Up For Removal

Response Number 3
Name: sen (by santanusen_82)
Date: September 28, 2005 at 02:02:04 Pacific
Subject: Threads in C++
Reply: (edit)

Hi sulf,

I did thread programming many days ago and don't quite recall everything. The declaration of pthread_create() is something like -

int pthread_create(pthread_t * thread, pthread_attr_t * attr, void *
(*start_routine)(void *), void * arg);


The first argument is obviously the thread pointer. Second one specifies the attributes you wanna pass to the thread. Third one, the most important one, a pointer to a function that'll run as a separate thread. Note that this function must have a return type of void* and take a viod* as argument, something like

viod* thread_function(void* arg);

The fourth one is the argument that will be passed as an argument to the thread function.


Finally, you MUST link your program to the pthread library as follows


gcc yourprog.c -lpthread

Santanu Sen
National Institute of Technology
Durgapur
India


Report Offensive Follow Up For Removal

Response Number 4
Name: Leo the 28C (by Sulfurik)
Date: September 28, 2005 at 14:33:00 Pacific
Subject: Threads in C++
Reply: (edit)

Hmm... does the -lpthread option work on Windows too? I'm still getting a bunch of errors... :-(
Thanks! ;-)

http://sulfurmidis.com/sulfursoft
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

If I connect my microwave to my PC, will I be able to download food?


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Threads in C++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software