LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 07-30-2012, 06:48 AM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871

(Well, most likely you don't have to copy those arrays, but if you are so keen to do so, use either memcpy or nest the arrays into structures; structures can be passed by-value, even if it is a very unusual and un-rational thing to do.)
 
1 members found this post helpful.
Old 07-30-2012, 06:58 AM   #17
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by esgol View Post
But as i repeat, i havent asked councils on this neither referared, JUST HOW TO PASS BY VALUE A DAMNED ARRAY not WHY I SHOULD or IF I SHOULD.
]
In C++:
Code:
#include <array>

static const int ARRAY_SIZE = 10;

void functionCopy(std::array<int, ARRAY_SIZE> a)
{
    // 'a' is a copy of 'arr'
}

void functionSame(std::array<int, ARRAY_SIZE>& a)
{
    'a' is the same as 'arr'
}

int main()
{
    std::array<int, ARRAY_SIZE> arr;

    functionCopy(arr);

    functionSame(arr);
}
P.S. std::array is part of C++0x standards; you may need to specify -std=c++0x when compiling the code above. The alternative is to rely on an std::vector.
 
Old 07-30-2012, 07:01 AM   #18
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
But as i repeat, i havent asked councils on this neither referared, JUST HOW TO PASS BY VALUE A DAMNED ARRAY not WHY I SHOULD or IF I SHOULD.
I can't help with the substantive issue, but I will share with you a reality of forums like LQ:

You will often not get the answer to your question, but the answer to the question that you should have asked. Corollary: People are reluctant to help others with the details of how to go down the wrong path.

Better to focus on what people are trying to say than to just get upset because noone is directly answering your question.
 
1 members found this post helpful.
Old 07-30-2012, 10:02 AM   #19
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by esgol View Post
Yes this is what i ment by conncurent safe. Its the obvious concurency problem.

Yes there are ways to programm concurent with pointers, but since I have only theoritical knowledge of the issue, i'll develop the API as Serial 1st trying to make it as object oriented and functional as possible keeping always in mind how the obgjects and methods, of a Multithread version should be, trying in the same time to secure the atomicity of operations as much as possible avoifing throwing pointers here and there.

If i was sending the arrays pointers then i should use mutexes or make free-lock programming designing the structure of the code entirely different.

For start trying having the code totaly obgect oriented and pure functional its a good start i think :S

But as i repeat, i havent asked councils on this neither referared, JUST HOW TO PASS BY VALUE A DAMNED ARRAY not WHY I SHOULD or IF I SHOULD.

Thats what sergei did not understood thanks for the further interest however, most usually don't even care

P.S.
dwhitney wrote

Lol dwhitney67 ^_^
http://farm4.static.flickr.com/3148/...269be596e0.jpg
Quote:
I have only theoritical knowledge of the issue
- so, it's time to start learning and understanding, isn't it ?

Quote:
Its the obvious concurency problem
- it is not necessarily a problem. For example, do you know what "atomic operation" is, what "atomic read-modify-write" is ?

Regarding the latter, it is the mechanism to acquire locks, and the whole point is that all the contenders are trying to access the same memory location.

Your insistence on passing by value without your explanation why you need shows your apparent misunderstanding of the subject. To make it more clear for you - suppose a human enters some data, say, a "Hello" string.

The human is supposed to be lazy, so he/she will enter the string just once - if you demand from the human to enter the string more than once, the human will most likely look for another program.

So, if you accept that, and if you still think you need multiple copies of the "Hello" string, you need to somehow replicate it. So, why not replicating it in the functions needing to process it ? And then, when you have a copy per function, you don't have the potential concurrency problems because there will be no concurrent access to the same data.

Quote:
If i was sending the arrays pointers then i should use mutexes
- you have so far failed to explain why you need all those complications for read-only data, and you apparently fail to understand you better not use the same data for both input and output, i.e. you better do not process in place. If you follow that principle, you again do not have concurrent access problem.

Regarding read-only data - I suggest first to read http://en.wikipedia.org/wiki/Const-correctness . You'll hopefully be able to find an answer how dealing with data pointed to by pointers avoid writability of the data.


Quote:
the code totaly obgect oriented and pure functional its a good start
- I think you again have a misunderstanding.

Though there is no strict definition of "object oriented", typically when people talk/think about it, classes and their instances are meant, and classes often have data members (with setters and getters for purists). The data members hold state, while functional paradigm gravitates towards pure functions which are stateless.
 
1 members found this post helpful.
Old 07-30-2012, 10:03 AM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by NevemTeve View Post
(Well, most likely you don't have to copy those arrays, but if you are so keen to do so, use either memcpy or nest the arrays into structures; structures can be passed by-value, even if it is a very unusual and un-rational thing to do.)
And we also have unions, don't we ?
 
1 members found this post helpful.
Old 07-30-2012, 12:58 PM   #21
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Very Off: Years ago, working with OpenSsl, I had a happy(?) moment when I realised that between two version of OpenSsl, typedef des_key_schedule has been changed from array to struct.
my code was sg like this:

Code:
myfun (des_key_schedule key) {
/* key is a pointer in the older version
   (call-by-reference) */
/* key is a structure in the newer version
   (call-by-value) */
This little difference did cause me a bit of head-ache, as changes in 'key' never got returned to caller in the newer version.
 
Old 07-31-2012, 01:58 AM   #22
esgol
Member
 
Registered: Jul 2012
Posts: 106

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Post

Quote:
Originally Posted by Sergei Steshenko View Post
- so, it's time to start learning [concurent] and understanding, isn't it ? [except having just theoritical knowledge]
I will but at the 2nd phase once my project has taken a skeleton as serial programmed.
Hope I want regret it ( wont be impossible to make it concurent from afterwards

Quote:
Originally Posted by Sergei Steshenko View Post
-[race condition] it is not necessarily a problem. For example, do you know what "atomic operation" is, what "atomic read-modify-write" is ?
Of course I know all that or at least i think i have understood. As the 2012 book on concurency for C++ 11 i chosed says, Its operations on specific places of the memory that are performed atomical meaning they re manipulated (either if its R or M or W ) only by one control flow (thread) at a time and no race condition exists.
They can be achieved throuh the use of atomic types in a lower and direct level if you re most expert with concurent programming. Thats lock-free programming
Or by lock brygramming through mutexes for more begginers. Gaining the cost of a potential serialization of the threads.

Yes and of couse i know wwhat RMW ops are as far as someone who stood and red page by page the 1200 A4 pages of Ramakrisnan Book DATABASE MANAGEMENT SYSTEMS can know ( except paraler bases and distributed bases

Quote:
Originally Posted by Sergei Steshenko View Post
Your insistence on passing by value without your explanation why you need shows your apparent misunderstanding of the subject. To make it more clear for you - suppose a human enters some data, say, a "Hello" string.

The human is supposed to be lazy, so he/she will enter the string just once - if you demand from the human to enter the string more than once, the human will most likely look for another program.
If i understood the example its completeley irelevant. The user input will be just once and stored to a string array into the structure. The copy will be done by the system. THough i think you inteded something else and I have not understand again.

Quote:
Originally Posted by Sergei Steshenko View Post
So, if you accept that, and if you still think you need multiple copies of the "Hello" string, you need to somehow replicate it. So, why not replicating it in the functions needing to process it ? And then, when you have a copy per function, you don't have the potential concurrency problems because there will be no concurrent access to the same data.
Thats what i do by passing by value its like copying, not? So in the end you agree that what i do through passing y value arrays as structs to each function, its something good?

Quote:
Originally Posted by Sergei Steshenko View Post
- you have so far failed to explain why you need all those complications for read-only data, and you apparently fail to understand you better not use the same data for both input and output, i.e. you better do not process in place. If you follow that principle, you again do not have concurrent access problem.
?! :O you mean doing like
PHP Code:
struct sArray str; { str=func(str); }   func(str){ return str; } 
can lead to instability?? :O Why? The input is done before the output no?

Oh and I never sed its gonna be read-only data, it was just an example the project its in a primitive stage. It will have full RMW operations. Its gonna be a DBMS. But for my Thesis so it wont have functionalities of the commercialss ones ofcourse

Thanks so much for the expanded and descriptive post.

What are unions?
----------------------------------------------------------------------------------
Quote:
Originally Posted by NevemTeve View Post
(Well, most likely you don't have to copy those arrays, but if you are so keen to do so, use either memcpy or nest the arrays into structures; structures can be passed by-value, even if it is a very unusual and un-rational thing to do.)
Why its un-rational, since its the only way to pass arrays by value if someone doesnt desire spreading pointers like dust?
Can you get any problems?

Last edited by esgol; 07-31-2012 at 02:27 AM.
 
Old 07-31-2012, 03:40 AM   #23
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
You still haven't explained why do you want to copy that poor innocent array...
Perhaps you could try and make up an example where this would be useful.
 
Old 07-31-2012, 04:35 AM   #24
esgol
Member
 
Registered: Jul 2012
Posts: 106

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Post

Oh excuse myself.

The simplest of all that i thought it would be obvious:

If the
PHP Code:
func(Array); 
Is threaded in 5 or 10 or 100 threads, THEN ALL THE THREADS will Read Write Modify on the same memory cells, UNLESS you use mutexes or atomic types and so.

Thats a SUPER-Race Condition a nightmare for concurency, I ll be geting a segmentation fault in no-time.

Besides, as a DBMS I will have to deal with Race Conditions to Tables(RAF files) anyway and have the Concurency Controlee. So I try to avoid having also a 2nd level of Race Conditions inside the system.

So i prefer creating memory copies of everything for start. As the project advances I will see if thats not convenient and will deal at that time, making a 2nd Level Concurency Control Mechanisms.
By
PHP Code:
struct CharArray{char Array[SIZE];}; func(CharArray1); 
Spred to 5-10-100 threads I think its impossible to get a race condition since each threads has its own internal copy of the Array.
And copies it back again when returned. No? Is there possible to get at a lower hardware level a racecondition during the "cloning" of the structure?

Last edited by esgol; 07-31-2012 at 04:43 AM.
 
Old 07-31-2012, 04:46 AM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
> Is threaded in 5 or 10 or 100 threads, THEN ALL THE THREADS will Read Write Modify on the same memory cells, UNLESS you use mutexes or atomic types and so.

That's what you should present a plausible example: what actual problem can be solved with threads that randomly access the same memory cells?

Edit: For example, when you have a large array and the same process should performed on each elements, threads might speed up the process, if they work on different sets of elements. (They certainly would slow down the process if they just randomly picked up elements to process.)

Last edited by NevemTeve; 07-31-2012 at 04:58 AM.
 
1 members found this post helpful.
Old 07-31-2012, 06:08 AM   #26
esgol
Member
 
Registered: Jul 2012
Posts: 106

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Post

OPS! OMG the specific you re wright! They return all to the same struct!!!
Thank you for the noting i may have ended doing it actually!!

That seems the reason Sergei sed to not pass in a function the same struct where the function returns
Quote:
better not use the same data for both input and output,
ok sorry but i think in this situation would save you than passing a array pointer.

PHP Code:
struct CharArray char Array[SIZE]; } ; 

class 
CLIENT
{
public:
void Write_Array(struct CharArray Local_Copy_of_Array)
{
    
int i=0;
    for (
i=0;i<SIZE;i++)
        
Local_Copy_of_Array.Array[i]='0';
}
}

int main()
{
struct CharArray sArray;
vector <CLIENT*> vClient;
vClient.pushBack(new CLIENT());
vClient.pushBack(new CLIENT());
vClient.pushBack(new CLIENT());

thread  tClient0 (&CLIENT:Write_Array, &vClient->Client[0] , sArray );
thread  tClient1 (&CLIENT:Write_Array, &vClient->Client[1] , sArray ); 
thread  tClient2 (&CLIENT:Write_Array, &vClient->Client[2] , sArray );  
//If a pointer of an sArray was passed allw would wright the same array.

tClient0.detach(); tClient1.detach(); tClient2.detach();



To be honest im not sure yet if im gonna really need a situation like that yet or where, but I want to be the most carefull

Last edited by esgol; 07-31-2012 at 06:15 AM.
 
Old 07-31-2012, 11:34 AM   #27
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by esgol View Post
I will but at the 2nd phase once my project has taken a skeleton as serial programmed.
Hope I want regret it ( wont be impossible to make it concurent from afterwards


Of course I know all that or at least i think i have understood. As the 2012 book on concurency for C++ 11 i chosed says, Its operations on specific places of the memory that are performed atomical meaning they re manipulated (either if its R or M or W ) only by one control flow (thread) at a time and no race condition exists.
They can be achieved throuh the use of atomic types in a lower and direct level if you re most expert with concurent programming. Thats lock-free programming
Or by lock brygramming through mutexes for more begginers. Gaining the cost of a potential serialization of the threads.

Yes and of couse i know wwhat RMW ops are as far as someone who stood and red page by page the 1200 A4 pages of Ramakrisnan Book DATABASE MANAGEMENT SYSTEMS can know ( except paraler bases and distributed bases
...
No, you do not understand. You have to understand how it happens at physical level. I.e. what is a bus, what is bus master, what is arbitration, etc. Remember, not only CPUs are bus masters, and there may be more than one CPU bus master. And it's much less than 1200 pages.

As I'm reading what you are writing, I'm getting more and more sure you lack fundamental knowledge in computing. And in C/C++ - your question on unions.

If my conclusion is correct, multithreaded programming is not for you yet.
 
  


Reply



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
[SOLVED] bash how pass array as one of several parameters to a function porphyry5 Programming 5 06-14-2011 09:11 PM
[C] Pass pointer of struct to function by refrence PJani Programming 3 11-19-2008 12:49 AM
in C, how do I pass a pointer of an array of structures to a function? randomsel Programming 2 02-04-2008 06:23 PM
Can't pass 2d-array-class-pointer Bariton Programming 3 09-11-2007 08:30 AM
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM

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

All times are GMT -5. The time now is 07:09 PM.

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