0

I am a bit of a novice with pthreads, and I was hoping someone could help with a problem I've been having. Say you have a collection of threads, all being passed the same function, which looks something like this:

void *func(void *args) {
    ...
    while(...) {
        ...
        switch(...) 
        {
            case one:
                do stuff;
                break;
            case two:
                do other stuff;
                break;
            case three:
                do more stuff;
                break;
        }
        ....
    }
}

In my situation, if "case one" is triggered by ANY of the threads, I need for all of the threads to exit the switch and return to the start of the while loop. That said, none of the threads are ever waiting for a certain condition. If it happens that only "case two" and "case three" are triggered as each thread runs through the while loop, the threads continue to run independently without any interference from each other.

Since the above is so vague, I should probably add some context. I am working on a game server that handles multiple clients via threads. The function above corresponds to the game code, and the cases are various moves a player can make. The game has a global and a local component -- the first case corresponds to the global component. If any of the players choose case (move) one, it affects the game board for all of the players. In between the start of the while loop and the switch is the code that visually updates the game board for a player. In a two player game, if one player chooses move one, the second player will not be able to see this move until he/she makes a move, and this impacts the gameplay. I need the global part of the board to dynamically update.

Anyway, I apologize if this question is trivial, but some preliminary searching on the internet didn't produce anything valuable. It may just be that I need to change the whole structure of the code, but I'm kind of clinging to this because it's so close to working.

Max
  • 1
  • 1
    May I suggest you to read some more technical things about Thread programming ? Especially about Thread synchronization and communication through Mutex and/or Conditional Variables. For example : https://computing.llnl.gov/tutorials/pthreads/ – Jean-Baptiste Yunès Dec 15 '13 at 09:27
  • Are the player threads spending long time doing `other stuff` or `more stuff` and do you want them to be interrupted in doing that, or are the threads mainly waiting for the player to make a move and do you want to interrupt this wait (and with what function do you wait there)? – Armali Nov 11 '16 at 10:20

2 Answers2

0

You need to have an atomic variable that acts as a counter for switch-case. Atomic variables are guarantied to perform math operations atomically which is what multihreaded environments require.

Init the atomic variable at 1 in the main/dispatcher thread and pass it via Args or make it global. Atmic variables are

volatile LONG counter = 1;

For windows use InterlockedAdd. The return value is the previous value.

Each thread does:

LONG val = InterlockedAdd(&counter, 1);
switch(val)
...

For GCC:

LONG val = __sync_fetch_and_add(&counter, 1);
switch(val)
...
egur
  • 7,830
  • 2
  • 27
  • 47
0

May I also suggest, you inform yourself about standard task synchronization schemes first? You might get an idea, how to change your program structure to be more flexible.

Task synchronization basics: (binary semaphores, mutexes)

http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:semaphores_mutexes (if you are interested in InterProcessCommuncation (IPC) in more detail, like message passing, queues, ... just ask!)

Furthermore I recommend reading about implementation of state machines, which could help making your player code more flexible! Bit complex (I know easier resources only in German - maybe a native speaker can help):

http://johnsantic.com/comp/state.html

Is there a typical state machine implementation pattern?

If you want to stick with what you have, a global variable, which can be changed & read by any other task will do.

Regards, Florian

Community
  • 1
  • 1
Florian
  • 372
  • 2
  • 12