473,396 Members | 1,655 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,396 software developers and data experts.

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(pThrdArgs[pThrdIDindex],SensorData,sizeof(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 3065
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.comwrote:
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(pThrdArgs[pThrdIDindex],SensorData,sizeof(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(*ThreadPass)" (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.comwrote:
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.comwrote:
On Feb 14, 9:20 am, "nass" <athanasios.si...@gmail.comwrote:


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
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...
5
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...
34
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...
5
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...
9
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...
2
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...
17
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...
4
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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...
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...
0
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,...

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.