0

I'm writing a server application, task oriented. Each Session (client) send packets to the server, the server enqueues them as Tasks, and workers (threads) handle them. Constraints are :

  • Tasks from a same Session have to be always sequentially executed ( => if a thread begin to handle a Task of a Session, another thread cannot handle a Task for the same Session while the first is not finished)
  • Tasks of some types have to be sequentially executed
  • Tasks of some other type don't have to be sequentially executed

What could be the best approach to do that ? how do we call this concept ? (I guess it is not double conditional-sequence execution)

I ideally would use TBB to achieve that but I don't know if a feature of TBB just fit for my need. (I'm open for any other propositions)

Here a piece of "pseudo-code" which would be my first idea [EDIT: but is wrong, sequential execution is not assured, but the code could explain my idea] :

struct Session {
    atomic<bool> locked;
}
struct Task {
    char type;
    Session* session;
    void execute() { ; }
};

atomic<bool> type_locked[SEQ_TYPE_NUMBER];

threadsafe_selfiterable_list<Task> list;

void do() {
    while( alive ) 
    {
        if( list.empty() )
            relax();
        if( list.has_next() ) {
            task = list.next();
            if( !compare_and_swap( task.session->locked, true ) ) {
                if( is_non_sequential_task_type( task.type ) ) {
                    queue.pop();
                    task.execute();
                } else if( !compare_and_swap( type_locked[task.type], true ) ) {
                    queue.pop();
                    task.execute();
                    type_locked[task.type] = false;
                }
                task.session->locked = false;
            }
        } else 
            list.rewind();
    }
}
Malandrain
  • 91
  • 1
  • 6

1 Answers1

0

Define a 'TaskCollection' Task that can hold, and execute, other tasks. If there are sequential task/s, shove them into a TaskCollection and submit that to your threadpool.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • The problem is the "double" sequential : a same task is sequential by his 'producer', and can be by the type. (note that my pseudo-code is currently wrong) – Malandrain Jul 23 '13 at 14:00