Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 5th, 2008, 8:29 AM   #1
mynickmynick
Newbie
 
Join Date: May 2008
Posts: 7
Rep Power: 0 mynickmynick is on a distinguished road
Question sig_atomic_t to avoid mutex/critical sections

On linux using pthreads, if I have a 2-bytes variable x shared by pthreads used only in the following ways:
(1)
x=value;
(2) or (separately)
if (x==value) {do something (not with x)}
(3) or (separately)
other variable (not shared) = x;

Is it correct to define it
sig_atomic_t x; (no matter if 4 or 2 bytes)
and AVOID to protect every instruction of kind (1) or (2) or (3) with pthread_mutex_lock()/trylock() before and pthread_mutex_unlock() after ?
Can I be sure there is no race condition/unpredicted event/problem?

In a few words: is it correct to speed up a software using sig_atomic_t data instead of mutexed data with the fundamental condition that data are as little as 2-4 bytes and are accessed only in the very simple way above?

Thanks
mynickmynick is offline   Reply With Quote
Old May 5th, 2008, 4:24 PM   #2
ranton
Newbie
 
Join Date: Apr 2008
Location: Birmingham, AL
Posts: 3
Rep Power: 0 ranton is on a distinguished road
Re: sig_atomic_t to avoid mutex/critical sections

Quote:
Originally Posted by mynickmynick View Post
On linux using pthreads, if I have a 2-bytes variable x shared by pthreads used only in the following ways:
(1)
x=value;
(2) or (separately)
if (x==value) {do something (not with x)}
(3) or (separately)
other variable (not shared) = x;

Is it correct to define it
sig_atomic_t x; (no matter if 4 or 2 bytes)
and AVOID to protect every instruction of kind (1) or (2) or (3) with pthread_mutex_lock()/trylock() before and pthread_mutex_unlock() after ?
Can I be sure there is no race condition/unpredicted event/problem?

In a few words: is it correct to speed up a software using sig_atomic_t data instead of mutexed data with the fundamental condition that data are as little as 2-4 bytes and are accessed only in the very simple way above?

Thanks
In the specific ways you indicate you want to access it, you should be safe, but there are other things you can't. Like safely do anything that may take more than one operation, such as increment the value(load, add,store).

This method is fine for doing something like quitting when a signal handler quits a flag which I have used it for in quite a few server processes I have written.
__________________
-Richard Anton
http://www.ranton.org
ranton is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Avoid Connecting To MySQL Multiple Times Sane Python 4 Nov 23rd, 2007 12:42 PM
Avoid default dialog box for file download? java_roshan Other Web Development Languages 1 Sep 12th, 2006 9:38 PM
Repetitive replacing sections of text files Novice Other Scripting Languages 2 Jun 17th, 2005 9:07 AM
How Avoid Dialog Box Open/Save Files see07 C# 3 Mar 30th, 2005 7:06 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC