Re: Thread safe queue in C




"Behzad" <Sedighzadeh@xxxxxxxxx> wrote in message news:dbb12be7-2214-4e38-bbdf-d4a0978d530c@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hi all,

I am in the middle of developing a multithreaded embedded program
within linux with C and i need a thread safe queue.How can i obtain a
library to do my job? my time is short so developing a library from
scratch is considered a very bad idea.
please conduct me to do that.


Your on Linux, so why not use PThreads? Are you looking for extreme peformance, perhaps real-time? How many producers and consumers will be hitting the queue at any one time? Anyway, creating a queue with POSIX Threads is rather trivial, I don't see why you can't quickly create one:


extremely quick brief pseudo-code
______________________________________________________

typedef struct node_s node;
typedef struct queue_s queue;

struct node_s {
node* next;
void* state;
};

struct queue_s {
node* head; /* = 0 */
node* tail; /* = 0 */
pthread_mutex_t mtx;
pthread_cond_t cond;
};


void queue_push(
queue* const _this,
node* const n,
void* const state
) {
node* tail;
n->next = 0;
n->state = state;
pthread_mutex_lock(&_this->mtx);
if (! (tail = _this->tail)) {
_this->head = n;
} else {
tail->next = n;
}
_this->tail = n;
pthread_mutex_unlock(&_this->mtx);
pthread_mutex_signal(&_this->cond);
}


node* queue_pop(
queue* const _this
) {
node* head;
pthread_mutex_lock(&_this->mtx);
while (! (head = _this->head)) {
pthread_cond_wait(&_this->cond, &_this->mtx);
}
if (! (_this->head = head->next)) {
_this->tail = 0;
}
pthread_mutex_unlock(&_this->mtx);
return head;
}
______________________________________________________


.



Relevant Pages

  • Re: filewatcher question
    ... I guess I can get the file watcher change event to just queue up the file ... Furthermore the file watcher changed event will do a quick look up if the ... I will also have to look up thread safe GUI as well ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Singly Linked List in C
    ... While a linked list is certainly one way to implement a queue, a circular buffer could be a better approach, if you can accept a fixed upper limit to the number of elements in the queue. ... I would not consider it objectionable, but I would want to make sure that you understand that making 'sp' const has nothing to do with protecting the argument of the function from modification. ... The first const would protect the thing that the pointer points at from change. ...
    (comp.lang.c)
  • Re: Thread safe vector class
    ... > between worker threads and the main thread. ... producer/consumer queue. ... If that one doesn't fit your needs, Google for "thread safe producer ...
    (microsoft.public.vc.language)
  • Mail in Exchnage 2003 Queue oder Link anzeigen
    ... Ich komme nur einfach nicht an die Mails in der Queue dran. ... Const admin = "administrator" ... Set objWMILocator2 = CreateObject ... For Each m In PendingQueue ...
    (microsoft.public.de.german.scripting.wsh)
  • Re: Singly Linked List in C
    ... circular buffer could be a better ... elements in the queue. ... It avoids the space overhead of having each node ... reasons that declaring any other local variable const might make sense. ...
    (comp.lang.c)