The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
The Hello Thread pathological What's on Your Mind? 4 09-23-2006 05:41 AM
Thread ! what is this iwbasts High Level Programming 1 10-26-2005 11:00 PM
How to cancel a thread safely from the initial thread? alan.zhao High Level Programming 1 04-29-2005 07:07 AM
thread asutoshch UNIX for Dummies Questions & Answers 1 03-28-2001 11:49 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 11-07-2003
Registered User
 

Join Date: Sep 2003
Location: south of hell
Posts: 3
TZ variable set within thread

Hello all,
I'm going to be using some of the date functions from time.h to do some time stamping. I will be getting a time and date from the header of a TIFF file. I will need to be able create a time for each time zone in the U.S. The source of the time stamp will be in GMT. What I’d like to do is to take the GMT time and date, load it up into a tm structure as GMT and set the TZ variable and then use localtime() to convert to the time zone I want. I need to be sure that this will only impact the thread I’m currently in. Since there will be many threads doing this at any given time I need to be sure that the manipulation of this variable will be local to the thread.
Thanks in advance!
-Bryan
__________________
Let the might of your compassion bring a quick end to flowing stream of the blood and tears
Reply With Quote
Forum Sponsor
  #2  
Old 11-07-2003
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
Bryan, this is not going to fly.

First, localtime() is not MT-safe. So you will need to use localtime_r().

But you can't use TZ like that. There is only one environment per process. Different threads can call putenv() without stepping on each other. But the last thread to change TZ wins. This is spelled out in the putenv man page: "These functions examine and modify the environment list, which is shared by all threads in a program."

The effect of TZ is to set some variables by indirectly invoking tzset(). I thought about bypassing tzset(), but it says here: "The ctime_r(), localtime_r(), and tzset() functions are MT-Safe in multithread applications, as long as no user-defined function directly modifies one of the following variables: timezone, altzone, daylight, and tzname. These four variables are not MT-Safe to access."

Maybe you can arrange for one process per time zone? Or write your version of localtime() that uses no per-process data?

I suppose that this sequence would work:
pthread_mutex_lock()
putenv()
tzset()
localtime_r()
pthread_mutex_unlock()
But I don't have enough experience with threads to know if that is as lame as it looks.

Personally, I would go with writing my own timezone routines. I know.... it's a lot of work. But it's the only path I see to a perfect result.
Reply With Quote
  #3  
Old 11-07-2003
Registered User
 

Join Date: Sep 2003
Location: south of hell
Posts: 3
Thx! I'll work on it a little more and check out another approach. If I come up w/anything cool I'll post it here.
-B
__________________
Let the might of your compassion bring a quick end to flowing stream of the blood and tears
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0