I have a server application that creates new thread for every incoming request.
However, every once in a while, it will create a thread with thread ID = 0 (used pthread_equal to check this). I have a structure that contains the thread ID that I pass to the function specified in pthread_create, and am checking this there.
Why would a thread get created with ID = 0?
Is there anything I can do if this happens? I cannot use this thread and want to exit it immediately.
=====================================================================
typedef struct
{
pthread_t tid;
other_struct_t Other;
} data_ptr_t;
void * worker(void * arg)
{
data_ptr_t local_data;
data_ptr_t * incoming_data = (data_ptr_t *) arg;
if (NULL == incoming_data || NULL == incoming_data->Other)
{
printf("invalid input\n");
}
else if (pthread_equal(incoming_data->tid, 0))
{
printf("invalid thread id\n");
}
else
{
// add to global thread pool
// do other stuff here
// remove from global thread pool
}
}
int main()
{
// server socket stuff
while (1)
{
// if incoming connection is valid
data_ptr_t data;
int error = pthread_create(&(data.tid), NULL, (void * (*) (void *)) worker, (void *) &data);
if (0 != errror)
{
printf("could not create thread (%d)\n", error);
}
else
{
pthread_detach(data.tid);
printf("thread dispatched\n");
}
}
}
Note: If the number of threads I'm creating is under 50 or so, it works fine. Upwards of 70, most threads go through just fine, the rest end up printing the "invalid thread id".
Note: This is on Linux.
You can't do this:
while (1)
{
// if incoming connection is valid
data_ptr_t data;
int error = pthread_create(&(data.tid),
NULL, (void * (*) (void *)) worker, (void *) &data);
your data_ptr_t is a local variable on the stack. On the next iteration of the while loop, that variable is destroyed/gone/not-valid.
The while loop might start another iteration long before the new worker thread starts running and makes use of the data you pass to it. Instead, dynamically allocate the data you pass to the worker thread so you can be sure it's still valid.
Related
while(1) {
char message_buffer[SIZE];
ssize_t message_length = mq_receive(mq_identifier, message_buffer, _mqueue_max_msg_size NULL);
if(message_len == -1) { /* error handling... */}
pthread_t pt1;
int ret = pthread_create(&pt1, NULL, handle_message, message_buffer);
if(ret) { /* error handling ... */}
}
void * handle_message (void * message) {
puts((char *) message);
return NULL;
}
The above example is not an MRE but it is extremely simple:
I've got a main thread with a loop that constantly consumes messages from a message queue. Once a new message is received, it is stored in the local message_buffer buffer. Then, a new thread is spawned to "take care" of said new message, and thus the message buffer's address is passed into handle_message, which the new thread subsequently executes.
The problem
Often, 2 threads will print the same message, even though I can verify with a 100% certainty that the messages in the queue were not the same.
I am not completely certain, but I think I understand why this is happening:
say that I push 2 different messages to the mqueue and only then I begin consuming them.
In the first iteration of the while loop, the message will get consumed from the queue and saved to message_buffer. A new thread will get spawned and the address of message_length passed to it. But that thread may not be fast enough to print the buffer's contents to the stream before the next message gets consumed (on the next iteration of the loop), and the contents of message_buffer subsequently overridden. Thus the first and second thread now print the same value.
My question is: what is the most efficient way to solve this? I'm pretty new to parallel programming and threading/pthreads and I'm pretty overwhelmed by the different synchronization primitives.
Mutex trouble
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
while(1) {
char message_buffer[SIZE];
pthread_mutex_lock(&m);
ssize_t message_length = mq_receive(mq_identifier, message_buffer, _mqueue_max_msg_size NULL);
pthred_mutex_unlock(&m);
if(message_len == -1) { /* error handling... */}
pthread_t pt1;
int ret = pthread_create(&pt1, NULL, handle_message, message_buffer);
if(ret) { /* error handling ... */}
}
void * handle_message (void * message) {
char own_buffer[SIZE];
pthread_mutex_lock(&m);
strncpy(own_buffer, (char *) message, SIZE);
pthread_mutex_unlock(&m);
puts(own_buffer);
return NULL;
}
I don't think my current mutex implementation is right as the threads are still receiving duplicate messages. The main thread can lock the mutex, consume a message into the buffer, unlock the mutex, spawn a thread, but that thread still may hang and the main one could just rewrite the buffer again (as the buffer mutex was never locked by the new thread), effectively making my current mutex implementation useless? How do I overcome this?
The problem is that you end the loop that contains message_buffer before guaranteeing that the thread has finished with that memory.
while (1) {
char message_buffer[SIZE];
ssize_t message_length = mq_receive(...);
if (message_len == -1) { /* error handling */ }
pthread_t pt1;
int ret = pthread_create(&pt1, NULL, handle_message, message_buffer);
if (ret) { /* error handling */ }
/****** Can't go beyond here until thread is done with message_buffer. ******/
}
void * handle_message (void * message) {
char own_buffer[SIZE];
strncpy(own_buffer, (char *) message, SIZE);
/******* Only now can the caller loop back. ******/
puts(own_buffer);
return NULL;
}
You could use a semaphore or similar.
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static int copied = 0;
while (1) {
char message_buffer[SIZE];
ssize_t message_length = mq_receive(...);
if (message_len == -1) { /* error handling */ }
pthread_t pt1;
int ret = pthread_create(&pt1, NULL, handle_message, message_buffer);
if (ret) { /* error handling */ }
// Wait until threads is done with message_buffer.
pthread_mutex_lock(&mutex);
while (!copied) pthread_cond_wait(&cond, &mutex);
copied = 0;
pthread_mutex_unlock(&mutex);
}
void * handle_message (void * message) {
char own_buffer[SIZE];
strncpy(own_buffer, (char *) message, SIZE);
// Done with caller's buffer.
// Signal caller to continue.
pthread_mutex_lock(&mutex);
copied = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
puts(own_buffer);
return NULL;
}
(The added chunks effectively perform semaphore operations. See the last snippet of this answer for a more generic implementation.)
But there's a simpler solution: Make the copy before creating the thread.
while (1) {
char message_buffer[SIZE];
ssize_t message_length = mq_receive(...);
if (message_len == -1) { /* error handling */ }
pthread_t pt1;
int ret = pthread_create(&pt1, NULL, handle_message, strdup(message_buffer));
if (ret) { /* error handling */ }
}
void * handle_message (void * message) {
char * own_buffer = message;
puts(own_buffer);
free(own_buffer);
return NULL;
}
I've been trying to pass a variable to threads ,but the instant a second thread is created the value changes despite it being created as a constant variable.
//for the first user
if (flag == 0) {
//store the socket
cl_sc[0] = client_sock_desc;
//set id
whichOne[0] = 0;
puts("Client accepted");
//second user ,same procedure
}
else if (flag == 1) {
cl_sc[1] = client_sock_desc;
whichOne[0] = 1;
puts("Client accepted");
}
//create thread and pass cl_sc as arguement
pthread_t sTcThread;
pthread_create(&sTcThread, NULL, server_to_client, (void*)whichOne);
And here is the thread implimentation
void* server_to_client(void* socket_desc)
{
//make the arguement readable
const int* whichOne = (int*)socket_desc;
//one int for retrieved data and one for his socket
int retrieve, socket = cl_sc[whichOne[0]];
//chat buddy socket
int palsSocket;
//the actual data
char data[DATA_LENGTH];
//free the string
memset(data, 0, DATA_LENGTH);
for (;;) {
//set accordingly
if (whichOne[0] == 0) {
palsSocket = cl_sc[1];
}
else if (whichOne[0] == 1) {
palsSocket = cl_sc[0];
}
printf("Im %d to join my socket is %d and my pals socket is %d\n", whichOne[0], socket, palsSocket);
}
}
Results of the execution
Client accepted Im 0 to join my socket is 4 and my pals socket is 0
Client accepted Im 1 to join my socket is 5 and my pals socket is 4
But the moment thread 0 is activated this happens
Im 1 to join my socket is 4 and my pals socket is 4
Changing the whichOne constant to 1.
Solved as such thanks to the answers and comments .
int *whichOneImported = (int *) socket_desc;
int whichOne = whichOneImported[0];
const int *whichOne = (int *) socket_desc; guarantees that whichOne isn't changed by an instruction of your subroutine, but not by another thread, since it points on a shared memory area (you pass the address to the whichOne array when you create your thread, and you modify its value afterwards when flag == 1)
you set whichOne[0] = 1; in the other thread, and I'm not sure of the logic but maybe you meant:
whichOne[flag] = flag;
and then
pthread_create(&sTcThread, NULL, server_to_client, (void*) (whichOne+flag));
to separate the data
Or make a allocated copy of whichOne prior to running the thread.
I'm trying to implement a server that deals with many tcp connections, from 100 - 1000 connections/day based on the time of the day. after reading a lot about the c10k problem with thread per connection and using epoll only I decided to use both as a pool of threads and the main will act as a dispatcher, so each new connection will be assigned to a thread.
I have a number of questions that I can't find answers anywhere else.
Is the following thread safe? Do I need to lock before adding the new fd?
int main ()
{
while(i < number_threads)
{
pthread_create( &id , NULL , worker , (void*) epoll_fd[i]);
i++;
}
//is it ok to add the new_sock for the epoll_fd[i] so the thread can pick it up
int y = 0;
while(1) {
new_sock = accept(...);
if (epoll_ctl(epoll_fd[y], EPOLL_CTL_ADD, new_sock, &ev) < 0)
{
print error;
}
y++;
if (y == number_threads)
y = 0;
}
}
void *worker(void *epfd)
{
epoll_wait //start waiting for event
}
If you do this:
pthread_create( &id , NULL , worker , (void*) epoll_fd + i);
and in the thread function, this:
void *worker(void *vp_epfd) {
int *p_epfd = (int*) vp_epfd;
then it should work, and be thread-safe, assuming that you check for updates in *p_epfd in correct places.
I have a program that is trying to use create and cancel through an implemented pool.
The creation is as follows:
while (created<threadsNum){
pthread_t newThread;
pthread_struct *st; //Open the thread that handle the deleting of the sessions timeout.
st = (pthread_struct*)malloc(sizeof(pthread_struct));
st->id = created;
st->t = &newThread;
pthread_mutex_lock( &mutex_threadsPool );
readingThreadsPool[created] = st;
pthread_mutex_unlock( &mutex_threadsPool );
if((threadRes1 = pthread_create( &newThread, NULL, pcapReadingThread, (void*)created)))
{
syslog(LOG_CRIT, "Creating Pcap-Reading Thread %d failed.",created);
printf( "Creating Pcap-Reading Thread %d failed.\n",created);
exit(1);
}
syslog(LOG_INFO, "Created Pcap-Reading Thread %d Successfully.",created);
created++;
}
Later I try to cancel them and restart them :
pthread_t* t;
pthread_struct* tstr;
int i;
pthread_mutex_unlock( &mutex_threadsPool );
//first go on array and kill all threads
for(i = 0; i<threadsNum ; i++ ){
tstr = readingThreadsPool[i];
if (tstr!=NULL){
t = tstr->t;
//Reaches here :-)
if (pthread_cancel(*t)!=0){
perror("ERROR : Could not kill thread");
}
else{
printf("Killed Thread %d \n",i);
}
//doesnt reach here
}
}
I checked the addresses in the memory of the created thread in part one and the address of the about to be cancelled thread in the second part..they match..
I read about the thread manager that can't work if one calls killall().
But I don't..
Anyone have any idea?
Thanks
while (created<threadsNum){
pthread_t newThread;
pthread_struct *st;
/* ... */
st->t = &newThread;
/* ... */
}
You've got st->t pointing to a local variable newThread. newThread is only in scope during the current loop iteration. After this iteration st->t will contain an invalid address.
newThread is on the stack, so after it goes out of scope that stack space will be used for other variables. That could be different pthread_ts on successive iterations, or once the loop is over then that stack space will be used for completely different types of values.
To fix this I'd probably change pthread_struct.t to be a pthread_t instead of a pthread_t *, and then change the pthread_create call to:
pthread_create(&st->t, /*...*/)
Also, you should be careful about adding st to the thread pool before you've called pthread_create. It should probably be added after. As it stands, there's a small window where st->t is on the thread pool but has not been initialized.
I created dispatch_queue_thread_t struct in the headerfile.
This code assign the thread, task and queue to the dThread struct
dispatch_queue_thread_t *dThread;
dThread = (dispatch_queue_thread_t *) malloc(sizeof(dispatch_queue_thread_t));
pthread_t dispatcher_thread;
if(pthread_create(&dispatcher_thread, NULL, dispatcher_threadloop, (void *)dispatch_queue)){
perror("ERROR creating thread."); exit(EXIT_FAILURE);
}
dThread->task=NULL;
dThread->queue=dispatch_queue;
dThread->thread=dispatcher_thread;
This code is the thread functions for dispatcher_thread.
I need to use thread in dThread to check if there is any task is assigned to it and if not need to assign the task to it.
How do I do that?
Is my code correct?
void *dispatcher_threadloop(void * queue){
//thread loop of the dispatch thread- pass the tast to one of worker thread
dispatch_queue_t *dQueue;
dQueue=queue;
//can I do this?
dispatch_queue_thread_t *dThread;
printf("message-boss1");
dQueue = (dispatch_queue_t *)queue;
if (dQueue->HEAD!=NULL){
for(;;){
sem_wait(dQueue->queue_task_semaphore);
dThread->task = dQueue->HEAD;
dQueue->HEAD = dQueue->HEAD->next;
dQueue->HEAD->prev = NULL;
sem_post(dQueue->queue_task_semaphore);
//TODO
}
}
printf("message-boss2");
}
No. The dThread variable in dispatcher_threadloop() isn't initialised, so it's an error to dereference it.
It seems like you should be passing dThread to the thread function instead of dispatchQueue, as the thread function can obtain the latter from the former. Something like this (note that casting to and from void * is unnecessary):
dispatch_queue_thread_t *dThread;
dThread = malloc(sizeof *dThread);
dThread->task = NULL;
dThread->queue = dispatch_queue;
if (pthread_create(&dThread->thread, NULL, dispatcher_threadloop, dThread)) {
perror("ERROR creating thread.");
exit(EXIT_FAILURE);
}
then in the thread function:
void *dispatcher_threadloop(void *arg)
{
dispatch_queue_thread_t *dThread = arg;
dispatch_queue_t *dQueue = dThread->queue;
/* ... */