The below is perfectly OK in C++, but I wonder how safe is it C#. There are
cases when one thread is writing to, another one is reading from the same
variable. I don't want/need to synchronize the access.
My question is, can the .NET framework handle this scenario, or it may throw
an exception? I realize the data may be out of sync (say, a half of the
variable contains new data, another half contains old data), but it's not an
issue.
Thanks
uint64 var = 0;
thread1_Write()
{
while(true)
{
var = random();
}
}
thread2_Read()
{
while(true)
{
Console.Write(" {0}", var);
}
} 2 1318
cristalink <cr********@nos pam.nospamwrote :
The below is perfectly OK in C++, but I wonder how safe is it C#.
Well, that depends on what you mean by safe. Your definition appears to
be a lot looser than mine. :)
There are cases when one thread is writing to, another one is reading
from the same variable.
The variable you've given is a long, which can't be made volatile. If
you're using a variable which could be declared volatile, you'd be
okay.
I don't want/need to synchronize the access.
Is this for performance reasons? If so, I *strongly* recommend that you
write code with full synchronization first and measure its performance.
Locking is very fast.
My question is, can the .NET framework handle this scenario, or it may throw
an exception? I realize the data may be out of sync (say, a half of the
variable contains new data, another half contains old data), but it's not an
issue.
You won't get an exception, but you might get half-changed data, or
worse you might not even see the new data at all from the reading
thread.
See http://www.pobox.com/~skeet/csharp/t...latility.shtml for
more information.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
"cristalink " <cr********@nos pam.nospamwrote :
The below is perfectly OK in C++, but I wonder how safe is it C#.
The code should behave the same in C++ and C# on a 32-bit machine - it's
possible to get partial reads and writes to var, where one half is read
while the other half is written to.
As far as exceptions or whatever, no, there'll be no exceptions.
-- Barry
-- http://barrkel.blogspot.com/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Eric B. |
last post by:
Hi,
I'm running into a slight programming issue here that I am hoping someone
can help me with. I am developping a web-based PHP application, which means
I have to consider multiple threads of the same PHP script running at the
same time.
Here is my problem. I have 2 database tables Table A and Table B. Table A
contains a counter field that I need to increment every time I save a record
in Table B. Furthermore, I need to save the...
|
by: Marshal Antony |
last post by:
Hi,
I have a web service creates a # delimited text file
on a server different than the web server per day for
tracking databse transactions.All transactions will be
writing to this same text file for the day.I was testing
this web service written in C# from a web page,
created 5 threads which create the file and 10 inserts
to the same file total.
But I got unpredictable results(not all records created,
|
by: Bill Davidson |
last post by:
Hello All:
I've got a question about synchronization requiremements in a C# worker
thread procedure that, among other things, sinks events from outside
sources. I realize the worker thread will be interrupted to handle an
incoming event (and the flow of execution subsequently diverted to the
respective event handler). My question is at what point or points could
this interruption occur. Will it only occur when the worker thread is...
|
by: Bill Davidson |
last post by:
(RESEND: I added a little more code to the sample for clarity)
Hello All:
I've got a question about synchronization requiremements in a C# worker
thread procedure that, among other things, sinks events from outside
sources. I realize the worker thread will be interrupted to handle an
incoming event (and the flow of execution subsequently diverted to the
respective event handler). My question is at what point or points could
this...
|
by: scott |
last post by:
hi all,
Thx to any one that can offer me help, it will be much appreciated.
iv got a multithreaded program and need to use thread synchronization. The
synchronization does not have to work across multiple processes just the
one. I was wondering if any one new which one used the least overhead. Im
at current using mutexes but was wondering if there was something a bit
| |
by: Rod |
last post by:
I orginally posted this to microsoft.public.sqlserver.ce but had not
received any responses.
I have a CF.NET application (C#) with a SqlCE database. We had
originally planned to use SQL Authentication to replicate with the
publication and indeed had SQL Authentication working flawlessly. Then
our client asked us to use Windows Authentication instead.
For incremental changes, Windows authentication works fine. When
trying to do the...
|
by: emma_middlebrook |
last post by:
Hi
Say you had N threads doing some jobs (not from a shared queue or
anything like that, they each know how to do their own set of jobs in a
self-contained way). How can you coordinate them so that they all wait
until they've all done one job before starting off on each of their
next jobs.
I have been thinking about this for a day and can't seem to find a
solution.
|
by: Tony Gravagno |
last post by:
I have a class that instantiates two Timer objects that fire at
different intervals. My class can be instantiated within a Windows
Form or from a Windows Service. Actions performed by one of the event
handlers may take longer than the interval for either of the timers,
so it's possible for multiple events to fire "simultaneously" and for
events to queue up. I'm attempting to get the timers to sync on some
reference type object, or use...
|
by: jimdefruscio |
last post by:
Hi,
I have developed a ASP.NET 2.0 web application and am having a data
access synchronization issue. I would appreciate anybody's
experience. My application is a multi-user application where each
person is working from a queue of work items. They open the app and
click a button which gets the next unprocessed item on the queue and
locks the record for the user by setting an isLocked flag on the
database record. When they are done...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |