473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

synchronous call


how to make a single method thread safe?

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a.func();

thread2:
A a2 = new A();
a.func();
Ivan
Dec 13 '05 #1
4 1979
Ivan,

The code that you just posted is actually thread safe because it doesn't
do anything.

Making an object or method call thread-safe depends on the granularity
you are looking to achieve, along with the resources you are trying to make
thread safe.

Can you be more specific about what you are trying to make thread-safe?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ivan" <iv*********@dynamic-imaging.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...

how to make a single method thread safe?

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a.func();

thread2:
A a2 = new A();
a.func();
Ivan

Dec 13 '05 #2
A.func() needs to be application-wide thread safe.
Having the example in mind, if a1 enters func(), a2 should wait.

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a1.func();

thread2:
A a2 = new A();
a2.func();
Ivan
Dec 13 '05 #3
Ivan,

Check out the MethodImpl attribute, and pass in the
MethodImplOptions.Synchronized value to the attribute.

However, this will make any call on the object (you need to apply it to
every method) hold whenever any other method is called on any other thread.

Again, you didn't show anything that you were trying to synchronize, so
this is the most generic code you could use.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ivan" <iv*********@dynamic-imaging.com> wrote in message
news:OO**************@TK2MSFTNGP15.phx.gbl...
A.func() needs to be application-wide thread safe.
Having the example in mind, if a1 enters func(), a2 should wait.

example:
class A
{
void func() {}
}

thread1:
A a1 = new A();
a1.func();

thread2:
A a2 = new A();
a2.func();
Ivan

Dec 14 '05 #4
Again, you didn't show anything that you were trying to synchronize, so ... synchronized code is substituted for simplicity by A.func()
this is code that only one thread needs to be in.
if I make it static and use this attribute (thanks for the attribute), my problem's fixed.

example:

using System.Runtime.CompilerServices;

class A
{
[MethodImplAttribute(MethodImplOptions.Synchronized )]
public static void func() {}
}

thread1:
A a1 = new A();
a1.func();

thread2:
A a2 = new A();
a2.func();
Ivan
Nicholas Paldino [.NET/C# MVP] wrote: Ivan,

Check out the MethodImpl attribute, and pass in the
MethodImplOptions.Synchronized value to the attribute.

However, this will make any call on the object (you need to apply it to
every method) hold whenever any other method is called on any other thread.

Dec 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Chris | last post by:
Hi. I have a ibrary I'm trying to use via javascript within IE. This library uses an asynchronous model where I call into a function and pass it a callback function as one of its arguments. My...
1
by: Alexander Kaplunov | last post by:
I have two different scenarios: 1. I have an application that calls a web service and passes an object. Web Service takes that object and does some stuff with it, which could take...
2
by: duncansinclair | last post by:
The following code works in IE, but in Forefox (1.0.7 & 1.5) the Asyncronous function (testA) works fine but the Synchronous one (testS) does not. It just doesn't work & I get no errors. ...
3
by: Pro1712 | last post by:
Hi, this may be a stupid question: How can I can call the DoWork-function of a BackgroundWorker synchronous? Or in other words: How can I extend the BackgroundWorker class with a function...
1
by: Walther W | last post by:
Hello, Is it possible to make a synchronous Web Service? I have a situation where quite big piece of data should be send from client to Web Service server but client and server should not...
4
by: keyofdminor | last post by:
Folks, Short version: Has anyone tried a synchronous call ("SJAX") to a server with the Prototype library? I'm curious if there is a bug in the library (possible) or if I am making mistake...
1
by: intrepid_dw | last post by:
All: I have prepared a .NET 1.1 client application that consumes a remote, public web service. All the functions work as expected, and, in general, the application has been successful for my...
5
by: HugeBob | last post by:
Hi All, I've got a question about Asynchronous vs Synchronous mode with the XMLHttpRequest object. What are the ramifications of using one mode vs the other? If the script uses Asynchronous...
6
by: Simon | last post by:
Hi All, An experiment i'm doing requires requires a synchronous cross-domain request, without using a proxy. I wondered if anyone had any ideas to help me achieve this. Below is what I have...
2
by: ng01 | last post by:
In AjaxPro, if you leave out the Callback parameter, it becomes a synchronous call. Is it possible to perform a synchronous call with ASP.NET AJAX? Please limit responses to the question...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.