473,791 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pthreads and the new operator for passing structure as argument

this is a thought experiment.
i do not have the time to implement it and test it to see if it works
so i am relying on your good will:)
thank you in advance

im on a linux machine (slackware 10.2) on an i686 intell processor.
its my first attempt to convert a fileIO procedure, to a pthread so it
won't stall the execution of the code.
im going through an online tutorial

http://www.llnl.gov/computing/tutorials/pthreads/

that rightly suggests that when passing argument to a thread i should
generate an array and each thread will be given a reference to a cell
of this array. so far so good.

now fileIO procedure is called every 1-2 seconds but it could need to
go down to 100msec.. in any case i assume the worst case scenario that
one thread of fileIO is NOT completed before a next fileIO thread is
created.

now since the structure of data i pass in the thread is rather big , i
thought its best to use dynamic memory allocation instead of
initializing an constant array. assume the struct is

ThreadPass *pThrdArgs;

so i declare it as such.
in the constructor i say

pThrdArgs = new ThreadPass[1];

so as to allocate 1 structure in mem.
during execution and when the condition is met i do a
memcpy(pThrdArg s[pThrdIDindex],SensorData,siz eof(ThreadPass) )

and i create the thread, run it and increment the pThrdIDindex. at the
very end of the thread execution i reset the
pThrdIDindex = 0 so that on the next fileIO procedure call,
pThrdIDindex will be either 0 (the thread has completed) or 1 in which
case it means it hasnt competed.

what i want to do is to check if the thread has finished execution (by
checking pThrdIDindex or any other means cause i know this way is not
bulletproof) and if it hasn't to somehow allocate space for
pThrdArgs[1], ie do
pThrdArgs = new ThreadPass[2];

or smth similar to allocate space for the next pThrdArgs, while still
keeping the data of the pThrdArgs[0] intact for the 1st thread call to
complete.

additionally and provided this can happen. how should i go about
deleting the pThrdArgs[0] data? will the pThrdArgs[1] remain alive?

thank you for your help
nass

Feb 14 '07 #1
5 3086
nass wrote:
this is a thought experiment.
i do not have the time to implement it and test it to see if it works
so i am relying on your good will:)
thank you in advance

im on a linux machine (slackware 10.2) on an i686 intell processor.
its my first attempt to convert a fileIO procedure, to a pthread so it
won't stall the execution of the code.
This is off-topic here. comp.lang.c++ discusses C++ as defined by the ISO
standard, which doesn't know anything about "linux", "i686" or "pthread".

Having that said, POSIX offers several different ways to do asynchronous I/O
without the need to resort to threads.

Feb 14 '07 #2
On 14 fév, 13:56, "nass" <athanasios.si. ..@gmail.comwro te:
now since the structure of data i pass in the thread is rather big , i
thought its best to use dynamic memory allocation instead of
initializing an constant array. assume the struct is

ThreadPass *pThrdArgs;

so i declare it as such.
in the constructor i say

pThrdArgs = new ThreadPass[1];

so as to allocate 1 structure in mem.
during execution and when the condition is met i do a

memcpy(pThrdArg s[pThrdIDindex],SensorData,siz eof(ThreadPass) )
You'd better std::copy(). But that's only an opinion.
<further explaination>

thank you for your help
nass
I don't get it. pThrdArgs = new ThreadPass[N]; allocates space for N
elements of type ThreadPass. These elements can't be deleted one by
one - you have to delete them all at once. So you can't just "delete
pThrdArgs[0];" - it doesn't have much sense, unless ThreadPass is a
pointer type and pThrdArgs[0] points to an object of type
"typeof(*Thread Pass)" (I know... silly notation). But if this is not
the case, "delete pThrdArgs[0];" will not even compile, as a
ThreadPass instance is not likely to be automatically casted to a
void*.

So, to be clear: I don't understand what you want to do.

Regards,

-- Emmanuel Deloget

Feb 14 '07 #3
On Feb 14, 3:05 pm, Rolf Magnus <ramag...@t-online.dewrote:
nass wrote:
this is a thought experiment.
i do not have the time to implement it and test it to see if it works
so i am relying on your good will:)
thank you in advance
im on a linux machine (slackware 10.2) on an i686 intell processor.
its my first attempt to convert a fileIO procedure, to a pthread so it
won't stall the execution of the code.

This is off-topic here. comp.lang.c++ discusses C++ as defined by the ISO
standard, which doesn't know anything about "linux", "i686" or "pthread".

Having that said, POSIX offers several different ways to do asynchronous I/O
without the need to resort to threads.
you are referring to the use of fstream and the << & >operators ?

Feb 14 '07 #4
On Feb 14, 9:20 am, "nass" <athanasios.si. ..@gmail.comwro te:
On Feb 14, 3:05 pm, Rolf Magnus <ramag...@t-online.dewrote:
nass wrote:
this is a thought experiment.
i do not have the time to implement it and test it to see if it works
so i am relying on your good will:)
thank you in advance
im on a linux machine (slackware 10.2) on an i686 intell processor.
its my first attempt to convert a fileIO procedure, to a pthread so it
won't stall the execution of the code.
This is off-topic here. comp.lang.c++ discusses C++ as defined by the ISO
standard, which doesn't know anything about "linux", "i686" or "pthread".
Having that said, POSIX offers several different ways to do asynchronous I/O
without the need to resort to threads.

you are referring to the use of fstream and the << & >operators ?
I believe he's referring to things like select() and the other basic
non-blocking methods for reading from files.

You don't really mention what your thread is supposed to be doing.
But, most threaded implementations of reading from a file use the
standard producer/consumer model with condition variables.

http://docs.sun.com/app/docs/doc/806...e9h032r?a=view

That webpage looks like it has a description of the model towards the
bottom.

That being said, if what you're wanting to do is read some large file
and processes it as you read it, then you probably just want something
like select(). Or you could even jump to something like libevent.

HTH,
Paul Davis

Feb 14 '07 #5
On Feb 15, 12:31 am, "paul.joseph.da ...@gmail.com"
<paul.joseph.da ...@gmail.comwr ote:
On Feb 14, 9:20 am, "nass" <athanasios.si. ..@gmail.comwro te:


On Feb 14, 3:05 pm, Rolf Magnus <ramag...@t-online.dewrote:
nass wrote:
this is a thought experiment.
i do not have the time to implement it and test it to see if it works
so i am relying on your good will:)
thank you in advance
im on a linux machine (slackware 10.2) on an i686 intell processor.
its my first attempt to convert a fileIO procedure, to a pthread so it
won't stall the execution of the code.
This is off-topic here. comp.lang.c++ discusses C++ as defined by the ISO
standard, which doesn't know anything about "linux", "i686" or "pthread".
Having that said, POSIX offers several different ways to do asynchronous I/O
without the need to resort to threads.
you are referring to the use of fstream and the << & >operators ?

I believe he's referring to things like select() and the other basic
non-blocking methods for reading from files.

You don't really mention what your thread is supposed to be doing.
But, most threaded implementations of reading from a file use the
standard producer/consumer model with condition variables.

http://docs.sun.com/app/docs/doc/806...e9h032r?a=view

That webpage looks like it has a description of the model towards the
bottom.

That being said, if what you're wanting to do is read some large file
and processes it as you read it, then you probably just want something
like select(). Or you could even jump to something like libevent.

HTH,
Paul Davis

i looked some more into things and possibly what i need is aio_write()
of the aio.h header.
what the program is doing:
i am running a loop and when the condition is met (basically a timer)
a buffer gets appended to a file. every so often (another timer) the
filename is changed so the next write must create a new file and start
appending into that. it is really a timed Binary log.
i have managed to do that with stream to file operations (where file
is of type fstream), and i have also managed to do it with fopen().
but it is a requirement that the execution is not halted so i looked
into threads... and then i came across asynchronous write.

now looking into aio_write and the struct aiocb that it takes as input
argument, i see that i must provide a file descriptor instead of ther
usual FILE *, that fopen returns.

and i am wondering if i should be opening the file prior to
aio_write(), or just opening the file once for evey new filename (*ie
when the file doesnt exist), getting the file descriptor, and closing
the file, so that when the time for aio_write() comes , aio_write()
will open, write and close the file on its own.

any ideas?

Feb 15 '07 #6

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

Similar topics

3
6693
by: Savagesmc | last post by:
Problem Background: I am working on a thin wrapper for the pthreads interface on linux, and have encountered frustrating behavior. The idea is to have a "PosixThread" class that any class can inherit from. A child then only needs to define a virtual "executive" method that has the thread code for the child object, and the threading setup and management are automatic then. The thread creation takes place in the PosixThread base class...
5
34378
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would be very appreciative: struct data { float amount; string fname;
34
6472
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
5
5187
by: Jason | last post by:
Hi everyone: I was trying to pass a pointer to a structure to a function, where the pointer is pointing to one of the members in the structure, is this possible??? For Example: struct reptile *animalPtr
9
5299
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
2
3089
by: www.brook | last post by:
I want to enfore the implementation of the operator overloading eg class Interface_ { public: virtual void operator = (const Interface &other)=0; virtual void operator +=(const Interface &other)=0; } but in the sub class
17
2336
by: Ashwin | last post by:
hi guys, i have overloaded the << operator.as shown below. ostream& operator<<(ostream &out, const student &a) { out<<a.idno; out<< " " ; // out<< a.name; out<< " " ; // out<< a.marks << endl;
4
4873
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename _Predicate::argument_type argument_type; typedef typename _Predicate::return_type return_type; _Predicate pred_;
6
3884
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is definined as taking a char * as an argument? For example the C++ header says SDCERR GetCurrentConfig(DWORD *num, char *name); I am using Uint for the *num parameter, which returns the correct value but for *name, I always get back a string of 6 squares....
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10207
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
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,...
0
9029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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();...
0
5431
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...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
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 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.