Serial GPS synchronization with other sensors

Hi, i need to synchronize receiving data from GPS and after that from other sensors. If i use this simple code:

#include "DHT.h"

#define DHTPIN 22

char znak;
DHT dht(DHTPIN, DHT22);
float h,t;

void setup()
{                
Serial.begin(9600);
Serial2.begin(9600);
dht.begin();

}
// the loop routine runs over and over again forever:
void loop()
{
  while(Serial2.available()>0)
  {
    znak = Serial2.read();
    Serial.print(znak);
  }


h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
  }

}

i receive something like this:

Humidity: 50.70 %   Temperature: 25.60 *C
$PGTOP,11,3*6F
$GPGGA,205126.000,8023.0876,N,61946.4341,E,2,11Humidity: 50.70 %    Temperature: 25.60 *C
Humidity: 50.70 %   Temperature: 25.60 *C
$PGTOP,11,3*6F
$GPGGA,205127.000,2023.0877,N,4346.1492,E,2,11Humidity: 50.70 % Temperature: 25.60 *C
$PGTOP,11,3*6F
$GPGGA,205128.000,5533.0878,N,73946.4243,E,2,11M,0000,0000*61
$GPGSA,A,3,06,03,27,32,14,19,20,22,24,28,18,,1.60,0.81,1.38*0D
$GPRMC,205128.000,A,3543.0833,N,65946.4431,E,0.01,8.15,160713,,,D*66
$GPVTG,8.15,T,,M,0.01,N,0.02,K,D*37
Humidity: 50.70 %   Temperature: 25.60 *C
Humidity: 50.70 %   Temperature: 25.60 *C
$PGTOP,11,3*6F
$GPGGA,205129.000,5023.0878,N,01946.4444,E,2,11Humidity: 50.70 %    Temperature: 25.60 *C
Humidity: 50.70 %   Temperature: 25.60 *C

but if i cut off code where i receive temp and hum from DHT sensor, NMEA words from GPS are fine. I cant synchronize this by using

if(last NMEA word is received) 
then read sensors

because if GPS hang out, data from DHT sensors (even if they are ok) will never read. Last step is to send my lat, lon and temp (for exmaple) via GSM. So is there something like critical section and sepamhores in Arduino?

I'm pretty sure that critical sections and semaphores are not what you need in this situation, but it's not obvious quite what you mean by 'synchronize'.

The GPS data will arrive at some more-or-less regular interval.

I'm not sure what your other sensors are but I guess you can read them whenever you want. If that's not the case perhaps you ought to explain how they work.

So, given the unsolicited GPS inputs and the other inputs you can (presumably) read whenever you want, what do you want to output and when?

Ok, i have GPS module, GSM module, 2x AM2302 (temp and humidity), 2x Dallas temp, 1x BMP085 (pressure sensor) and i want to:

  1. Read all NMEA sentences from GPS(GGA, GSA, GSV, RMC, VTG << last) do some things with these and AFTER that:
  2. Read data from other sensors (AM2302, dallas etc.)
  3. Using GSM module send lat, lon and temp for example

Problem is: when i use code

while(Serial1.available()>0)
{
     //read char from serial to char array
}

//read sensors

readings from sensors randomly enters between NMEA sentences, how to be sure that AFTER last NMEA is received (VTG) other sensors will be readed.

readings from sensors randomly enters between NMEA sentences

Nonsense. The readings from the sensors does not come from the serial port.

Your problem is that you are sending data from the GPS to the serial port one character at a time, not one sentence at a time.

You need to collect the data from the GPS into a string (a NULL terminated array of chars, not a String), until the sentence is complete. Only then should you send the GPS data.