LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2010, 07:35 PM   #1
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Rep: Reputation: 16
Multithreaded reading and writing of a shared integer


Hi guys,

It it safe to read a shared integer while another thread is writing to it on a multicore system? I guess the main question here is if the system call to write is atomic for primitives.
 
Old 11-04-2010, 11:40 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
The reading and writing of integers is not a system call, so it is not explicitly protected in any way.

It will mostly work, but there are circumstances in which it is unsafe. For example, the integer type might be larger than the size of the write, or it might not be on a word boundary, and so on. You would also have to watch out for caching and reordering and so on affecting the logic of your code.

You would want to have a good reason for doing this sort of trick.
 
1 members found this post helpful.
Old 11-05-2010, 01:10 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Let's make it simpler:

No: reading and writing a global variable shared between multiple threads is not safe. Not under any circumstances, but especially not under a multi-core CPU.
 
1 members found this post helpful.
Old 11-05-2010, 01:22 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
... unless the app manages locking (properly).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Multithreaded RPC Servers for Linux.Thread-safe code writing dba477 Red Hat 1 08-25-2010 12:33 AM
READING AND WRITING an IMAGE mukhram Programming 2 08-23-2010 02:02 AM
reading integer values from excel using java - interprets it as float kshkid Programming 3 07-28-2008 12:53 AM
writing and reading at same time dideas Programming 16 11-13-2006 06:49 PM
Need help writing and reading shared memory djgannon Linux - Newbie 0 02-23-2006 08:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration