I need to use pthreads in C++ but I can't use the function pthread_create, it shows me an error. Also, I need to pass multiple parameters to a method:
void Read(int socks, int client) {
while (1) {
int n;
char buffer1[256];
bzero(buffer1, 256);
n = read(socks, buffer1, 255);
if (n < 0) {
perror("ERROR leyendo el socket");
exit(1);
}
cout << "Mensaje de cliente " << client << ":" << buffer1 << endl;
Jsons json1;
json1.parseJson(buffer1);
writeMsg(socks, "hola\n");
}
}
void ThreadServer::Thread(int sock, int client) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_t tid;
pthread_create(&tid,&attr,Read);
}
If I understand you correctly, you want to send multiple parameters to a thread. The thread functions for pthread take a single void *.
void threadfn(void *data);
you just need to create a data structure to hold your parameters
struct threadData
{
int param1;
int param2;
};
declare your struct and assign parameter values. When you call pthread_create, pass the struct pointer.
struct threadData data = {1,2};
pthread_create(&tid, &attr, Read, &data);
when you get the pointer in read function, cast and use it to extract parameters.
void Read( void * thrData)
{
struct threadData *myParams = (struct threadData*)thrData;
.
.
.
Related
I want to call a function with multiple threads, and I only need to pass a single integer to that function (the thread id, so if it is accessible I need no value to pass).
How should I do this?
for example like:
for(int i=0; i < numberOfThread; i++ ){
pthread_create(&threads[i], NULL, multichaper, &td[i]);
}
in which multichaper is my function and threadID is an integer.
Update: I marked the answer from user3286661 as the right answer and that worked for me, if you want a more detailed answer you can check my own solution to this question in answers.
General approach to this is to make the function like this:
void* multichaper(void* arg) {
int tid = *(int*)arg;
...
}
And while calling pthread_create:
pthread_create(&threads[i], NULL, multichaper, &td[i])
where td[i] is an int.
You really should consider moving to C++11 threads:
#include <thread>
#include <iostream>
void show_id(int id) {
std::cout << id << std::endl;
}
int main()
{
std::thread t(show_id, 10);
t.join();
}
If you must use pthreads, though:
#include <iostream>
#include <pthread.h>
void *show_id(void *x_void_ptr)
{
const int id = *static_cast<int *>(x_void_ptr);
std::cout << id << std::endl;
return NULL;
}
int main()
{
pthread_t t;
int id = 10;
if(pthread_create(&t, NULL, show_id, &id)) {
std::cerr << "couldn't create" << std::endl;
return -1;
}
if(pthread_join(t, NULL)) {
std::cerr << "couldn't join" << std::endl;
return -2;
}
}
Note how much better the first version is:
No casts
Fewer explicit checks
No problem with the lifetime of the object you're passing - in the first version, you're passing a pointer to it, and thus must ensure it's "alive" while the thread is using it.
No unintuitive void * returns (with the same lifetime problems).
No. You can't do that. The function you pass to pthread_create must have the signature void *(*start_routine) (void *). That is, a function taking a non-const pointer to void and returning a non-const pointer to void.
The simplest way is something like:
int *arg = new int(threadID);
pthread_create(&threads[i], NULL, multichaper, threadID );
and then multichaper looks like:
void *multichaper(void *arg)
{
int *pint = static_cast<int*>(arg);
int threadID = *pint;
delete pint;
...
return nullptr;
}
Note that I have allocated the int on the heap to avoid having to worry about variable lifetimes. If you can guarantee that the variable threadID in the calling function will outlive the thread, then you can skip that bit.
I strongly recommend you use C+11 and the built-in threading library, or if you can't do that, use boost::threads. They both make this much easier!
As i want to pass numbers from 0 to NumberOfThreads to my function i finally used the code below, by passing an integer inside a struct and locking (lock_mutex) that when trying to retrieve the threadNum:
Calling function in multi threads in a member function of SVAnchor class:
pthread_t threads[this->numberOfThread];
pthread_attr_t attr;
params_t params;
pthread_mutex_init (¶ms.mutex , NULL);
pthread_cond_init (¶ms.done, NULL);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(int i=0; i < this->numberOfThread; i++ ){
params.id = i;
params.ptr = this;
rc = pthread_create(&threads[i], NULL, &(SVAnchor::multichaperWrapper), ¶ms);
pthread_cond_wait (¶ms.done, ¶ms.mutex);
}
pthread_attr_destroy(&attr);
void* status;
for(int i=0; i < this->numberOfThread; i++ )
rc = pthread_join(threads[i], &status);
pthread_mutex_destroy (¶ms.mutex);
pthread_cond_destroy (¶ms.done);
with params_t as follows:
struct params {
SVAnchor* ptr;
pthread_mutex_t mutex;
pthread_cond_t done;
int id;
};
typedef struct params params_t;
and then multichaperWrapper is as follows:
void* SVAnchor::multichaperWrapper(void* arg){
return (((params*)arg)->ptr)->multichaper( ((params*)arg));
}
and multichaper is as follows:
void* SVAnchor::multichaper( void *threadarg /*0 <= threadNum < numberofthreads*/ ){
int threadNum;
/* Lock. */
pthread_mutex_lock(&(*(params_t*)(threadarg)).mutex);
/* Work. */
threadNum = (*(params_t*)(threadarg)).id;
/* Unlock and signal completion. */
pthread_mutex_unlock(&(*(params_t*)(threadarg)).mutex);
pthread_cond_signal (&(*(params_t*)(threadarg)).done);
cout<<threadNum<<endl;
...
}
If all you want to do is pass an id to the calling thread you can do so by burying it in the void* parameter, and do so portably. Like,
pthread_create(&threads[i], NULL, multichaper, (void*)threadID );
So, I am writing a small winsock app and I need to make a multi-client server.
I decided to use threads for every new connection, the problem is that I don't know how to pass multiple data to a thread, so I use struct.
Struct:
typedef struct s_par {
char lttr;
SOCKET clientSocket;
} par;
_stdcall:
unsigned __stdcall ClientSession(void *data) {
par param = data;
char ch = param.lttr;
SOCKET clntSocket = param.clientSocket;
// ..working with client
}
Main:
int main() {
unsigned seed = time (0);
srand(seed);
/*
..........
*/
SOCKET clientSockets[nMaxClients-1];
char ch = 'a' + rand()%26;
while(true) {
cout << "Waiting for clients(MAX " << nMaxClients << "." << endl;
while ((clientSockets[nClient] = accept(soketas, NULL, NULL))&&(nClient < nMaxClients)) {
par param;
// Create a new thread for the accepted client (also pass the accepted client socket).
if(clientSockets[nClient] == INVALID_SOCKET) {
cout << "bla bla" << endl;
exit(1);
}
cout << "Succesfull connection." << endl;
param.clientSocket = clientSockets[nClient];
param.lttr = ch;
unsigned threadID;
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &ClientSession, ¶m, 0, &threadID);
nClient++;
}
The problem is that I get errors with data type conversion. Maybe someone could suggest an easy fix with passing this struct to a thread?
With each round of your while-loop you're doing two ill-advised activites:
Passing the address of an automatic variable that will be destroyed with each cycle of the loop.
Leaking a thread HANDLE returned from _beginthreadex
Neither of those is good. Ideally your thread proc should look something like this:
unsigned __stdcall ClientSession(void *data)
{
par * param = reinterpret_cast<par*>(data);
char ch = param->lttr;
SOCKET clntSocket = param->clientSocket;
// ..working with client
delete param;
return 0U;
}
And the caller side should do something like this:
par *param = new par;
param->clientSocket = clientSockets[nClient];
param->lttr = ch;
...
HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, &ClientSession, param, 0, &threadID);
if (hThread != NULL)
CloseHandle(hThread);
else
delete param; // probably report error here as well
That should be enough to get you going. I would advise you may wish to take some time to learn about the C++11 Threading Model. It makes much of this considerably more elegant (and portable!).
Best of luck.
I need some algorithm help with a multithreaded program I'm writing. It's basically the cp command in unix, but with a read thread and a write thread. I'm using semaphores for thread synchronization. I have structs for buffer and thread data defined as
struct bufType {
char buf[BUFFER_SIZE];
int numBytes;
};
struct threadData {
int fd;
bufType buf;
};
and a global array of bufType. Code for my main is
int main(int argc, const char * argv[])
{
int in, out;
pthread_t Producer, Consumer;
threadData producerData, consumerData;
if (argc != 3)
{
cout << "Error: incorrect number of params" << endl;
exit(0);
}
if ((in = open(argv[1], O_RDONLY, 0666)) == -1)
{
cout << "Error: cannot open input file" << endl;
exit(0);
}
if ((out = open(argv[2], O_WRONLY | O_CREAT, 0666)) == -1)
{
cout << "Cannot create output file" << endl;
exit(0);
}
sem_init(&sem_empty, 0, NUM_BUFFERS);
sem_init(&sem_full, 0, 0);
pthread_create (&Producer, NULL, read_thread, (void *) &producerData);
pthread_create (&Consumer, NULL, write_thread, (void *) &consumerData);
pthread_join(Producer, NULL);
pthread_join(Consumer, NULL);
return 0;
}
and read and write threads:
void *read_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
while((thread_data->buf.numBytes = slow_read(thread_data->fd, thread_data->buf.buf, BUFFER_SIZE)) != 0)
{
sem_post(&sem_full);
sem_wait(&sem_empty);
}
pthread_exit(0);
}
void *write_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
sem_wait(&sem_full);
slow_write(thread_data->fd, thread_data->buf.buf, thread_data->buf.numBytes);
sem_post(&sem_empty);
pthread_exit(0);
}
So my issue is in what to assign to my threadData variables in main, and my semaphore logic in the read and write threads. I appreciate any help you're able to give
Being a windows guy who does not use file descriptors I might be wrong with the in's and out's but I think this needs to be done in your main in order to setup the threadData structures.
producerData.fd = in;
consumerData.fd = out;
Then declare ONE SINGLE object of type bufType for both structures. Change for example the definition of threadData to
struct threadData {
int fd;
bufType* buf;
};
and in your Main, you write
bufType buffer;
producerData.buf = &buffer;
consumerData.buf = &buffer;
Then both threads will use a common buffer. Otherwise you would be writing to the producerData buffer, but the consumerData buffer will stay empty (and this is where your writer thread is looking for data)
Then you need to change your signalling logic. Right now your program cannot accept input that exceeds BUFFER_SIZE, because your write thread will only write once. There needs to be a loop around it. And then you need some mechanism that signals the writer thread that no more data will be sent. For example you could do this
void *read_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
while((thread_data->buf->numBytes = slow_read(thread_data->fd, thread_data->buf->buf, BUFFER_SIZE)) > 0)
{
sem_post(&sem_full);
sem_wait(&sem_empty);
}
sem_post(&sem_full); // Note that thread_data->buf->numBytes <= 0 now
pthread_exit(0);
}
void *write_thread(void *data)
{
threadData *thread_data;
thread_data = (threadData *) data;
sem_wait(&sem_full);
while (thread_data->buf->numBytes > 0)
{
slow_write(thread_data->fd, thread_data->buf->buf, thread_data->buf->numBytes);
sem_post(&sem_empty);
sem_wait(&sem_full);
}
pthread_exit(0);
}
Hope there are no more errors, did not test solution. But the concept should be what you were asking for.
You could use a common buffer pool, either a circular array or a linked lists. Here is a link to a zip of a Windows example that is similar to what you're asking, using linked lists as part of a inter-thread messaging system to buffer data. Other than the creation of the mutexes, semaphores, and the write thread, the functions are small and simple. mtcopy.zip .
I want to know how to pass a structure to a thread. I've written an example application where I declare a structure in main and try to pass it to the thread.
Here's my code:
DWORD WINAPI Name1(LPVOID lparam)
{
data x;
x.name[15]="Sarah";
x.DOB="19/10/2007";
fputs(stdout,name,15);
fputs(stdout,DOB,15);
return 0;
}
int main()
{
struct data
{
char name[15];
char DOB[15];
};
HANDLE thread2;
DWORD threadID2;
thread2= CreateThread(NULL,0,Name1,(LPVOID *)data,0,&threadID2);
if(thread2==NULL)
{
cout<<"Couldn't Create Thread:("<<endl;
exit(0);
}
return 0;
}
Unfortunately, I am not getting the hang of passing a structure to a thread :( I would really appreciate it if somebody helped me out.
I tried to change the datatype of the structure to pass it, but, I guess I don't know how to do it.
You are passing a local variable to the thread startup function. Once the variable goes out of scope it will be destroyed. This means it may not exist when the new thread tries to access it. You should either pass by value for integral types or allocate the object in dynamic storage (the heap).
Once the new thread has the pointer to the object it should probably be responsible for destroying it as well. That all depends on how you want to assign and manager ownership of the object.
struct Foo
{
char name[15];
char DOB[15];
};
void Start()
{
Foo *someObject = new Foo();
CreateThread(NULL, 0, threadFunc, (LPVOID *)someObject, 0, &threadID2);
}
DWORD WINAPI threadFunc(void *v)
{
Foo *someObject = static_cast<Foo*>(v);
delete someObject;
return 0;
}
If you want to pass a struct to a thread, you've to get that struct on the heap and not on the stack and pass its address to the thread.
I also fixed a few mistakes... Like string copy, and so on...
I didn't use any typedef, as it appears you're using C++.
struct data{
char name[15];
char DOB[15];
};
DWORD WINAPI Name1(LPVOID lparam)
{
data *x = (data*)lparam;
strcpy(x->name, "Sarah");
strcpy(x->DOB, "19/10/2007");
fputs(stdout, x->name, 15);
fputs(stdout, x->DOB, 15);
HeapFree(GetProcessHeap(), 0, x);
return 0;
}
int main()
{
HANDLE thread2;
DWORD threadID2;
data * x;
x = HeapAlloc(GetProcessHeap(), 0, sizeof(data));
thread2= CreateThread(NULL, 0, Name1, (LPVOID)x, 0, &threadID2);
if(thread2==NULL)
{
cout << "Couldn't Create Thread:(" << endl;
exit(0);
}
return 0;
}
I'm working on a multiclient chat project.
Here is my code:
struct RecvDataModel
{
int sockAddr;
char *inData;
};
void *ProcessData(void *arg);
void Client::Recv(int sockAddr, char *inData)
{
RecvDataModel outData;
outData.sockAddr = sockAddr;
outData.inData = inData;
pthread_t rThr;
pthread_create(&rThr, NULL, ProcessData, (void*)&outData);
}
void *ProcessData(void *arg)
{
RecvDataModel *inData = (RecvDataModel*)arg;
cout << inData->inData << endl;
return 0;
}
Basically if sockAddr (in Client::Recv) equals "55" ProcessData's cout function writing "31784736", if equals "0" cout's "5120"
That's my big problem! I can't continue without this! (I'm using eclipse C++)
What's the problem? I have already looked some example projects like this: Link >>>
You're passing a pointer to a RecvDataModel which is a function-local variable. It will go out of scope at the end of the Client::Recv function.
Try allocating it with new instead:
RecvDataModel * outData = new RecvDataModel();
outData->sockAddr = sockAddr;
outData->inData = inData;
pthread_t rThr;
pthread_create(&rThr, NULL, ProcessData, outData);
Don't pass around pointers to local variables that go out of scope. As soon as you create that thread, outData isn't valid anymore, and so the pointer you gave to it is no good. You need to declare outData with a static qualifier, or allocate space for it dynamically so that it doesn't vanish when Client::Recv returns.