473,394 Members | 2,020 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,394 software developers and data experts.

Assignning same char value on same location from multiple threads

Each thread executes:
my_char_array[i] = 1;

my_char_array is shared. Char i is private to each thread. However, the value of i may be the same for two or more threads.

No reading is performed. Reading of my_char_array will be done after all threads are finished.

Should I worry and place a mutex lock?

In other words, would it a problem if two threads try to execute say

my_char_array[5]=1

would result in a value different than 1 being set.
Oct 11 '10 #1
4 1524
ashitpro
542 Expert 512MB
Anytime a global resource is accessed by more than one thread the resource should have a Mutex associated with it.

http://www.yolinux.com/TUTORIALS/Lin...ixThreads.html
Oct 12 '10 #2
If they are all writing the same '1' to it, it should be fine. I am assuming it is an array of 'int's also. This is dodgy programming. If you are doing anything other than this, it is *extremely* dodgy programming.
Oct 13 '10 #3
donbock
2,426 Expert 2GB
Please elaborate on what you're trying to do. What is the meaning of the my_char_array? Do the threads ever write anything other than 1 into the array?

Do you intend to use my_char_array as a mutual-exclusion lock? There are reliable algorithms for implementing mutual-exclusion via shared memory without any OS-provided mutex support. See Dekker's algorithm, Peterson's algorithm, and Lamport's bakery algorithm. Notice that these algorithms are all versions of spinlocks.

By the way, it is imperative for all variables accessed by multiple threads to be declared as volatile.
Oct 13 '10 #4
Thank you for your replies.

Array my_char_array[] is an array of char initialized to zeros. The only value to be set by the threads is 1. I do want to avoid using mutex variables.

I'm working on the LU factorization of large sparse matrices. Each thread works with a set of rows. While working a row, a thread tags only a subset of the columns. The issue is that multiple threads may tag the same column. When all threads are finished, I need to know which columns were tagged. Array my_char_array should tell me so.
Oct 13 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Guyon Morée | last post by:
If I have multiple threads reading from the same file, would that be a problem? if yes, how would I solve it? Let's say I want to take it a step further and start writing to 1 file form...
10
by: MikeE | last post by:
Hi all, What's the best way to queue up and wait for number of threads to complete. This problem was trivial in VC++ 6 but I'm finding it rather hard to solve in VB.NET. My calculations run...
2
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these...
9
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same...
3
by: Me | last post by:
I am trying to figure out any issues with calling System.Reflection.MethodInfo.Invoke() when dealing with multiple threads. For instance.. Say I have a class that allows you to pass in a...
6
by: cj | last post by:
As many of you know I'm writing a TCP/IP server with multiple threads handling multiple short conversations (submit a short string, send back a sort string). Threads are created as needed to...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
2
by: =?Utf-8?B?RGFtZW9u?= | last post by:
Hi - I am attempting to write lines to a file at high volume, multiple threads. Here is my scenario: (initial "WriteToFile" object created via a parent multithreaded process, which receives...
16
by: WATYF | last post by:
Hi there... I have a huge text file that needs to be processed. At the moment, I'm loading it into memory in small chunks (x amount of lines) and processing it that way. I'd like the process to be...
1
by: gallois | last post by:
hello everyone, i've been trying to write something that checks the inputs from the possible devices where a mouse can be connected (debian/linux) in order to make it configure the system...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
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...

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.