I am a new learner of c. I am trying to code a car game .I have some problems in it .
In this,the car is moving separately according to the input .The random cars are generated.I am not able to combine both the functions simultaneously. could any one help me how to create thread ..........

In many cases threads complicate matters before they solve them - especially if you are unfamiliar with threading in general.
Why not have some discrete concept of time where you invoke each behavior at each step? Something like:

while (true) {
   has_input = check_for_input
   if (has_input)
      apply_input
   do_controlled_car
   do_random_cars
}

This way you don't have to worry about the issues threads introduce.

Comments
true

In many cases threads complicate matters before they solve them - especially if you are unfamiliar with threading in general.
Why not have some discrete concept of time where you invoke each behavior at each step? Something like:

while (true) {
   has_input = check_for_input
   if (has_input)
      apply_input
   do_controlled_car
   do_random_cars
}

This way you don't have to worry about the issues threads introduce.

I think that's a good idea too.

Something like:..

OR you can just always thread.
On Linux you can use fork().
On windows call CreateThread(), something like this:

#include <windows.h>
#include <iostream>

DWORD CALLBACK threadFunc(void*)
{
printf("New Thread");
 return 0;
}

int main(int argc, const char* argv[])
{
  DWORD id;
  HANDLE h = CreateThread(NULL, 0, threadFunc, NULL, 0, &id);
  int d;
std::cin >> d; //wait for user to input before terminating
 return 0;
}

WARNING: The code is C++, so you will have to change it a bit for C.

Edited 5 Years Ago by sergent: n/a

Invoking each behaviour at each step doesnt work.i tried a new method. i called the moving car in the randomcar using kbhit.so when there is key stroke the moving car is called.
Thanks for ur help

OR you can just always thread.
On Linux you can use fork().
On windows call CreateThread(), something like this:

#include <windows.h>
#include <iostream>

DWORD CALLBACK threadFunc(void*)
{
printf("New Thread");
 return 0;
}

int main(int argc, const char* argv[])
{
  DWORD id;
  HANDLE h = CreateThread(NULL, 0, threadFunc, NULL, 0, &id);
  int d;
std::cin >> d; //wait for user to input before terminating
 return 0;
}

WARNING: The code is C++, so you will have to change it a bit for C.

It's also important to note that Visual Studio has their own version of CreateThread and using CreateThread with VS leaks memory. For some reason <shrug>.

Anyway, for Visual Studio users you can use _beginthreadex() and _endthreadex(),
http://msdn.microsoft.com/en-us/library/kdzttdcb(v=VS.100).aspx

Comments
..

It's also important to note that Visual Studio has their own version of CreateThread and using CreateThread with VS leaks memory. For some reason <shrug>.

Microsoft screwed up something again. Why am I not surprised?

This article has been dead for over six months. Start a new discussion instead.