473,586 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: VLAs with threads

On 25 May 2008 at 20:15, Aggelidis Nikos wrote:
It is a know fact that pthreads share the same address space. I thought
that this meant that whatever variable i had in main() before creating
a thread, i would be able to use in the thread-itself. Unfortunately
threads have different stacks and that means that any local variable of
main can't be used by threads.
There's a good reason for that. If a variable with automatic storage
could be accessed by any thread, when would you suggest it gets
destroyed?
The problems begin when i decided that i wanted to use a c99 feature:
the variable length arrays {vla}. The problem is that i can't declare a
gloval vla {it isn't permitted by the language}. Also i can't have a vla
inside a structure.... so what can i do?
Create your array on the heap with malloc() instead, protect accesses to
it using a mutex, and decide for yourself when the right time is to
free() it.

Jun 27 '08 #1
5 1454
On Sun, 25 May 2008 20:30:08 +0000, Antoninus Twink wrote:
Create your array on the heap with malloc() instead, protect accesses to
it using a mutex, and decide for yourself when the right time is to
free() it.
but vla's where introduced to solve numeric problems. That's why i want to
use vla...

Jun 27 '08 #2
On 25 May 2008 at 20:44, Aggelidis Nikos wrote:
On Sun, 25 May 2008 20:30:08 +0000, Antoninus Twink wrote:
>Create your array on the heap with malloc() instead, protect accesses to
it using a mutex, and decide for yourself when the right time is to
free() it.

but vla's where introduced to solve numeric problems.
What do you mean? The only possible advantage I can think of of a VLA is
that if you have a heavily-called function then the time savings from
twiddling the stack pointer instead of calling malloc()/free() might be
significant. A big disadvantage of VLAs is that it isn't possible to
detect and attempt recovery if the allocation fails.
That's why i want to use vla...
Use the appropriate tool for the job. A VLA is not a good vehicle for
inter-thread communication.

Jun 27 '08 #3
On Sun, 25 May 2008 21:38:38 +0000, Antoninus Twink wrote:
Use the appropriate tool for the job. A VLA is not a good vehicle for
inter-thread communication.
thanks for the advice Antnoninus, it seems that you are right, i will
revert back to classic malloced arrays...

-nicolas
Jun 27 '08 #4
In article <sl************ *******@nospam. invalid>,
Antoninus Twink <no****@nospam. invalidwrote:
>but vla's where introduced to solve numeric problems.
>What do you mean?
I don't know if it's what's being referred to here, but there's
an obvious use for C99's VLAs in numerical work:

int invert(int n, double matrix[n][n]);

- a construct which many programmers had lamented the lack of over a
couple of decades.

In this case of course there is no allocation.

-- Richard
--
In the selection of the two characters immediately succeeding the numeral 9,
consideration shall be given to their replacement by the graphics 10 and 11 to
facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
Jun 27 '08 #5
On Sun, May 25, 2008 at 09:38:38PM +0000, Antoninus Twink wrote:
On 25 May 2008 at 20:44, Aggelidis Nikos wrote:
On Sun, 25 May 2008 20:30:08 +0000, Antoninus Twink wrote:
Create your array on the heap with malloc() instead, protect accesses to
it using a mutex, and decide for yourself when the right time is to
free() it.
but vla's where introduced to solve numeric problems.

What do you mean? The only possible advantage I can think of of a VLA is
Well, if the OP wants variable length arrays, let's do that, he might
have a good reason: e.g. he has a well tested set of routines for vlas,
and he is trying to port it to a multicore system.

Posix threads are not standard C, but let's not cancel the discussin at this
point. Posix threads allow one single void* to pass to a function executed
in a separate thread. The OP wants now to use this to pass a vla.

One typically creates a struct that contains all the desired data to pass,
and one gives the pointer to this struct (converted to void*) to
pthread_create( ).

If the question was about a one-dimensional array, I'd suggest the flexible
array member. With two dimensions, I'd better let the OP declare the array
in the main, and put a pointer to it (or the its first subarray) into the
sturct of parameters.

Let me show an example:

#include <stdio.h>
#include <pthread.h>

struct parameters {
int n;
void *array;
};

void* thread(void*P)
{
int n=((struct parameters*)P)->n;
double (*A)[n]=((struct parameters*)P)->array;
/* now you have n and A exactly as if you had a prototype, like
thread(int n,double A[n][n])
*/
for(int i=0;i<n;i++,put s("")) for(int j=0;j<n;j++) printf("%g ",A[i][j]);
return NULL;
}

int main()
{
int n=4;
double A[n][n];
for(int i=0;i<n;i++) for(int j=0;j<n;j++) A[i][j]=10*i+j;
struct parameters P={.n=n,.array= (void*)A[0]};
pthread_t T;
pthread_attr_t attr;
pthread_attr_in it(&attr);
pthread_create( &T,&attr,thread ,&P); /* error checking ... */
pthread_join(T, NULL);
}

Szabolcs
Jun 27 '08 #6

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

Similar topics

3
5386
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import thread, sys
0
2000
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux distributions using the Native Posix Threading Layer (NPTL), this isn't entirely true anymore and is AFAIK unsupported (using a pid to signal/identify...
22
3036
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said structure. The compiler obviously allows VLA however it craps out after the above amount of bytes. I was told i was attempting to put everythng on the...
6
3187
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to...
34
10764
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
10
1666
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes, OS execute (All have same priority) Thread#1 may be other threads, Thread#2 may be other threads, Thread#3 may be other threads,
3
5960
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads,...
10
1747
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that T2, T4 and T5 are already running. Thanks, Darian
11
1577
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
0
7841
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...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
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 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...
0
6617
agi2029
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...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3838
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...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1184
bsmnconsultancy
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...

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.