0
votes

I am having a structure

struct myStruct {
    fstream fp;
    char *buffer;
    size_t size;
};

I am new to C++ and trying to write a code wherein one thread will read from a file into buffer and main thread will write the buffer to other file. The sample of the code is as follows:

int main() {
    pthread tid1;
    struct myStruct readArgs;
    fstream fileP, fileP2;
    fileP.open("/tmp/20MBFile", fstream::in);
    fileP2.open("/tmp/trial-file", fstream::out);
    char *buffer;
    readArgs.fp = fileP;
    readArgs.buffer = buffer;
    readArgs.size = 1048576;
    pthread_create(&tid1, NULL, Read, &readArgs);
    pthread_join(tid1, NULL);
    fileP2.write(buffer, 1048576);
    ......
}

The read function is as follows:

void *Read(struct myStruct *readArgs) {
    readArgs->fp.read(readArgs->buffer, readArgs->size);
    pthread_exit(NULL);
}

however, when I compile my code I get following errors:

error: use of deleted function 'std::basic_fstream& std::basic_fstream::operator=(const std::basic_fstream&)' readArgs.fp = fileP;

AND

error: invalid conversion from 'void* ()(myStruct)' to 'void* ()(void)' [-fpermissive] pthread_create(&tid1, NULL, Read, &readArgs); ^ In file included from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/x86_64-redhat-linux/bits/gthr-default.h:35:0, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/x86_64-redhat-linux/bits/gthr.h:148, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/ext/atomicity.h:35, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/bits/ios_base.h:39, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/ios:42, from /usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../include/c++/4.8.3/ostream:38, from /usr/lib/gcc/x86_64-redhat-
....
/usr/include/pthread.h:232:12: error: initializing argument 3 of 'int pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)' [-fpermissive] extern int pthread_create (pthread_t *__restrict __newthread,

Am I missing anything here? Thanks in advance!

1
So what can be the solution here..Bhagyesh Dudhediya
The keyword struct is optional in struct myStruct readArgs; for c++sjsam
It is declared in actual code, missed while pasting it here. Edited it.Bhagyesh Dudhediya
Why aren't you just using the struct's members directly instead of creating extraneous local variables? What is the purpose of the local variables bufffer, fileP, fileP2? They serve no purpose and your main is just doing unnecessary work assigning to them, and then you assign them to your struct's members.PaulMcKenzie
@bhagyeshdudhediya -- You don't need those extra local variables. Just use the member variables in the struct directly (readArgs.fp.open(...);) Second, buffer is uninitialized pointer, and your code is attempting to use it in that state.PaulMcKenzie

1 Answers

0
votes

Found the answer..
1. fstream - fstream cannot be assigned using = but can be moved. swap() also didi not work because of the gcc version. I have gcc version 4.8.3.x which I guess does not support swap() call of fstream.
2. pthread_create() - it expects function with signature void *foo(void *), so one has to pass the required parameter and typecast it when you are in the required function.
Here I would do it like this:

pthread_create(&tid1, NULL, Read, &readArgs);

The Read() function would be :

void *Read(void *args) {
    myStruct *readA = (myStruct *)args;  
    ....
}  

Thank you all for your time!