![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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. |
|
#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 |
|||
| Google The UNIX and Linux Forums |